Im currently developing a 2d platformer in my spare time and having some issues with object collision along the X axis. My collion techniques work but the feedback can be quite erroneous. Im stuck trying to find a logical way to get around it. For example...
if(dbSpriteCollision(plr1, a) == 1)
{
if(iDir == right)
{
xPos = dbSpriteX(a) - dbSpriteWidth(plr1);
velx = 0;
dbText(0, 15, "Vertical Collision Detected");
}
if(iDir == left)
{
xPos = dbSpriteX(a) + dbSpriteWidth(a);
velx = 0;
dbText(0, 15, "Vertical Collision Detected");
}
}
If the player collides with the wall, it detects the collision fine but if you want to back away from the wall it will change your iDir (direction) and imediatly alter your xPos (position along x axis) to the oposite side of the object. One way i thought to counter this was to...
xPos = dbSpriteX(a) - dbSpriteWidth(plr1) - 1;
but that will do the obvious. Any one have any other techniques for object collision along x axis?
EDIT - Found a way, instead of using 1 object as a 'wall' and testing collisions from both angles, make 1 wall for left collisions and 1 for right collisions.
//TEST X COLLISION --->|
for(int a = wall_start_l; a < wall_end_l; a++)
{
if(dbSpriteCollision(plr1, a) == 1)
{
if(iDir == right)
{
xPos = dbSpriteX(a) - dbSpriteWidth(plr1);
velx = 0;
dbText(0, 15, "Vertical Collision Detected");
}
}
}
//TEST X COLLISION |<---
for(int a = wall_start_r; a < wall_end_r; a++)
{
if(dbSpriteCollision(plr1, a) == 1)
{
if(iDir == left)
{
xPos = dbSpriteX(a) + dbSpriteWidth(a);
velx = 0;
dbText(0, 15, "Vertical Collision Detected");
}
}
}
For platforms, make 2 images, 1 for the platform its self, 200 x 20, and another 1 x 20. Place the first image at say x100 y200, then place the second image twice @ x98 y200 and x301 y200 and test 2 seperate dbSpriteCollision, one for the left wall and one for the right wall to avoid the flaws that i got on my first attempt. If any one has any other methods feel free to share and if any one is having issues with thiers let me know and i will try my best to assist.
-Stephen