collision detection
hi!
i have a probelm with the following code:
//move object a x-
if (ay<=0.00f || ay>=4.00f || ax>4.00f || ax<0.01f)
{
if (keys[''A''] && ax>=-5.00f && !keys[''W''] && !keys[''S''])
{
ax-=0.02f;
}
}
//move object a x+
if (ay<=0.00f || ay>=4.00f || ax>3.99f || ax<0.00f)
{
if (keys[''D''] && ax<=5.00f && !keys[''W''] && !keys[''S''])
{
ax+=0.02f;
}
}
//move object a y+
if (ax<=0.00f || ax >4.00f || ax==4.00f || ay>3.99f || ay<0.00f)
{
if (keys[''W''] && ay<=5.00f && !keys[''A''] && !keys[''D''])
{
ay+=0.02f;
}
}
//move object a y-
if (ax<=0.00f || ax >=4.00f || ay>4.00f || ay<0.01f)
{
if (keys[''S''] && ay>=-5.00f && !keys[''A''] && !keys[''D''])
{
ay-=0.02f;
}
}
it is supposed to be something like a very simple collision detection.
Sometimes it works.
But sometimes i get the ax values 0.02f or 3.98f if ay is !>4.00f or !<0.00f
or the ay values 0.02f or 3.98f if ax is !>4.00f or !<0.00f.
does someone know my mistake or a better way to do that?
I would simplify things and break things down a lot more to make the code easier to read. Even if your code worked fine, it's scary and hard to read what's going on at first glance.
Here's what I would do:
ThomW
www.LMNOpc.com
Edited by - thomw on December 16, 2000 2:32:29 PM
Here's what I would do:
void HandleKeys (){ // Make sure only on key is pressed at a time. If // more than one key is pressed, exit this routine. // if (!(keys['A'] ^ keys['D'] ^ keys['W'] ^ keys['S'])) return; // Change ax and ay based on the key that is pressed // if (keys['A']) { ax -= 0.02f; } if (keys['D']) { ax += 0.02f; } if (keys['W']) { ay += 0.02f; } if (keys['S']) { ay += 0.02f; } // Check that ax and ay are within bounds // if (ax > 5.0f) { ax = 5.0f; } else if (ax < -5.0f) { ax = -5.0f; } if (ay > 5.0f) { ay = 5.0f; } else if (ay < -5.0f) { ay = -5.0f; } return; }
ThomW
www.LMNOpc.com
Edited by - thomw on December 16, 2000 2:32:29 PM
ThomWwww.LMNOpc.com
thx for your tip!
i just built the funktion using your code and it works. it is really easier to read!
i think i can solve my prob that sometimes the the cube i control with ax and ay moves into the other ( 1,1 ;1,3 ; 3,1 ;3,3) by using the code you use for ax!<5 ...
a last question:
how to get the code on the message board into such a white box?
i just built the funktion using your code and it works. it is really easier to read!
i think i can solve my prob that sometimes the the cube i control with ax and ay moves into the other ( 1,1 ;1,3 ; 3,1 ;3,3) by using the code you use for ax!<5 ...
a last question:
how to get the code on the message board into such a white box?
Nika -
I'm glad the code worked for you, though I just spotted a bug. The 'S' key increases the value of ay instead of decreasing it. The 'S' key code should read:
I used the [ source] [ /source] tags (no spaces) to create the white box.
More information about the message boards can be found here:
http://www.gamedev.net/community/forums/faq.asp
ThomW
www.LMNOpc.com
Edited by - ThomW on December 17, 2000 8:52:07 AM
I'm glad the code worked for you, though I just spotted a bug. The 'S' key increases the value of ay instead of decreasing it. The 'S' key code should read:
if (keys['S']) { ay -= 0.02f; }
I used the [ source] [ /source] tags (no spaces) to create the white box.
More information about the message boards can be found here:
http://www.gamedev.net/community/forums/faq.asp
ThomW
www.LMNOpc.com
Edited by - ThomW on December 17, 2000 8:52:07 AM
ThomWwww.LMNOpc.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement