We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to make a program where the image changes direction when two images collide.
I want to use this code but I'm uncertain where I put it? Also what do I substitute for ob1 (object1) and x1 and where do I put the width and height variables? Thanks
boolean Collision()
( ob1 - x1, ob1 -y1, ob1 - w, ob1 - h,
ob2 - x1, ob2 -y1, ob2 -w, ob2-h)
return
(ob1 - x1 < ob2 - x2 && ob1 - x2 > ob2 = x1 && //insert y variables
//start of code
class Bouncy
{
int x;
int y;
int dx;
int dy;
PImage nw, ne, sw, se;
Bouncy(int x, int y, int dx, int dy)
{
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
nw = loadImage("NorthW.png");
ne = loadImage("NorthE.png");
sw = loadImage("SouthW.png");
se = loadImage("SouthE.png");
}
void update()
{
render();
move();
}
void render()
{
if (dx == -1 && dy == -1)
image(nw,x,y);
else if (dx == 1 && dy == -1)
image(ne,x,y);
else if (dx == -1 && dy == 1)
image(sw,x,y);
else if (dx == 1 && dy == 1)
image(se,x,y);
}
void checkCollisions()
{
int edge = 65; // half width of one of the PNG files
if (y<=(edge-edge)) // hit top border
dy=1; // switch to moving downwards
if (y>=500-(edge*2)) // hit bottom border
dy=-1; // switch to moving upwards
if (x<=edge-edge) // hit left border
dx=1; // switch to moving right
if (x>=500-(edge*2)) // hit right border
dx=-1;
}
void move()
{
checkCollisions();
x += dx;
y += dy;
}
}
Bouncy janet,jeff,jerry;
void setup()
{
size(500,500);
janet = new Bouncy(10,100,1,-1);
jeff = new Bouncy(10,150,-1,1);
jerry = new Bouncy(10,350,1,1);
}
void draw()
{
background(255);
janet.update();
jeff.update();
jerry.update();
}
Answers
Just edited this, if anyone could point me in the right direction?
Check these examples:
https://processing.org/examples/bounce.html
https://processing.org/examples/circlecollision.html
https://processing.org/examples/bouncybubbles.html
You also need to become familiar with how the image function works: https://processing.org/reference/image_.html specifically working with 5 parameters.
Based on line 65, it seems your image files have a dimension of 65. Are they square?
Kf
See also this general tutorial on collision detection, with many examples: