Page 1 of 1

Movement and Collision: how do you do both?

Posted: Fri Feb 01, 2013 4:35 am
by sanjiv
What's the best way to do this? What's the basic concept?

a) Move from position A to position B IF AND ONLY IF position B isn't a collision(with an impenetrable object)?
b) Move from position A to position B, and if position B is a collision, then re-adjust?
c) something else?

QUESTION 2: should a movable agent
a) have the same movement function applied to him at all times?
b) have various states, and have different movement functions for those various states? I.e. if it's in the air, on the ground, clinging to a wall, etc?

What's worked for you in the past? Ultimately I want to start collaborating, and I figure there are "best practices" out there.

Re: Movement and Collision: how do you do both?

Posted: Fri Feb 01, 2013 6:44 am
by substitute541
Question 1 :
The simplest, though not extremely accurate, but fine for most cases, is (b).

Question 2 :
Depends. I will go with (a), for simple games, but for complex platformers, like those with a sticky floor and walls, and a blowing wind, I will go with (b).

Re: Movement and Collision: how do you do both?

Posted: Fri Feb 01, 2013 7:59 am
by Inny
B. The basic idea is that you take the bounding rect of the player, and the movement vector, and keep trying to apply the X component to that base bounding rect, to see what the rect collides with. If it collides, then subtract from the X component how much the collision overlap was. Then reapply for the next one. It's considered a valid movement when everything has been accounted for (and you use some kind of Broadphase step to pare down how many other things need to be analyzed). Then you do it again for the Y component. The reason you separate them is to prevent things like unintentional sticking to walls and catching on internal borders between objects. The tell tale sign of the X and Y components being separated during collision detection is that you can "slide" along a wall as you move towards it.

As for different movement styles between ground and air, that's a Game Mechanic, meaning that your game is what you decide here. Some games use the same control scheme for both (Megaman, for instance) where as others will change your motion between various modes (Castlevania).