Jump to content
  • Advertisement
Sign in to follow this  
Sean_Seanston

Collision Detection and Movement

This topic is 2602 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Having quite a problem now with my 2D Top Down Shooter. I have the controls, movement and collision with the map implemented and working but I'm now trying to get collision between moving entities such as the player and enemies working. I'm running into trouble and confusion because of the way my movement system is implemented and I'm wondering if I did it in a reasonably good way at all or if it should really work a bit differently.

Here's what happens so far, taking the player as an example:
- Every frame the player calls move() which checks if the player's new position, after velocity is added, would collide with the map. If it would, it's position is moved to the edge of the map tile, if not, its velocity is simply added to its old position.
- When the user presses the movement keys, W for up for example, the appropriate value is added to the player's velocity. So for W that looks like "yvel += speed". When the key is released, that value is subtracted like so: "yvel -= speed".

There are clearly some problems with that...
- I never liked the idea of pressing/releasing simply adding and subtracting a value. For one, it acts all wrongly if at some other point the player's velocity is set to 0 (if you want to do that after it hits a wall for example, which I currently don't do), then you let go of the forward key... the player of course starts going in the opposite direction. Not what I wanted. On the other hand, setting it simply to 0 might not be ideal either. Maybe something else could have added to its velocity in the meantime and now releasing the key is undoing that too? I can't figure out a way of only "canceling" the velocity of pressing the key. Is it a terrible idea to have it based on key presses and releases in the first place? Should I instead be constantly polling to see if a key is down instead of using key events?
- Currently, the map collision code is inherently linked with the code for adding velocity. That's a bad idea because I need 2 different kinds of collision detection now. I tried moving the "xPos += xvel" code after the collision detection and having the collision detection move the player to the edge of the tiles etc. and setting the velocity to 0 if there's a collision but that doesn't work right because of how the input works. One effect of that is that you hold down, hit a wall, then keep down held and move along to the right where there's a clear passage down... you have to release and press the down key again to go down because its "influence" on the velocity was nullified when the velocity was set 0 after the map collision occurred.

What would be a better way of doing things? Am I very close and just need a few changes, or is there something inherently very wrong? Is polling for a key being down necessary? Actually, I do keep track of the direction of travel (changed by pressing a movement key) independent of velocity so perhaps the answer lies there. Perhaps the keys should decide direction of travel and only that, then the velocity can be derived from direction of travel but also from collisions etc? Hmm... that might solve some of the problem and I'll have to explore that in the meantime.

Thoughts?

Share this post


Link to post
Share on other sites
Advertisement
I managed to poke around and I think I've solved those problems.

Now I've a more annoying collision problem.

Say the player is behind a moving enemy. If the player rams into the enemy from behind, the enemy teleports to the other side of the player.

This happens because as I have it now, my collision code looks something like this:

if( collision )
if( moving right )
{
//Reposition to edge
xPos = left edge of other entity;

xvel = 0; //Set velocity to 0
}


Makes sense. But, how should I reorganize my collision code? Clearly, it's not enough that if an entity is moving right and is involved in a collision with another entity that it moves to the left side of that other entity.

I suppose I could ensure that it only moves to the left side if the entity is to the left of other one and so on. Is that a good way of doing it? I just don't want to find that whatever way I solve this ends up being inadequate and causes headaches down the road.

Share this post


Link to post
Share on other sites
Instead of: if(moving right)

Do: if(moving right and this object's right edge > colliding object's left edge)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!