• Advertisement
Sign in to follow this  

Trajectory Help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya, I am doing a bit of a clone of Scorched Earth, but with Monkeys on buildings tossing banana's around. The original game was called Gorilla. Anyways, can anyone help me (someone who has not taken an advanced math class in 3-4 years) with the formulas and data needed to track the bananas as they hurl across the screen? Obviously the starting point is having the angle and velocity (or power of the throw) correct? So where do I go from here????

Share this post


Link to post
Share on other sites
Advertisement
depends on many things. Do you want the trajectory to be a circle? a realistic curve with gravity thrown in? Do you want the banana thrower to aim at a precise point?

Share this post


Link to post
Share on other sites
an arc, that is pretty realistic. I found an app that had trajectories with and without gravitational pull, and either will do.

Have you ever played Scorched Earth?? The tank game that players have an angle and power of their turret and then shoot. They narrow the distance they missed by each turn they have until theyn finally nail the guy on the other side of the screen. Good fun! Anyways, that is the type of trajectory I would like to figure out how to use.

Share this post


Link to post
Share on other sites
that's easy. using standard physics will be both simpler, and more useful that an arc approximation.

it's basic rigid body dynamics, or Newton's law of physics.

sum for the forces on a particle = mass of particle * acceleration of particle

=> F = m * A
=> A = F / m

and the accleration is the velocity derived by time.

A = dV / dt

=> dV = A * dt
=> V += A * dt

and the vbelocity is the change in position in time

=> dV = dP / dt

=> dP = dV * dt

=> P += V * dt

these three equations are all you need to make a particle (bullet, banana, whatever) move in a pretty realistic way.

dt is obviously the time elapsed betwen two physics frame (usually, 1 / 60).

first,

A = F / m, where F is the sum of the forces acting on that particle.

The forces that act on the particle are the gravity, and the original force geenerated by the shell propellant when the gun is fired.

The gravity force is simply G = Vector(0.0f, -9.81f * mass);

The force generated by the canon and propelant is simply...

F = Vector(cos(angle) * force, sin(angle) * force);

where force is the power of the shot, and angle the angle of the shot.

to summarise...



struct Vector
{
float x, y, z;

Vector()
{}

Vector(float _x, float _y, float _z=0.0f)
: x(_x)
, y(_y)
, z(_z)
{}

Vector& operator += (const Vector& V) { x += V.x; y += V.y; z += V.z; return *this; }
Vector& operator -= (const Vector& V) { x -= V.x; y -= V.y; z -= V.z; return *this; }
Vector& operator *= (float k) { x *= k; y *= k; z *= k; return *this; }
Vector& operator /= (float k) { return (*this) *= (1.0f / k); }

Vector operator * (float k) const { Vector Temp(*this); return Temp *= k; }
Vector operator / (float k) const { Vector Temp(*this); return Temp /= k; }
Vector operator + (const Vector& V) const { Vector Temp(*this); return Temp += V; }
Vector operator - (const Vector& V) const { Vector Temp(*this); return Temp -= V; }

friend Vector operator * (float k, const Vector& V) { return V * k; }

Vector operator - () const { return Vector(-x, -y, -z); }
float operator * (const Vector& V) const { return x*V.x + y*V.y + z*V.z; } // dot product
Vector operator ^ (const Vector& V) const { return Vector(y*V.z - z*V.y, z*V.x - x*V.z, x*V.y - y*V.x); } // cross producrt

float Magnitude() const { return sqrt(x*x + y*y + z*z); }
float Normalise() const { float mag = Magnitude(); if (mag < 1.0E-8f) return 0.0f; (*this) /= mag; return mag; }
};

struct cParticle
{
cParticle(const Vector& xPos, const Vector& xVel, const Vector& Force, float mass)
: m_xPosition(xPos)
, m_xVelocity(xVel)
, m_xSumForce(Force)
, m_xAcceleration(0.0f, 0.0f)
, m_fMass(mass)
{}

void AddForce(const Vector& Force)
{
m_xSumForce += Force;
}

void Update(float dt)
{
AddForce(Vector(0.0f, -9.81f * m_fMass));

m_xAcceleration = m_xSumForce / m_fMass;
m_xVelocity += m_xAcceleration * dt;
m_xPosition += m_xVelocity * dt;
m_xSumForce = Vector(0, 0); // clear forces for next frame
}

void Render()
{
glPointSize(3.0f);
glEnable(GL_POINT_SMOOTH);
glBegin(GL_POINTS);
glVertex2f(m_xPosition.x, m_xPosition.y);
glEnd();
}

Vector m_xPosition;
Vector m_xVelocity;
Vector m_xAcceleration;
Vector m_xSumForces;
float m_fMass;
};

// make a bullet
cParticle MakeBullet(const Vector& xOrigin, float fAngle, float fPower)
{
return cParticle(xOrigin, Vector(0, 0), Vector(cos(fAngle), sin(fAngle)) * fPower, 1.0f);
}



Share this post


Link to post
Share on other sites
Thanks for the code!
Your explanation helped a lot as well. I have also found a highschool physics course online that helped as well (since I DID take this back in highschool but... you know, I'm old now so... haha)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement