# Inaccurate collision taking place..?

## Recommended Posts

squiller    110
Hello all, Been working on this Air Hockey game for quite a while now and as a relative newbie, i'm still not sure what's wrong with the following code. But it's basically a check to see if the puck has intersected (collided) with the paddle and performs an appropriate collision and bounce backwards. But it doesn't seem to be exactly the most accurate collision! To see what i mean, download the executable here: http://homepage.eircom.net/~basquilletj/AirHockey.rar (1.2 MB) Any comments on where exactly the accuracy suffers? Any others comments on the game welcome! Many thanks. Squiller EDIT: Can't seem to run the executable without the RawMouse componant so i've updated the link to the actual project in a RAR file.
[source lang=c#]
private int Min(float A, float B)
{
if(A < B)
{
int newA = Convert.ToInt32(A + 0.5);
return newA;
}
else
{
int newB = Convert.ToInt32(B + 0.5);
return newB;
}
}

private float Distance2D(float X, float Y)
{
X = Math.Abs(X);
Y = Math.Abs(Y);
int Distance = Min(X, Y);
return (X + Y - (Distance >> 1) - (Distance >> 2) + (Distance >> 4));
}

{
float p1NormalX = puck_x - paddle1_x;
float p1NormalY = puck_y - paddle1_y;

float p1Distance = Distance2D(p1NormalX, p1NormalY);
if(p1Distance <= 2.0f * (paddleWidth / 2))
{
p1NormalX /= p1Distance;
p1NormalY /= p1Distance;

x_vel = (p1NormalX - -p1NormalY) * paddleForceP1;
y_vel = (p1NormalY - p1NormalX) * paddleForceP1;
}
}

private void CheckPlayer1HitPuck()
{
{
player1PuckHit = true;
}

if(player1PuckHit == true)
{
Player1Collision(10);
}

player1PuckHit = false;
}



##### Share on other sites
DirectXFreak    166
It seems to me that the problem is that you're only using one step per frame. Try subdividing the timestep and making more tests per frame, perhaps 30 or so, maybe that will help.

##### Share on other sites
squiller    110
Quote:
 Original post by DirectXFreakIt seems to me that the problem is that you're only using one step per frame. Try subdividing the timestep and making more tests per frame, perhaps 30 or so, maybe that will help.

Thanks for the responsem DirectXFreak.

Not too sure what you mean.. can you clarify it a bit more please?

[Edited by - squiller on March 27, 2005 10:09:23 PM]

##### Share on other sites
leiavoia    960
He means that your timeslices are too thick. basically, you're not checking for collisions often enough, which can cause weirdness. You can try "subdiving" the time or you can try a swept volume test. For airhockey, i would definately go with swept volume tests because they are as precise as your data types are. Time steps and frame rates are not an issue. Google for "swept spheres" or "swept volumes"

##### Share on other sites
squiller    110
Ah right!

Yeah, i presumed that was why it was inaccurate alright.

Any chance someone has a link to an example with using these Sphere Volumes tests?

Cos all i can find when i google it is an endless numbers of long documents / theories based on it.

I really need to see an example to see it's effect.

Thanks again!

##### Share on other sites
squiller    110
Quote:
 Original post by leiavoiaYou need to register (for free) for this one, but it has code and diagrams:http://www.gamasutra.com/features/19991018/Gomez_2.htmOliii's code might be of some use. He has examples:http://www.gamedev.net/community/forums/topic.asp?topic_id=192562

Thanks for that.. will register for the first one!

Oliii's seems to be offline.

##### Share on other sites
squiller    110
Have no clue how to do this sweep-test.

C++ seems like such a pain in the a compared to C#!

##### Share on other sites
leiavoia    960
Well, no one said you couldn't do it in C# !