Hey everyone, Im a little new to java and im having a problem with my collision detection.
So let me explain my problem. When a player presses the up key the ingame character (wich is a rectangle atm) increases its Y Coords by 100 and returns to 0 after hitting 100. Iv added some collision detection when comming from the right or left. if the char come’s from left -> right it will substract 1 from its X Coords. Same thing when the char come’s from right -> left except increasing the X Coords upon collision.
When the char is comming down from the jump his Y coords increases, when he the char collides while comming down it will decrease its y coord by 1 every attempt of moving down. At this point the char is ontop of the rectangle wich it is colliding with and 35pixels above ground level. How can i get the char back down to ground level?
I tried many diffrent ways, one of them was detecting howmany pixels the char was above ground level, and every 5ms making it go down. But the problem with that is that it wont let me go up anymore because it keeps pusing me down, found that out the hard way ^^.
This is my code:
Check collision method: call’s the method every 5 ms.
public void checkCollision() {
Rectangle r = new Rectangle(rx, ry, 50, 50);
if(p.getBounds().intersects(r)) {
if(p.moveUp == true)
p.changeY(1);
if(p.jumping == true)
p.changeY(-1);
if(p.moveLeft == true)
p.changeX(1);
if(p.moveRight == true)
p.changeX(-1);
p.colliding = true;
} else {
p.colliding = false;
}
}
My character jumping method:
public void jump() {
if(jumpMovement < 100 && inJump == true) {
jumpMovement++;
changeY(-1);
} else if(jumpMovement > 0 && inJump == false) {
jumpMovement--;
changeY(1);
} else {
jumping = false;
}
if(jumpMovement == 100) {
inJump = false;
}
}
Thanks in advance
Also: Im sorry if this isnt enough information but im not really sure how to describe my problem… If anything is unclear do post so, thanks
[EDIT] While im at it, whats the best way to calculate wich side its colliding on? above, left, right, or from under?