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!