# How can I make a rectangular collision of an image?

**URL:** https://discourse.processing.org/t/how-can-i-make-a-rectangular-collision-of-an-image/28538
**Category:** Coding Questions
**Created:** [March 14, 2021, 7:36pm UTC](https://discourse.processing.org/t/how-can-i-make-a-rectangular-collision-of-an-image/28538 "2021-03-14T19:36:55Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![vinijoncrafts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vinijoncrafts/32/14306_2.png) [@vinijoncrafts](https://discourse.processing.org/u/vinijoncrafts)
#### Post date: [March 14, 2021, 7:36pm UTC](https://discourse.processing.org/t/how-can-i-make-a-rectangular-collision-of-an-image/28538/1 "2021-03-14T19:36:55Z")

</div>

I am having trouble with making an image collide with something

The image i use: [https://drive.google.com/file/d/1yGA6DMe9KDWJG15VfWLT1DlvyNVDuhCg/view](https://drive.google.com/file/d/1yGA6DMe9KDWJG15VfWLT1DlvyNVDuhCg/view)

The code:

> Ship Ship;  
> Obstacles Obstacles;  
> //KeyBinds Keys;  
> PImage ship;  
> float x, y, w, h;  
> float obsX = 1920;  
> float vel = 1;  
> float aceleramento = 0.7;  
> float acelerar = 0.7;  
> int vel\_maxima = 30;
> 
> void setup() {  
> size(1920, 1000);  
> surface.setLocation(0, 0);  
> ship = loadImage(“ship.png”);  
> Ship = new Ship();  
> Obstacles = new Obstacles();  
> //Keys = new KeyBinds();  
> x = 100;  
> y = 100;  
> w = ship.width;  
> h = ship.height;  
> }
> 
> void draw() {  
> y += vel;  
> vel += acelerar;
> 
> background(255);  
> [//Keys.keybinds](https://Keys.keybinds)();  
> Obstacles.circles();  
> Ship.rotating();  
> Ship.actions();  
> println(x, y);  
> }
> 
> class Obstacles {  
> void circles() {  
> obsX -= 5;  
> pushMatrix();  
> translate(obsX, width/2);  
> fill(0);  
> circle(0, 0, 100);  
> popMatrix();  
> }  
> }
> 
> class Ship {
> 
> void rotating() {  
> imageMode(CENTER);  
> pushMatrix();  
> translate(x, y);  
> rotate(radians(vel));  
> image(ship, 0, 0);  
> popMatrix();  
> imageMode(CORNER);  
> }
> 
> void actions() {  
> if (mousePressed) {  
> if (mouseButton == LEFT) {  
> acelerar = -aceleramento;  
> }  
> } else {  
> acelerar = aceleramento;  
> }
> 
> ```
> if (y > height - 45) {
> y = height - 45; 
> vel = 0;
> }
> 
> if (y < height - height + 45) {
> y = height - height + 45; 
> vel = 0;
> }
> 
> if(vel > vel_maxima) {
> vel = vel_maxima; 
> }else if(vel < -vel_maxima) {
> vel = -vel_maxima; 
> }
> 
> if (dist(height - height + 45, height - height + 45, y, y) < 20 && mousePressed) {
> y = height - height + 45;
> vel = 0;
> } else if (dist(height - 45, height - 45, y, y) < 30 && !mousePressed) {
> y = height - 45;
> vel = 0;
> }
> 
> ```
> 
> }  
> }

If someone can help me with this it would be great! Also if the code can be a little… “optimized”, tell me pls

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [March 15, 2021, 9:11am UTC](https://discourse.processing.org/t/how-can-i-make-a-rectangular-collision-of-an-image/28538/2 "2021-03-15T09:11:08Z")

</div>

Hi,

Welcome to the forum! 😉

Remember to put your code between backticks like this ``` ``` or use the `</>` button when editing a message. (you can also edit your previous message)

You might want to look at this previous thread for rectangle collision :

> [@Collision of two rectangles](https://discourse.processing.org/t/collision-of-two-rectangles/22010/6):
>
> in p5, but easily portable … [[Coding Challenge #31: Flappy Bird] ](https://www.youtube.com/watch?v=cXgA1d_E-jY)

---

<div class="post-metadata">

### Author: ![vinijoncrafts](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/vinijoncrafts/32/14306_2.png) [@vinijoncrafts](https://discourse.processing.org/u/vinijoncrafts)
#### Post date: [March 15, 2021, 11:53am UTC](https://discourse.processing.org/t/how-can-i-make-a-rectangular-collision-of-an-image/28538/3 "2021-03-15T11:53:43Z")

</div>

k i’ll see that. thx
