Sign in to follow this  
Followers 0
skuhtz

Needing advice on 2D collision response.

6 posts in this topic

I've been struggling with this for a while and was determined to figure it out on my own, but I give up.

I'm using per pixel collision detection in a scrolling shooter, which works just fine - It's the collision response that I've been struggling with.
The bit of code bellow works perfectly fine while colliding with the terrain directly bellow the ship, but in any other direction the collision responds differently. What I'm trying to do with the code bellow is find the coordinates of where the collision takes place, turn that into a direction and then negate the ships movement based on that direction... Or at least I'm pretty sure that's what I'm trying to do. I've been staring at it for too long and it's almost 3am lol.

fgACollisionP1 is where the collision takes place.

[CODE]
Vector2 newPos = player1Pos - fgACollisionP1;
player1Pos += Vector2.Normalize(newPos) * 8;
[/CODE]

Can anyone shed some light on this?
0

Share this post


Link to post
Share on other sites
Doesn't normalize(newPos) give you the direction from the collision point back to the player's position? So adding it to the player's position wouldn't be correct then. Maybe you want

Vector2 newPos = fgACollisionP1 - player1Pos;

Not 100% sure though.

EDIT: Nevermind ... I suppose you want the collision response to push the playerPos away from the collision point, so what you've got seems OK.

Maybe the strange rebound is happening from the velocity of the player? Do you need to cross the player's direction vector with the response vector?

See if you can scavenge anything useful out of this stuff from Bullet's character controller:

[code]
/*
* Returns the reflection direction of a ray going 'direction' hitting a surface with normal 'normal'
*
* from: http://www-cs-students.stanford.edu/~adityagp/final/node3.html
*/
btVector3 btKinematicCharacterController::computeReflectionDirection (const btVector3& direction, const btVector3& normal)
{
return direction - (btScalar(2.0) * direction.dot(normal)) * normal;
}
/*
* Returns the portion of 'direction' that is parallel to 'normal'
*/
btVector3 btKinematicCharacterController::parallelComponent (const btVector3& direction, const btVector3& normal)
{
btScalar magnitude = direction.dot(normal);
return normal * magnitude;
}
/*
* Returns the portion of 'direction' that is perpindicular to 'normal'
*/
btVector3 btKinematicCharacterController::perpindicularComponent (const btVector3& direction, const btVector3& normal)
{
return direction - parallelComponent(direction, normal);
}
[/code] Edited by Steve_Segreto
0

Share this post


Link to post
Share on other sites
Thanks for the reply. I couldn't make out anything useful from the code you posted, but what you said before got me thinking a little differently. I revised the code to look like this:

[CODE]
if (fgACollisionP1.X <= player1Pos.X)
player1Pos -= Vector2.Normalize(player1Pos - fgACollisionP1) * shipSpeed;
else
player1Pos -= Vector2.Normalize(fgACollisionP1 - player1Pos) * shipSpeed;
[/CODE]

subtracting the collision point from the players position works fine while colliding with the back end of the ship and subtracting the player's position from the collision point works the same as before. The problem though, is when I put them into this if statement the first line doesn't read. I would figure this statement would check to see if the collision takes place to the left or right of the ship?

Also, it's not actually a bouncing effect I'm getting, it's the opposite. The angles that aren't working properly actually suck the ship into the image rather than negating the movement. Edited by skuhtz
0

Share this post


Link to post
Share on other sites
If you want to negate the movement, might it be an option to reduce the speed to zero and then place the player excactly on the spot where it collided? Just a suggestion, I don't know excactly what you are trying to achieve.

Aart
0

Share this post


Link to post
Share on other sites
I've gotten a lot closer with this... still not quite working correctly but I feel like I'm on the right track. I've basically divided the player's ship into 4 quadrants and adjusting the response based on the location of the collision in relation to the quadrants. I'm hitting the sack though, hopefully I can resolve this tomorrow [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]
[CODE]
if (fgACollisionP1.X < (player1Pos.X + (player1.Width / 4)) |
fgACollisionP1.Y < (player1Pos.Y + (player1.Height / 4)))
player1Pos -= Vector2.Normalize(player1Pos - fgACollisionP1) * player1Speed;
else if (fgACollisionP1.X > (player1Pos.X + (player1.Width * 0.75f)) |
fgACollisionP1.X > (player1Pos.Y + (player1.Height * 0.75f)))
player1Pos -= Vector2.Normalize(fgACollisionP1 - player1Pos) * player1Speed;
[/CODE]
0

Share this post


Link to post
Share on other sites
This is a bit of code obtained from Nick Gravelyn's RPG tutorial which will bounce the player character away from an nc character it collides with, you could possibly make something useful out of it. Sprite is the PC that is moving into s which is the npc, the Origin is the Position + an offset which is where you determine collision points.

if (AnimatedSprite.AreColliding(sprite, s))
{
// we are assuming that npcs will never move
Vector2 d = Vector2.Normalize(s.Origin - sprite.Origin);
sprite.Position =
s.Position - (d * (sprite.CollisionRadius + s.CollisionRadius));
}
0

Share this post


Link to post
Share on other sites
[quote name='LordRhys' timestamp='1354637069' post='5007108']
This is a bit of code obtained from Nick Gravelyn's RPG tutorial which will bounce the player character away from an nc character it collides with, you could possibly make something useful out of it. Sprite is the PC that is moving into s which is the npc, the Origin is the Position + an offset which is where you determine collision points.

if (AnimatedSprite.AreColliding(sprite, s))
{
// we are assuming that npcs will never move
Vector2 d = Vector2.Normalize(s.Origin - sprite.Origin);
sprite.Position =
s.Position - (d * (sprite.CollisionRadius + s.CollisionRadius));
}
[/quote]

I modified the code using ideas out of this, but I still can't get it working properly. This is the only thing I've had major problems with and it's so aggravating [img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]

Although this doesn't work maybe some tips can be salvaged out of this?
[CODE]
Vector2 direction = Vector2.Normalize(collisionPoint - player1Origin);
player1Pos = collisionPoint - (direction * player1Speed);
[/CODE] Edited by skuhtz
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0