SDL Collision wont let me collide and move another direction

Started by
2 comments, last by BeerNutts 9 years, 2 months ago

So, I have a collision with a wall but theres a problem, if I hold the direction of collision and another direction, it wont move.

heres the code:


void onKeyPressed(){

for (int i = 0; i < NBR_PLATFORMS; i++)
    {
	if (players[0].m_spriteImage.bb_collision(platform[i].sprite))
	{
	    players[0].m_spriteImage.set_world_position(oldX, oldY);
        }
    } 

}

void characterCollision()
{
	for (int i = 0; i < NBR_PLATFORMS; i++)
	{
		if (players[0].m_spriteImage.bb_collision(platform[i].sprite))
		{
			players[0].m_spriteImage.set_world_position(oldX, oldY);
		}
	}
}

Any help would be greatful

Advertisement

Perhaps a dense question on my part, but when you say "press the direction of collision and another direction", do you mean simultaneously? Your code looks like it resets the position back to the previous iteration. If you're processing the collision key after the other direction, it doesn't look like it would move, or if you're checking collision after your movement functions. Either way, pressing the collision direction key is going to reset your position if that is returned true and is after any movement functions. Or maybe I'm reading your code incorrectly.

Beginner here <- please take any opinions with grain of salt

Looks like you're responding to collisions by disallowing motion to take place, so any time you're making a collision happen you should lose the ability to move. You don't really want to drive collision response that way. It's better to use input to apply forces to the entity and do some lightweight physics. That is, the entity has a position and a velocity; every update the position has the velocity added to it. The input influences the velocity rather than the position. This gives you access to smoother control and gets you away from the input vs collision problem.

When colliding with 'hard' objects, such as immovable walls, instead of just undo-ing the movement for the frame, determine the voronoi region of interception and change the position to resolve the collision. This will allow you to slide along walls gracefully, etc. This must be done simultaneously for all objects that the entity is colliding with, so...

* move object to new position according to velocity

* create a 'correction' vector and initialize it to 0,0

* for each 'hard' collidable object

** determine the depth of collision along the separating axis

** reverse the penetration vector to get a correction vector for that specific collision

** add it to the overall correction vector

* apply the correction vector directly to the entity's position

In order to detect the separating axis you'll want to use voronoi tesselation. If your walls are orthogonal (square) and axis-aligned then this is quite simple:

012

345

678

4 is 'inside' the wall, the other numbers represent individual voronoi regions for the wall. You can determine the region your entity is in by testing its centerpoint against the edges of the wall:

int region = 0;

if(center.x > wall.left) {region += 1;}

if(center.x > wall.right) {region += 1;}

if(center.y > wall.top) {region += 3;}

if(center.y > wall.bottom) {region += 3;}

In regions 0,2,6, and 8, push directly away from the matching vertex. In region 4 push away from the center of the wall. In the other regions just push in the direction that they represent. For instance, in region 1 you want to push straight upward, etc.

If your walls are not orthogonal and axis-aligned then you'll need to work out the tesselation differently before you can use SAT testing and response. If that's the case just let us know and I'll try to explain the technique. Or if any of this doesn't make sense, I can explain it in more depth.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

A simpler solution more befitting beginners would be to simply move along each axis and check the collision for each axis.

Meaning, moving along the X-axis, check collision. If colliding, then you can move the object back to where it was on the X-axis. then move it along the Y-axis, and check collision. If colliding, move it back.

This will allow your player to run into a wall, but still move along one of the axis'.

Not that what Khatharr is incorrect, this solution is just much easier to implement and understand as a beginner. Eventually, I'd suggest looking into a 2d physics library, but that can come later.

Good luck and have fun.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement