• Create Account

### #Actualfarmdve

Posted 12 December 2012 - 05:17 PM

Long story short, I followed the SDL tutorial on LazyFoo here http://lazyfoo.net/S...son29/index.php and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
else if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
else if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}


player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one http://pastebin.com/mrFkLTLH but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; http://i.imgur.com/Rn7MY.png
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are on the screenshot, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

EDIT:It is fairly obvious to me, that since my collision code is inside a single if-statement, and I am indeed colliding with a wall, while trying to move up or down, either of the sub-statements always execute thus preventing me to move. No idea how to fix it though,

### #6farmdve

Posted 12 December 2012 - 04:35 PM

Long story short, I followed the SDL tutorial on LazyFoo here http://lazyfoo.net/S...son29/index.php and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
else if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
else if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}


player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one http://pastebin.com/mrFkLTLH but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; http://i.imgur.com/Rn7MY.png
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are on the screenshot, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

EDIT:It is fairly obvious to me, that since my collision code is inside a single if-statement, and I am indeed colliding with a wall, while trying to move up or down, either of the sub-statements always execute thus preventing me to move. No idea how to fix it though,

### #5farmdve

Posted 12 December 2012 - 04:35 PM

Long story short, I followed the SDL tutorial on LazyFoo here http://lazyfoo.net/S...son29/index.php and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
else if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
else if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}


player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one http://pastebin.com/mrFkLTLH but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; http://i.imgur.com/Rn7MY.png
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

EDIT:It is fairly obvious to me, that since my collision code is inside a single if-statement, and I am indeed colliding with a wall, while trying to move up or down, either of the sub-statements always execute thus preventing me to move. No idea how to fix it though,

### #4farmdve

Posted 12 December 2012 - 04:33 PM

Long story short, I followed the SDL tutorial on LazyFoo here http://lazyfoo.net/S...son29/index.php and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
else if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
else if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}


player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one http://pastebin.com/mrFkLTLH but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; http://i.imgur.com/Rn7MY.png
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

### #3farmdve

Posted 12 December 2012 - 04:33 PM

Long story short, I followed the SDL tutorial on LazyFoo here http://lazyfoo.net/S...son29/index.php and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}


player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one http://pastebin.com/mrFkLTLH but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; http://i.imgur.com/Rn7MY.png
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

### #2farmdve

Posted 12 December 2012 - 04:26 PM

Long story short, I followed the SDL tutorial on LazyFoo here http://lazyfoo.net/S...son29/index.php and wrote the game from scratch in C(since the examples were C++), but borrowed some of the functions.

What is happening is that the collision code is working, but not entirely. If I were to hit a wall, and continue to walk towards it(and being unable to pass through it obviously) and at the same time try to move in a different direction(but not the opposite to the wall) I can't move.

This is my if/else statement

if(touches_wall(player.box))
{
if(player.box.x < player.prev_x)
{
player.box.x += GetVelocity(player, 0);
}
if(player.box.x > player.prev_x)
{
player.box.x -= GetVelocity(player, 0);
}

if(player.box.y > player.prev_y)
{
player.box.y -= GetVelocity(player, 1);
}
else if(player.box.y < player.prev_y)
{
player.box.y += GetVelocity(player, 1);
}
}


player.box.x and player.box.y are the current positions of the player on the X and Y axis respectively. player.prev_x and player.prev_y are the previous positions.
GetVelocity pretty much returns some static hardcoded values of the velocity for each axis(depending on the second argument being 0 or 1).

My previous collision handling code was this huge one http://pastebin.com/mrFkLTLH but while it worked the way I wanted it allowed under some conditions the object to pass through the wall near the corners.

Here's a picture of a more detailed explanation; http://i.imgur.com/Rn7MY.png
Where the arrows are, I basically make the object go left, let it collide and then either go up or down, while still going left, but it will stay still and won't move either way.
With my previous large code that I just pasted on pastebin, where the small blue lines are, that's where the object passed through the wall on some conditions.

So I basically need a solution on how to fix this. I've been working on that since last night, to the point where I wrote the same code over and over again, expecting a different result.

PARTNERS