We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Collisions between two objects
Page Index Toggle Pages: 1
Collisions between two objects (Read 591 times)
Collisions between two objects
Mar 18th, 2010, 7:28pm
 
Hi!
As the title states, I'm attempting to detect a collision between two objects, ignoring the object that is attempting to detect the collisions itself.

I've pasted the code below:

Code:
// These are actually in three different "tabs", so I'll separate them by comments

int x = 0;
int y = 0;

Vector entities = new Vector();

int numEntities = 0;

void setup() {
size(800, 640);
// Init all controller keys to "not pressed"
for (int i = 0; i < 4; i++) {
keys[i] = false;
}
entities.add(new Entity(mouseX, mouseY));
}

void draw() {
background(255);
move();
drawEntities();
}



void move() {
if(keys[W] == true) { //Move the left player up.
if (( (Entity) entities.get(0) ).getCanJump()) {
( (Entity) entities.get(0) ).setJumping(true);
}
}
if(keys[A] == true) { //Move the left player down.
( (Entity) entities.get(0) ).setX( ( (Entity)entities.get(0) ).getX()-6 );
}
if(keys[S] == true) { //Move the right player up.
( (Entity) entities.get(0) ).setY( ( (Entity)entities.get(0) ).getY()+6 );
}
if(keys[D] == true) { //Move the right player down.
( (Entity) entities.get(0) ).setX( ( (Entity)entities.get(0) ).getX()+6 );
}
}

void mousePressed() {
entities.add(new Entity(mouseX, mouseY));
}

void drawEntities() {
for (int i=0; i<entities.size(); i++){
((Entity)entities.get(i)).update();
}
}

// new tab

class Entity {

int x;
int y;

int ewidth;
int eheight;

float vspeed;
float hspeed;


boolean canJump;
boolean jumping;

int jumpStep;

Entity (int x, int y) {

this.x = x;
this.y = y;

this.vspeed = 9.8;
this.canJump = true;
this.jumping = false;
this.jumpStep = 0;

this.ewidth = 50;
this.eheight = 50;
}
int getX() {
return this.x;
}
int getY() {
return this.y;
}
int getEWidth() {
return this.ewidth;
}
int getEHeight() {
return this.eheight;
}
void setX(int x) {
x = max(0, min(800-50, x));
this.x = x;
}
void setY(int y) {
y = max(0, min(640-50, y));
this.y = y;
}
void calculateGravity() {
this.vspeed = this.vspeed += .5;
}
boolean getCanJump() {
return this.canJump;
}
void setJumping(boolean jumping) {
this.vspeed = -10;
this.canJump = false;
this.jumping = jumping;
}
void update() {
calculateGravity();

if ((int)floor(this.y+this.vspeed) >= 590 || collWithAnyEntityY() != -1) {
this.y = 590;
this.jumping = false;
this.canJump = true;
}
else {
this.y = (int)floor(this.y+this.vspeed);
}

this.x = max(0, min(750, (int)ceil(this.x+this.hspeed)));
rect(this.x, this.y, this.ewidth, this.eheight);
}
boolean collWithEntityX(Entity entity) {
return !(this.x > entity.getX() + entity.getEWidth() || this.x+this.ewidth < entity.getX());
}
boolean collWithEntityY(Entity entity) {
return !(this.y > entity.getY() + entity.getEHeight() || this.y + this.eheight < entity.getY());
}
int collWithAnyEntityX() {
int myID = entities.indexOf(this);
int collID = -1;
for (int i=0; i<entities.size(); i++){
if (i != ) {
if (collWithEntityX(((Entity)entities.get(i)))) {
collID = i;
}
}
}
return collID;
}
int collWithAnyEntityY() {
int myID = entities.indexOf(this);
int collID = -1;
for (int i=0; i<entities.size(); i++){
if (i != this.id) {
if (collWithEntityY(((Entity)entities.get(i)))) {
collID = i;
}
}
}
return collID;
}
}

// new tab

boolean[] keys = new boolean[4];
final int W = 0;
final int A = 1;
final int S = 2;
final int D = 3;

void keyPressed() {
switch(key){
case('d'):
keys[D] = true;
break;
case('w'):
keys[W] = true;
break;
case('a'):
keys[A] = true;
break;
case('s'):
keys[S] = true;
break;
}
}

void keyReleased() {
switch(key){
case('d'):
keys[D] = false;
break;
case('w'):
keys[W] = false;
break;
case('a'):
keys[A] = false;
break;
case('s'):
keys[S] = false;
break;
}
}


as I seem to continuously get the problem of all of the "Entities" sticking to the ground after I add a new one.

Thanks in advice for your help!

EDIT: In fact, I actually wish to detect on which side of the square the collision is taking place - the top, the bottom, the right, or the left. Thanks!
Re: Collisions between two objects
Reply #1 - Mar 19th, 2010, 6:25am
 
The code you posted has errors preventing the program from running.

You said
Quote:
I'm attempting to detect a collision between two objects, ignoring the object that is attempting to detect the collisions itself


In the methods collWithAnyEntityY() and collWithAnyEntityX(0 change the if statement inside the loop with

Code:
if (entities.get(i) != this) {


This will prevent the box checking against itself.

Not the full solution to your problem but it is a starting point.
Re: Collisions between two objects
Reply #2 - Mar 19th, 2010, 11:31am
 
Thanks for replying Quinn!
I must have pasted it wrong, somehow. ?

Anyways, thanks for that tip! I'm still working on the original problem though, so if anyone could lend a hand, I'd be very grateful.

Thanks!
Page Index Toggle Pages: 1