# Collision problem

Hey guys,
You know that feeling that you solved a problem but you're wondering if anyone can do it better. I've got it with a collision.

Consider an object and a wall, no gravity no bouncing. The object is moving 5 pixels per frame horizontaly.

At a given point the object is let's say 3 pixels away from the wall like this:

************************||
******************oo****||
*****************oooo***||
******************oo****||
************************||

Now I want the object to stick to the wall but in my testing I only test it like this:

object.x + object.width + 5; // 5 is the speed

Should I test each pixel to the right up to 5 or first test 5 pixels to the right and then if things collide test each pixel back to 1.

• : Hey guys,
: You know that feeling that you solved a problem but you're wondering if anyone can do it better. I've got it with a collision.
:
: Consider an object and a wall, no gravity no bouncing. The object is moving 5 pixels per frame horizontaly.
:
: At a given point the object is let's say 3 pixels away from the wall like this:
:
: ************************||
: ******************oo****||
: *****************oooo***||
: ******************oo****||
: ************************||
:
: Now I want the object to stick to the wall but in my testing I only test it like this:
:
: object.x + object.width + 5; // 5 is the speed
:
: Should I test each pixel to the right up to 5 or first test 5 pixels to the right and then if things collide test each pixel back to 1.
:
:
:
[blue]So, you are testing the screen pixels? You should have the wall as an object with coordinates, so you know the coordinates of a wall, then it is simple code:[/blue][code]
int iDist2Wall;
int iBallX, iWallX;

if ((iDist2Wall = iWallX-iBallX) > 5) {
// Move the ball:
iBallX += 5;
}
else {
// Last move:
iBallX += iDist2Wall; // Ball stuck to the wall
}
[/code]