Edit 2
In the following picture sprite1 (the red square) is hitting the platform from the left, so:
sprite1_rightEdge>platformSprite leftEdge
would be true, so I can simply reposition sprite1 to align it with the left edge of the platform. So....
sprite1X=platformSpriteX-platformSpriteWidth
This is fine, however, lets say that sprite1 hits from the right, the problem I have here is that the first condition (above) is still true. So....
sprite1_rightEdge>platformSprite leftEdge
So my sprite1 still gets positioned at the left edge of the platform......
The same also happens in reverse....
Using velocity
I've had some success with velocity, but again I'm not clear on how this works properly.
For example, if I say something like:
if (Sprite1 moving right at time of collision)
Then I can assume it's hit the platform from the left. This works, but what if the sprite is 'falling' from the top and lands on the platform while also moving right? It will again get re-positioned at the left edge of the platform.
partial sucess
So far, I managed to get this working (although not fully implemented yet), by saving the old position of Sprite1, repositioning it and checking the old position, so if the old position was to the left of the platform, then I can assume it came in from the left.
If someone could let me know the best way to proceed it would be much appreciated.
Just to clarify what I'm asking - I have 2 objects - 1 is a platform and the other is the main character, I wish to make the player stop regardless of which side he hits the platform (from the top, bottom, left or right) - I will definitely be moving at distances of more than 1 px at a time).
I've attempted using velocity but this doesn't seem to work correctly as well as double checking x/y positions once a collision has happened but again I get some very off results. Some pseudo code of how to detect which side a sprite has hit would be much appreciated :-)
I have researched this and have seen pretty much every Q&A on this site regarding this subject (as far as I know) but the answers are either not relevant to my particular query or I simply don't understand them.
Original question
I understand the basic principles of collision detection for squares / rectangles
i.e.,
(Pseudo code)
public boolean testCollision(){
if (bottom of sprite1<top of sprite2)
return false;
if(top of sprite1>bottom of sprite2)
return false;
if(left edge of sprite1 > right edge of sprite2)
return false;
if(right edge of sprite1 < left edge of sprite2)
return false;
return true;
}
I also know that I can stop my sprite going off the edge of the screen like so:
if(spriteX<0){spriteX=0};
However using something akin to the first method above for detecting collision between 2 specific objects, how can I make my 1st sprite actually stop so he can't pass through it?
At the moment, although I can detect when the two are touching, I'm unsure how to make it so that they can't actually pass through each other.
A push in the right direction would be a great help - thanks!