Sign in to follow this  
squiller

Convert from C into C#?

Recommended Posts

Apologies.. this was in a thread i already made but thought it would get more views out here in a new thread. I'm developing an Air Hockey game in C# and i'm trying to simulate a collision between two ellipses.. the paddle which the user is controlling with the mouse (e.X, e.Y) and also a puck which should move it the paddle collides with it. I have the collision detection working correctly. I think i got an example using vectors on how to simulate a collision between these two ellipses. Also, i understand how to get the velocity of the puck? It's specified already in my source code (x_vel, y_vel) how would i get the velocity of the paddle the user is moving with the mouse? The source code i found is unfortunately in C.. can anyone convert this to C# as can't find ways to implement DotProd or Normal in C#. Many thanks in advance. Squiller.
void collision (void)
{
// ball movement (Yes?)

x1 += xVect1;
y1 += yVect1;

x2 += xVect2;
y2 += yVect2;

// test for collision

if (distance > radius1 + radius2)
{
return;
}

// if we get here, the balls have collided,
// so we do the collision calculations

xVelocity = xVect2 - xVect1;
yVelocity = yVect2 - yVect1;

// Normalize
Normal (x1 - x2, y1 - y2, normal);

approach = DotProd(normal[0], normal[1], xVelocity, yVelocity);

// calculate velocity change

xNewVect = normal[0] * approach;
yNewVect = normal[1] * approach;

// change velocities

xVect1 += xNewVect;
yVect1 += yNewVect;

xVect2 -= xNewVect;
yVect2 -= yNewVect;
}

Share this post


Link to post
Share on other sites
isnt that same with c#?
i have done some homeworks of the school (easy ones) in c#, and it seems to me the same

maybe the only thing to change would be this:
void collision (void)
to
public void collision()

and when you callthe 2 methods (dotproduct and normal) , well just create an instance of the class.
class c
{
// something first
c instance = new instance();
instance.normal(xxxxx);
intance.dotproduct(xxxx);
// something then...
}

Share this post


Link to post
Share on other sites
Use static functions.


class Whatever {

public static double DotProduct( double a, double b, double c, double d ) {

return a*c + b*d;
}
}

// later...

double x = Whatever::DotProduct( foo, bar, baz, foobar );




An excellent way would also be to implement a vector class (since vectors can be easily represented by objects), although I'm almost sure there's a 2D vector C# class laying around somewhere.

Share this post


Link to post
Share on other sites
To answer your other question, in your simplistic simulation the velocity of an object is the distance it moves between two updates. To find the velocity of the paddle, substract its previous position from the current position.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
... Whatever::DotProduct( foo, bar, baz, foobar ); ...

Make that Whatever.DotProduct( foo, bar, baz, foobar );. In C# they changed the namespace scope specification punctuator from '::' to '.'. Sometimes I make the opposite mistake when I write in C++. [wink]

Share this post


Link to post
Share on other sites
How's the development of the C# compiler going for Linux? I've stayed away from C# because I want to be as "platform-independant" as possible... and it seems I can stick with that with either Java or C++ (haven't learned Java enough yet, so C++ it is!).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this