Archived

This topic is now archived and is closed to further replies.

RavNaz

2d scroller collision detection

Recommended Posts

any ideas?? I''ve got a 2d scroller up and running. Now for collision detection with scenery I''m gonna specify that tile numbers under say 128 are all collidable, so when wandering around the map I just check the PlayChar surrounding location for these tiles and prevent movement in that direction. Now, problem is that on the map I have the PlayChar moving about, and when it reaches a set of predetermined coordinates say >= ScreenWidth /2 then I start scrolling the map. Problem is that this plays hell with the routine that calculuates the player position, as the playerchar coords are remaining the same in certain parts and when indexing the map to check for colidable tiles these will be incorrect. any help??

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sounds to me you should use logical and physical coordinates for your chars. handle all the spritepositions (logic) independent from the display, only then get going on the display.

as for colliding with the tiles. i had loads of problems with that when i coded a platform game once. first i moved the sprite back to it''s original position: it worked but looked silly. Afterwards I decided to clip and this looked quite good, only a bit trickier to code.

hope i understood correctly?
earx

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
first i moved the sprite back to it''s original position: it worked but looked silly. Afterwards I decided to clip and this looked quite good, only a bit trickier to code.

hope i understood correctly?
earx



Sounds to me like you do the colission detection too late. the trick is to check if the next move will make you collide BEFORE you actually move the sprite on screen, so that if moving one more pixel would collide the player sprite with an unwalkable tile, the sprite just doesn''t move at all. uhm... I think.

Share this post


Link to post
Share on other sites
I would say the same thing. Write a function, bool Collision(), in which it checks whether the object collides and returns the bool value true if it collides and false if it doesn't. Then you make:

bool A=Collision();
if (!A) Move();

Very simple. Of course, if that isn't the problem, I feel stupid.

--------------------------------------
I am the master of stories.....
If only I could just write them down...

[edited by - Nathaniel Hammen on November 6, 2002 9:20:18 PM]

Share this post


Link to post
Share on other sites
I agree with our anonymous poster. You need to store the position of the player on the map NOT on the screen. If you say that the top left corner of the map is the origin and store the player''s position relative to that origin (rather than the top-left corner of the screen) then all of your problems with finding which tiles the player has run into become solved.

In order to work out where to draw the player on screen you simply have to subract the scroll offset from the players position. For example:

The player is at position (500,900) relative to the top left corner of the map.
The map has been scrolled 200 pixels left and 700 pixels down.
Therefore the position of the player on the screen is 300,200.

Alternately you can lock the scroll offset of the screen to the players position (so the player is always in the middle of the screen), in this case the scroll offset is equal to the players position - half the screen size.

Share this post


Link to post
Share on other sites