Advertisement

How to detect what side of an object you collide with.

Started by April 12, 2001 10:45 AM
11 comments, last by IronFist 23 years, 9 months ago
I have been trying to get my collision detection to work, but I''m having problems. I can detect when they collide just fine, but I don''t know how to determine which side of the player collided with. It is a side-view game (kind of like Mario). I want to be able to tell when the player is touching one of the sides, the top, or the bottom of the block. Do you have any ideas of how to solve this problem? Thanks.
Play Sumasshu, which is a game I programmed. CLICK HERE
What would think is that take a point on the player..and a point on your other object and figure out what the degree is to that object so if your player coords are 0,0 and your object is at 0,1 .. you know its 45 degrees from the player.

now whether that object is moving away from the player or toward the player is up to you how to handle that ... but still you can determine what direction the object is relitive to the player by doing that...

''...but thats just my opinion...I could be wrong''

Jenison





Advertisement
Not sure if this is possible, but could you define the character to have width and if any part of the character collided with anything it would tell.

"There is humor in everything depending on which prespective you look from."
"There is humor in everything depending on which prespective you look from."
Jenison, do you know how I could do that? I need equations and stuff, or a nice tutorial.

Edited by - IronFist on April 13, 2001 2:36:42 PM
Play Sumasshu, which is a game I programmed. CLICK HERE
Maybe I'm not understanding the question, but if its just a 2D game, and you can tell when a collion happens, all you have to do is look at the objects positions. If obj1 is above obj2 ie obj1.y < obj2.y (depending on the coordinate system you use, this is in screen coordinates) then obj1's bottom collided with obj2's top. if obj1.x < obj2.x, obj1 is to the left of obj2, so obj1's right side collided with obj2's left side. Then just expand this.

Rastagon 3

Edited by - Domini on April 13, 2001 2:38:04 PM
Domini, that is how I have it set up now, but I keep on running into problems. I was just seeing if there was a better way(hoping there was a better way ), because the way I''m doing it is just giving me a headache. I will probably just keep doing it the same way, but I need to figure out how to fix some of the problems it''s giving me. Does anyone have any open source game or something that uses this kind of 2d collision detection?

The problem I''m having is when there are two blocks, one on top of the other, you can stand on the side of the bottom block (because it thinks you''re on the floor, when you''re really on the wall). And you can hit your head on the bottom of the top block.

Click here to see a pic of what I''m talking about:
http://www.geocities.com/serratedangel/help.jpg

Anyways, I''m off to go play around with my code some more . If anyone has any ideas, feel free to post them.
Advertisement
Back to the top one last time.
Play Sumasshu, which is a game I programmed. CLICK HERE
I''ve solved this problem before, and the solution is quite logic. Even if you treat your character as a single point this collision method works... Firstly, you need to use a little bit of physics with your objects... I HIGHLY recommend that you store your object information in the following manner.

gameobject {

xPos, yPos; // coordinates in world space
xVel, yVel; // velocity object is travelling.

}

Every game look you update your character''s x and y positions by their respective velocities.

Now, the collision part comes in.

When you update your x pos you check to see if your new position puts you inside of a tile.. you do this by taking your x and y position and using it to find out which tile you are in in the tile map... Let''s say the tiles are each 16 x 16 pixels big... you simply divide x and y by 16 and you''ll get which tile you lie within.

How to tell which side to reset mario to?
It''s actually rather simple... You use your velocity... Logic dictates that if you are hitting something on its left side in a 2D world you MUST be travelling to the right... So, if during the collision your velocity is positive, you must be re-positioned just outside the left of the tile... Same goes for the opposite direciton, and for the vertical axis...

Anyways... I''m writing this in a hurry... I hope this helps!

Daniel Piron
Jenison, there is a problem with that though. If I am moving right on the screen and I'm on the top of a tile, it will "warp" me over to the left side of the tile instead of just putting me on top of the tile again. And also, what about moving blocks? That wouldn't work for that either. The way you suggested would work if it was an overhead view game, but my game is a side view game with gravity constantly pushing down on the character.

Edited by - IronFist on April 16, 2001 6:28:27 PM
Play Sumasshu, which is a game I programmed. CLICK HERE
Oops. Double Post.

Edited by - IronFist on April 16, 2001 6:24:08 PM
Play Sumasshu, which is a game I programmed. CLICK HERE

This topic is closed to new replies.

Advertisement