flypp3r

im pretty new to gamedev, so simulating physics is new to me too. i know the projectile motion formulas, im just having trouble applying them. is there any "tutorial" on projectile motion physics for XNA

basically, someone suggested to do "scorched earth". im trying to get the hang of just some simple projectile before i apply it to a game. i know i should basically have to just enter the initial velocity vector and the angle. then have it passed to a function that gets the initial x velocity, and initial y velocity. also, im assuming that whatever i get for y-velocity should be negative as "up" is negative in the XNA graphics. then, after each update, it should find the current change in position for x and y and add it.

ive confused the hell out of myself with the code and its a big mess, so its best to not ask me to post what i have ...however, if you insist- ill try to clean it up somewhat lol

physics and myself are not friends ive noticed over the past few months despite my great math skill



Re: XNA Game Studio Express simple projectile motion physics??

flypp3r

this is what i have so far. its sorta sloppy in the OO aspect, but ill fix that up later. im trying to figure out whats up with the math. if i put in anything BUT a 45 degree angle, it doesnt come out right. if i put in the 45 degree angle, it comes out perfectly fine and i can control the velocity

heres the code for the "bomb" class

class bomb

{

public double vel0y, vel0x; //Initial velocity of x and y

public double IA; // Initial angle

public Point pos;

// the x and y are starting coords. vel0 is the starting velocity. IA is angle

public bomb(int x, int y, double vel0, double IA)

{

//convert IA from degrees to radians

this.IA = Convert.ToInt32(IA * (Math.PI / 180));

pos = new Point(x, y);

// find the x and y velocitys using Vo*Cos(theta) and Vo*Sin(theta)

vel0x = vel0 * Math.Cos(IA);

vel0y = vel0 * Math.Sin(IA);

}

public void update()

{

// "decelerate" the y-velocity with gravity

vel0y -= 9.8;

//add the velocities to the positions

pos.X += (int)vel0x;

pos.Y -= (int)vel0y;

}

}

now all i need to do in my main program is create a new "bomb" with starting coords of lets say (100, 500) and starting velocity of about 50. as long as the starting angle is 45, when i call bomb.update(), it properly adjusts the x and y coords, and then i redraw it based on pos.X and pos.Y

