Vectors & Collision
I have made a short code which should detect if a player collides with an object. It looks like this :
------CODE--------
#define VECTOR_KPL 4
int p0x,p0y,p1x,p1y,s1x,s1y,p2x,p2y,p3x,p3y,s2x,s2y;
typedef struct vektori_typ
{
int x,y;
} vektori, *vektori_ptr;
vektori vektorit[VECTOR_KPL] = {
{ 10,100},
{ 10,200},
{ 200, 200},
{ 200, 100},};
void Collision()
{
p0x=Player.x;
p0y=Player.y;
p1x=Player.x+Player.xv;
p1y=Player.y+Player.yv;
s1x=p1x-p0x;
s1y=p1y-p0y;
for (int index=0;index < VECTOR_KPL; index++)
{
p2x=vektorit[index].x;
p2y=vektorit[index].y;
p3x=vektorit[index+1].x;
p3x=vektorit[index+1].y;
s2x=p3x-p2x;
s2y=p3y-p2y;
s=(s1y*(p0x-p2x) + s1x*(p0y-p2y))/(s2x*s1y+s1x*s2y);
t= (s2x*(p0y-p2y) - s2y*(p0x-p2x))/(s2x*s1y+s1x*s2y);
if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
{
Player.Collision = 1;
}
else
{
Player.Collision = 0;
}
}
}
------CODE--------
I think the problem is when the code calculates ''t'' and ''s''. The code is taken from Andre Lamothes book, "trick of the windows game programming gurus".
Tino Torro a.k.a. Abaddon
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement