Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 25 Sep 2011
Offline Last Active Jan 25 2013 02:00 AM

Posts I've Made

In Topic: Needing advice on 2D collision response.

09 December 2012 - 04:04 AM

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));

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 Posted Image

Although this doesn't work maybe some tips can be salvaged out of this?
Vector2 direction = Vector2.Normalize(collisionPoint - player1Origin);
player1Pos = collisionPoint - (direction * player1Speed);

In Topic: Needing advice on 2D collision response.

01 December 2012 - 06:03 AM

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 Posted Image
			    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;

In Topic: Needing advice on 2D collision response.

01 December 2012 - 03:31 AM

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:

if (fgACollisionP1.X <= player1Pos.X)
	 player1Pos -= Vector2.Normalize(player1Pos - fgACollisionP1) * shipSpeed;
	 player1Pos -= Vector2.Normalize(fgACollisionP1 - player1Pos) * shipSpeed;

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.