# Noob collision detection question

This topic is 2550 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello people,

I'm trying to create a mario clone, just to get some game dev practice, but i'm having problems with the collision detection.
what i wanna do is check if the player collided with a piece of ground.
i'm doing it like this:
i have the Postion, and the speed in both directions, than i calculate the new position by doing

last_position=position
position.X=Position.X+speed.X*elapsedtime
Position.Y=Postion.Y+speed.Y*elapsedtime.

so now i have the new position and the last position, and i wanna check if there was an object between the two.
but now i don't know how to do that:p
because there's ofcourse a gap between those two positions, and i dont know how to get al the positions in the gap.

example:
last_position=(0,0)
position=(32,26)

object on (6,2)
so how do i figure out wheter or not i went through this coor?

i already have a Path.update function, wich takes 2 coor, and TRIES:p to return all the coors of the traveled path, but, it isn't correct:p, sometime it skips a certain coor due to rounding etc.
so how should i do this?

hope i made my problem clear,
Thx

##### Share on other sites
I'm new to developing myself as well so i'm not sure this is the best way to go about this, but i would do what you are asking using vector math, especially collisions. You have two points in a coordinate system, which basically create a line segment. You can check if this line segment collides with geometric shapes, such as lines, boxes or spheres.

Since this is a mario clone you are talking about, i'll just assume the object you are colliding with is a box. In that case, and example of how it can be done is easily found on google, like this one Line Segment vs Box. You can get the intersection point there too, and use it as the new position so that your avatar can land on the ground.

##### Share on other sites

I'm new to developing myself as well so i'm not sure this is the best way to go about this, but i would do what you are asking using vector math, especially collisions. You have two points in a coordinate system, which basically create a line segment. You can check if this line segment collides with geometric shapes, such as lines, boxes or spheres.

Since this is a mario clone you are talking about, i'll just assume the object you are colliding with is a box. In that case, and example of how it can be done is easily found on google, like this one Line Segment vs Box. You can get the intersection point there too, and use it as the new position so that your avatar can land on the ground.

oke Thanks,
i knew i had to do this line box intersection, but i can't figure out how to do it since the line is basicly 'analog' and the pixels are digital, so checking if are certain pixel is on the line doesn't make sense, since the chance that that pixel is exacly on the line is quite small, if you understand what i mean.
but ill check that site i see if it works, thx

##### Share on other sites
What you can also do since this is a 2D game, (from the sounds of it) is as follows..... Please be advised that this is SIMPLIFIED problem, just to give you a general idea

Lets say your characters jumping position (for simplicity purposes) the characters position is represented by PosX, PosY, and there is one Object @ ObjX, ObjY
Both the character and the Object are rectangle so we can detect if one Rectangle Collided with another using a Function we will name

RectCollide(Rect1, Rect2);
RectCollide Returns true if the 2 Rectangles we supplied make contact with eachother.

Lets say we also have an Inverse function that tells us our Characters Position & Rectangle at a specific X or Y coordinate according to our trajectory,

so Lets say we want to get our characters total position at X=50, then we do GetCharPos_AtX(50) or if we want to supply the Y position at lets say 90 we do
GetCharPos_AtY(90), you get the picture

Now since we know the Objects Position, and the trajectory of the Character The most effecient way to do this is Check the 4 Boundarie Conditions of The Obj which is are

X=ObjX & ObjX + ObjWid
Y=ObjY & ObjY+ ObjHei;

now we just get our heros Rectangle at those 4 boundarie conditions using our inverse functionand check if the Returned Rectangle Collided with the object, if it dead we know that The character might slam into the sides of the wall at X = ObjX or X = ObjX + ObjWid

Or that the character will either Land Ontop of the object at ObjY + ObjHei, Or he will slam his head at ObjY;

puesdo code fallows

HeroSlammedLeft = GetCharPos_AtX(ObjX);
HeroSlammedRight = GetCharPos_AtX(ObjX + ObjWid);
HeroLandedTop = GetCharPos_AtY(ObjY + ObjHei);

if(RectCollide(HeroLandedTop, ObjRectangle))
{
// he landed safely on top
}
{
// Pop a coin out of that box!
}

I hope you foudn this post helpful and not confusing lol, good luck to you on your gamedev adventure.

1. 1
Rutin
26
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 10
• 10
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002086
×