How can I make a rectangular collision of an image?

I am having trouble with making an image collide with something

The image i use: 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();
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

Hi,

Welcome to the forum! :wink:

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 :

k iā€™ll see that. thx

1 Like