• Create Account

### #ActualTankyroo

Posted 16 August 2012 - 08:19 AM

Thanks for the replies everyone.

Okay, I've taken some of what each of you guys said, and I think I have a very very basic version of what I want, the only problem is, it's only on the X-axis.

Here is what I have so far:

if(p.p.intersects(c.c) && p.x + p.width <= c.x + 5 && p.x + p.width >= c.x - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.x <= c.x + c.width + 5 && p.x >= c.x + c.width - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.y + p.height >= c.y - 5 && p.y + p.height <= c.y + 15){
p.y -= p.yVelocity * 60 / 1000.0;
p.yVelocity = 0.0f;
p.onGround = true;
}

This was in the update function of the main class by the way.

Please note that I'm only writing the collision code against one rectangle because this is just to learn the basics of collision detection.

What's happening now is the player will stop when he hits either side on the X-axis (just like I want), but if he jumps onto the block colliding on the Y-axis, he continuously bounces up and down very slightly, making my "onGround" boolean go from true to false very rapidly so the player is unable to jump. What can I do to fix this?

Also, I haven't done any collision detection on the bottom yet, but I'm assuming it will be similar to how I've done it on the X-axis.

P.S. The multiplying the velocity by 60 / 1000.0 is to simulate multiplying it by delta as I've done in the player class. Please tell me if I'm misunderstanding this whole delta thing.

### #1Tankyroo

Posted 16 August 2012 - 08:16 AM

Thanks for the replies everyone.

Okay, I've taken some of what each of you guys said, and I think I have a very very basic version of what I want, the only problem is, it's only on the X-axis.

Here is what I have so far:

if(p.p.intersects(c.c) && p.x + p.width <= c.x + 5 && p.x + p.width >= c.x - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.x <= c.x + c.width + 5 && p.x >= c.x + c.width - 5){
p.x -= p.xVelocity * 60 / 1000.0;
p.xVelocity = 0.0f;
}
if(p.p.intersects(c.c) && p.y + p.height >= c.y - 5 && p.y + p.height <= c.y + 15){
p.y -= p.yVelocity * 60 / 1000.0;
p.yVelocity = 0.0f;
p.onGround = true;
}

This was in the update function of the main class by the way.

Please note that I'm only writing the collision code against one rectangle because this is just to learn the basics of collision detection.

What's happening now is the player will stop when he hits either side on the X-axis (just like I want), but if he jumps onto the block colliding on the Y-axis, he continuously bounces up and down very slightly, making my "onGround" boolean go from true to false very rapidly so the player is unable to jump. What can I do to fix this?

Also, I haven't done any collision detection on the bottom yet, but I'm assuming it will be similar to how I've done it on the X-axis.

PARTNERS