Ruchira
Ruchira

Reputation: 955

implement projectile motion. android

I am trying to implement a projectile motion for my app.To begin with I am trying to plot the points on the projectile.I am assuming my initial velocity to be 5m/s.

I tried to calculate my initial velocity based on projection angle for x and y direction respectively.But I am getting it's vale us NaN(Not a number).

Also I am trying to plot first 10 points on the path of trajectory but am getting NaN for them also.As the time returned by now is in millisecond I am converting it into seconds.

Please suggest where I went wrong.

public void drawProjectile(double angle)
 {
     Log.w(this.getClass().getName(),"drawProjectile called");
     mUx = mUi*Math.acos(angle);
     mUy = mUi*Math.asin(angle);


     Log.d(this.getClass().getName(), "Value of mUx: " + Double.toString(mUx));
     Log.d(this.getClass().getName(), "Value of mUy: " + Double.toString(mUy));
     for(int i = 1;i<=10;i++)
     {
     Log.w(this.getClass().getName(),"In plotting points loop");
     long now = System.currentTimeMillis();
     mX1=(float) (mUx*now)/1000;
     mY1 = (float) (mUy*now/1000+(mGravity/2)*now*now/1000000);

     Log.d(this.getClass().getName(), "Value of mX1: " + Float.toString(mX1));
     Log.d(this.getClass().getName(), "Value of mY1: " + Float.toString(mY1));
     mCanvas.drawPoint(mX1, mY1, mPaint);
     }

Upvotes: 1

Views: 1729

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234795

I think you want Math.cos(angle) and Math.sin(angle), not Math.acos(angle) or Math.asin(angle). Also, make sure that angle is in radians, not degrees.

EDIT: Regarding the "exponential values," my guess is that you shouldn't be using the current system time in your calculations. (That is the number of milliseconds since the start of January 1, 1970; hardly a time relevant to your problem.) You should be using the elapsed time from when your program started (long start = System.currentTimeMillis(); before the loop, then subtract start from now inside the loop), or (perhaps better) a simulated time between points. Something like this:

float now = 0f;
for(int i = 1; i <= 10; i++) {
    Log.w(this.getClass().getName(),"In plotting points loop");
    mX1 = mUx*now;
    mY1 = mUy*now + (mGravity/2)*now*now;

    Log.d(this.getClass().getName(), "Value of mX1: " + mX1);
    Log.d(this.getClass().getName(), "Value of mY1: " + mY1);
    mCanvas.drawPoint(mX1, mY1, mPaint);
    now += 1f; // or whatever time increment you want
 }

If you need to use actual elapsed time, then this loop won't do it, because the time for executing the 10 iterations won't amount to any perceptible change in system time.

Upvotes: 2

Related Questions