10 Replies - 11542 Views - Last Post: 20 September 2010 - 02:07 PM Rate Topic: ***-- 2 Votes

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Projectile motion

Posted 19 September 2010 - 10:10 AM

Ok so i want to implement projectile motion like when you throw a rock and gravity
and gravity brings it down and makes a sort of arc.

My knowledge so far, is knowing how to show something on an angle
ex:
x += (Math.cos(Math.toRadians(angle)) * speed);
y += (Math.sin(Math.toRadians(angle)) * speed);



I want just like what is in this applet at http://www.ngsir.net.../ThrowABall.htm

How is this possible?

This post has been edited by Java Student: 19 September 2010 - 10:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Projectile motion

#2 Nasm  Icon User is offline

  • New D.I.C Head

Reputation: 27
  • View blog
  • Posts: 47
  • Joined: 10-June 10

Re: Projectile motion

Posted 19 September 2010 - 11:39 AM

What is done in that applet is simply that for n steps they draw a dot. Everything else is just simple physics, you get a curve like that when you have a initial velocity and gravity applied to that.

Vector position,speed;
double gravity;
LinkedList<Vector> dots;

init(){
position = new Vector(10,10);
speed = new Vector(vx,vy);
gravity = 9.8;
}

update(dTime){
speed.X -= dTime * gravity;
position += speed * dTime;    //Java doesn't allow operator overloading so you have to do this separately for the X and Y axis or create add/multiply methods.
dots.pushback(position);
}

paint(){
for(Vector d : dots) // you can draw lines in-between the dots if you want a curve here. 
g.fillrectangle(d.X,d.Y,2,2);  //There is even a drawPolynom method that does this for you.
g.fillRectangle(position.x,position.Y, 20,20);
}



This should hopefully give you the idea of how it is done and most other physics simulations out there. They follow the laws observed in the real world.
Was This Post Helpful? 0
  • +
  • -

#3 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Projectile motion

Posted 19 September 2010 - 01:00 PM

That sort of gives me an idea of to do it but
in my situation, would i have something like this
then:
double gravity = 9.8
dobule speedX;
double speedY;
double dTime; //?

//updated every 30m/s
speedX -= (Math.cos(Math.toRadians(angle)) * speed) - dTime * gravity;
speedY = (Math.sin(Math.toRadians(angle)) * speed);

x += speedX * dTime;
y += speedY * dTime;



and where do you get dTime from?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Projectile motion

Posted 19 September 2010 - 01:19 PM

That variable is simply representative of elapsed time. Also, perhaps you should take a look into my tutorial on 2D Gravity using Calculus and Parametric Equations.
Was This Post Helpful? 0
  • +
  • -

#5 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Projectile motion

Posted 19 September 2010 - 01:22 PM

Thanks macos i took a look at that but that looks WAY over my head, i couldn't understand it sorry
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Projectile motion

Posted 19 September 2010 - 01:58 PM

Essentially what I did was defined two functions: one for x and one for y. And going from the starting point to the ending point took twice as much time as the amount of time it took for y to reach its maximum height. So Calculus is a nice, easy way to find the maximum (or minimum) of a function. If you have a quadratic, you can solve for the max using plain old algebra. So if it takes 30 units for y to reach its max, then it takes 60 units for y to go from its max back to the ground. Does this make sense?
Was This Post Helpful? 0
  • +
  • -

#7 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Projectile motion

Posted 19 September 2010 - 02:47 PM

View Postmacosxnerd101, on 19 September 2010 - 12:58 PM, said:

Essentially what I did was defined two functions: one for x and one for y. And going from the starting point to the ending point took twice as much time as the amount of time it took for y to reach its maximum height. So Calculus is a nice, easy way to find the maximum (or minimum) of a function. If you have a quadratic, you can solve for the max using plain old algebra. So if it takes 30 units for y to reach its max, then it takes 60 units for y to go from its max back to the ground. Does this make sense?


In the math world it makes perfect sense
but im just having trouble applying that
in code.

One other question:

In
y += ( (dTime * (Math.sin(Math.toRadians(angle)) * speed) + 0.5 * gravity *(dTime*dTime)) );



what purpose does the
(dTime * dTime)
serve in that physics equation?

This post has been edited by Java Student: 19 September 2010 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,246
  • Joined: 27-December 08

Re: Projectile motion

Posted 19 September 2010 - 02:51 PM

Given y = -4.9t^2 + 15t + 22;, tell me where the max is. Now to apply this in your program, plug in values for t and plot that as the y coordinate, for each iteration. That is all you need to do.

Edit: Gravity is acceleration (or more specifically, deceleration). As position is a distance unit (meters), velocity is a distance over time, acceleration is the rate of change of the velocity (which is also a rate of change), so acceleration is distance/time^2. That is why time is squared in the gravity portion.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Projectile motion

Posted 19 September 2010 - 08:28 PM

Every point (x,y) can be determined using a few formulas

x = x_0 + v_0*t + 0.5at^2 and
y = y_0 + v_0*t + 0.5at^2

x_0 and y_0 are the starting x and y, v_0 are the starting velocities (the x and y components of the velocity angle), a is -9.8 (gravitational constant), and t is the current time in seconds. In order to solve these problems, you should know basic Kinematics, but this should do it.
Was This Post Helpful? 0
  • +
  • -

#10 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 481
  • Joined: 05-February 10

Re: Projectile motion

Posted 20 September 2010 - 09:44 AM

View PostDogstopper, on 19 September 2010 - 07:28 PM, said:

x = x_0 + v_0*t + 0.5at^2 and
y = y_0 + v_0*t + 0.5at^2


That looks allmost exactly like my equation, i,ll definatly look deeper into Kinematics.
Except for a few things like -9.8, my gravity is +9.8 because the way a game screen is layed out.

I,ll ask if i have anymore questions thanks :bigsmile:

This post has been edited by Java Student: 20 September 2010 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,032
  • Joined: 15-July 08

Re: Projectile motion

Posted 20 September 2010 - 02:07 PM

View PostJava Student, on 20 September 2010 - 11:44 AM, said:

That looks allmost exactly like my equation, i,ll definatly look deeper into Kinematics.
Except for a few things like -9.8, my gravity is +9.8 because the way a game screen is layed out.

I,ll ask if i have anymore questions thanks :bigsmile:


Gravity can have a positive or negative acceleration depending on which direction you establish as the positive numbers. Just make sure that you adjust the whole coordinate system.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1