however, if i change the angle to anythign else, it goes all cattywompas and decides to throw it in all kinda x-directions :(

my brain hurts from other things all day, so im probably mis-casting or have a simple math mistake... or it can be that its a huge flop and the code is all wrong and ill get embarassed.. maybe itll hit me in my sleep





Re: XNA Game Studio Express simple projectile motion physics??

AndyL

flypp3r,

if you want a flexible way of doing it, then a particle simulation would do the job. Probably a bit more complex than you really need, but would allow some cool effects later on. I'll skip the derivations as they are hard to do in plain text...

Define a particle as:

    Vector2 momentum;
    Vector2 velocity;
    Vector2 position;
    float mass;

The initial values of mass, position and velocity you know. The momentum will be: mass * velocity.

Now for each timestep you are going to apply a force (also a Vector2), so do the following in this order:

    position += velocity*timeStep;
    momentum += force*timeStep;
    velocity = momentum / mass;

Thats it! The particle should have moved through space.. At 60fs the timestep is going to be around 0.016 sec - you will find the larger the timestep the more inaccurate the motion of the particle, due to the very simple Euler integration used.

Now all of the vectors will be relative to the screen coordinates, so if you stick to SI units for everything, the force that you will apply for gravity will be 9.81 * mass, probably downwards. If you work the maths out with just gravity in the frame, the mass is going to disappear - all objects drop to the ground with the same acceleration, regardless of mass. Leaving it in however allows you to plonk drag in later on.

I would advise using SI units (kg, meters, seconds) for now, unless you want to add unit conversion to the maths above!

Andy.





Re: XNA Game Studio Express simple projectile motion physics??

McDunkey

this sounds like it will work. but what about adding a trajectory to this particle. for example i want to fire this off at like a 45 degree angle.  how would that be done



Re: XNA Game Studio Express simple projectile motion physics??

AndyL

Assuming it is being 'launched' from a stationary vehicle, the trajectory is simply the initial velocity expressed as a vector2 (e.g. Vector2(horizontalSpeed, verticalSpeed)).

So for 45 degrees:

velocity = Vector2(1,1); // This creates a 45 degree vector, but with a length > 1
velocity.Normalize(); // This makes the length of the velocity vector (also called the speed!) 1.0
velocity *= launchSpeed; // This scales the speed up to the speed you want to launch the projectile at

This gives you the initial velocity of the particle/projectile.

If it is being lanched from a moving vehicle, just add the velocity of the vehicle to the result above.

NB: The signs and directions depend on which way is up and which way is right. I am used to working in 3D and am not sure which way they are in XNA when using 2D, but assuming that up is negative y and right is positive x (i.e. origin (0,0) is top left of screen) then to launch the particle up and right, the code above would start with a Vector2 of (1, -1).

Andy.

 

Edit: Put another way, the initial velocity is Vector2(sin(theta), -cos(theta)), where theta is the elevation of a gun that might launch the projectile.





Re: XNA Game Studio Express simple projectile motion physics??

flypp3r

VERY helpful!  After I posted this, I looked around for a while for tutorials and whatnot on motion physics for XNA (projectile to be specific) and all I could find was actual physics lessons.  which, my very basic high school physics work there should TECHNICALLY be enough, but for some reason it only likes to work at a 45 degree angle.  ill give your process a go in a lil bit.  it seems a lil more thorough and "realistic" and can be used for just about any motion phyics



Re: XNA Game Studio Express simple projectile motion physics??

Derek Nedelman

You will probably want to take a look at my Balls demo, which has most of what you're looking for including some simple collision detection and response.





Re: XNA Game Studio Express simple projectile motion physics??

McDunkey

awesome. great input from everyone. ill post back here within a day or so with a link to what i come up with and source.



Re: XNA Game Studio Express simple projectile motion physics??

BlogusMaximus

I think I'm the one who mentioned Scorched Earth. You may want to take a look at the T2DVectorUtil class in TorqueX. It offers several helpful methods like:

public static float AngleFromInput(Vector2 inputVector);
public static float AngleFromTarget(Vector2 srcPos, Vector2 targetPos);
public static float AngleFromVector(Vector2 vector);
public static Vector2 VectorFromAngle(float rotation);
public static Vector2 VelocityFromInput(Vector2 inputVector, Vector2 speedScale);
public static Vector2 VelocityFromTarget(Vector2 srcPos, Vector2 targetPos, float speed);






Re: XNA Game Studio Express simple projectile motion physics??

flypp3r

i uploaded what i have so far.  as you can see- its very very simple projectile motion physics.  no friction, mass, or anything taken into account except initial velocity and angle.  it works, HOWEVER, only if i enter certain angles.  45 works, so i set that as the default angle.  also, you can see its not really smooth at all.  im not really worrying about that right now- im just worrying about why its not allowing the angle to be anything but 45 for the most part.  space bar "launches" it

http://www.threesixbox.com/project/ id=9acdc44c77





Re: XNA Game Studio Express simple projectile motion physics??

AndyL

Works fine if you change the following line:

double IA = Convert.ToInt32(InitA * (Math.PI / 180));

to:

double IA = InitA * (Math.PI / 180);

Why were you trying to truncate the angle to an integer





Re: XNA Game Studio Express simple projectile motion physics??

flypp3r

well ill be!  truthfully- i dont know.  it was actually something i found on projectile motion in C# on google... however, i recall doing it like you said at one point and for some reason, it did the same.  whatever the case, it works now.  i knew it was a stupid mistake in math somewhere lol

now to add some bouncing, rolling and all that fun stuff!





Re: XNA Game Studio Express simple projectile motion physics??

AndyL

flypp3r wrote:

well ill be! truthfully- i dont know. it was actually something i found on projectile motion in C# on google... however, i recall doing it like you said at one point and for some reason, it did the same. whatever the case, it works now. i knew it was a stupid mistake in math somewhere lol

now to add some bouncing, rolling and all that fun stuff!

Reminds me of my A-Level maths teacher, who said if you copy someone elses homework and hand it in, don't copy the mistakes!





Re: XNA Game Studio Express simple projectile motion physics??

Derek Nedelman

A screenshot would be nice too. 





Re: XNA Game Studio Express simple projectile motion physics??

flypp3r

 Derek Nedelman wrote:
A screenshot would be nice too. 
lol theres not much to see, but i added one for ya

Ontop of it- i cleaned up the animation and added bouncing to it.  I still need to add some momentum and whatnot to it so velocity.X can decrease.  if anyone has any suggestions, feel free to let me know.  im trying to make a very basic 2D platform physics engine  if you wanna call it that.  with it, i think the platform games (mario) and scorched earth type games can be alot more of a breeze.  basically, just for myself to build off of so dont think im planning "big" like TorqueX or something.  also as a reference for other beginner game programmers