Public Group

# What exactly does this code do?

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

## Recommended Posts

Hello all. Got this code here a couple of weeks back. It's extremely useful code for causing a basic collision between two ellipses in C#. But i'm having trouble understanding it. puck_x = x_position of an ellipse (a puck) puck_y = y_position of an ellipse (a puck) paddleForce = speed you wish to perform the collision at x_vel and y_vel is the x and y velocities of the puck Can anyone explain exactly how it does this from the code?
private void Player1Collision(float paddleForceP1)
{
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 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));
}


[Edited by - squiller on April 1, 2005 8:26:39 PM]

##### Share on other sites
Seems perfectly clear to me... At a glance, at least. Just basic C# syntax...
Maybe it would be better if you'd asked a direct question. What line are you having problems with?

##### Share on other sites
I'm just not too sure how it works out the and and y velocities of the ellipse by simply recieving the force.

I'm puzzled my most of it but the lines i'm most puzzled by are:

if(p1Distance <= 2.0f * (paddleWidth / 2)){	p1NormalX /= p1Distance;	p1NormalY /= p1Distance;	x_vel = (p1NormalX - -p1NormalY) * paddleForceP1;	y_vel = (p1NormalY - p1NormalX) * paddleForceP1;}

What is 2.0f? Why are you dividing by p1NormalX by p1Distance? And how does it get the x_vel and y_vel?

And also:

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));}

What does the Abs function do? Why do we need to check if distance > 1, 2 or 4?

##### Share on other sites
Quote:
 What does the Abs function do?

Absolute value.

##### Share on other sites
Quote:
 Original post by squillerWhy do we need to check if distance > 1, 2 or 4?
>> and << are the bit-shift right and bit-shift left operators, not comparision operators. In this case, they're shifting the distance right 1 bit, then right 2 bits, then right 4 bits.

##### Share on other sites
To answer a few of your questions... In order to get the normal of a vector you must divide each component of the vector by the vector length. If you then calculate the length of the normal it will have the same direction as before but now it will have a length of 1. This is meant to save time calculating things like angles if I remember correctly.

2nd the >> and << operators are bitshifts in this case and not less than and greater than operators like you seemed to imply. It operates like a division by 2 so you could rewrite it like:

(X + Y - (Distance / 2) - (Distance / 4) + (Distance / 16)

and it would likely work the same. I don't really know how it works to be honest, my math is a bit rusty. If I had to guess I'd say it's some type of expansion using calculus to quickly approximate the distance of the points.

And abs almost certainly takes the absolute value of the variable. Hope this helps.

##### Share on other sites
Thanks everyone!!

You've made it a whole lot clearer. I'm a complete newbie to game programming, and decided to undertake a 3D Air Hockey game in C#. Probably wasn't the best idea ever but i'm getting there!

Anyways, thanks again - it's much appreciated!

##### Share on other sites
...I must be dumber than a log, but it still seems clear to me... Oh, well :(

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 31
• 16
• 11
• 10
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015617
×