Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tiny Gravity procedure
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Amarylis




PostPosted: Mon Mar 19, 2012 9:53 pm   Post subject: Tiny Gravity procedure

So this just applies gravity into an object by adding the required y-axis velocity to the object.

It's probably severely redundant, it's just in case anyone is wanting to not have to figure it out themselves or something. Come to think of it, not entirely sure why I made this, just thought I'd start developing a few physics things. Don't judge me, please.

Turing:

procedure Gravity (var yVel : int, var t : int)
    yVel -= round(9.8 * ((Time.Elapsed - t) / 1000) ** 2)
end Gravity


And an example for how it could be used:

Turing:

procedure Gravity (var yVel : int, var t : int)
    yVel -= round(9.8 * ((Time.Elapsed - t) / 1000) ** 2)
end Gravity
View.Set ("offscreenonly")


var yVel : int := 100
var t : int

t := Time.Elapsed
for i : 0 .. 500
    Draw.FillOval (0 + i, 200 + yVel, 10, 10, 16)
    Gravity (yVel, t)
    View.Update
    cls
    exit when yVel < -400
end for
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Mar 19, 2012 11:11 pm   Post subject: RE:Tiny Gravity procedure

you might call the variable "velocity", but it's tracking the value of position instead.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Amarylis




PostPosted: Tue Mar 20, 2012 1:33 am   Post subject: RE:Tiny Gravity procedure

The purpose to this was really just to get things to fall at a "more accurate speed", so that's really all it's doing, assuming 1 px = 1m
Tony




PostPosted: Tue Mar 20, 2012 2:30 am   Post subject: RE:Tiny Gravity procedure

okay. Though I'm not sure why you are raising the time different to the power of 2, in your calculations. Any elaboration on that?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Amarylis




PostPosted: Tue Mar 20, 2012 5:33 am   Post subject: RE:Tiny Gravity procedure

a = 9.8 mps^2,

quite literally, 9.8 per second per second
Raknarg




PostPosted: Tue Mar 20, 2012 9:14 am   Post subject: RE:Tiny Gravity procedure

well, if you're just calculating velocity with that, the original formula would be:

a = v/t

therefore:

v = at

I think when you say 9.8t^2, you're thinking in terms of distance rather than velocity.
Amarylis




PostPosted: Tue Mar 20, 2012 9:49 am   Post subject: RE:Tiny Gravity procedure

I don't think so...

since the impact of gravitational acceleration should be a parabola if represented on a V-T graph.
Dreadnought




PostPosted: Tue Mar 20, 2012 10:32 am   Post subject: Re: Tiny Gravity procedure

No, the trajectory of a projectile in a uniform gravitational field will be a parabola. However, the component of the velocity in the direction of the field will vary linearly.

In general for position of an object undergoing constant acceleration we have x(t) = x(0) + v(0)*t + (1/2)*a*t^2 where v is the velocity (the derivative of position if you know calculus).
For the velocity (again with constant acceleration) we have v(t) = a*t.

This makes sense, when we consider what acceleration and velocity are, the rate of change of velocity and position respectively.

Make sure you don't confuse position and velocity.

A position-time graph would show a parabola, a velocity-time graph will show a straight line (with a slope equal to the acceleration).
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Mar 20, 2012 10:48 am   Post subject: RE:Tiny Gravity procedure

No. The impact of gravitational acceleration should be a line on a V-T graph and a parabola on a D-T graph, ignoring anything complex, like non-uniform G fields, air resistance, etc.

Your current code is incorrect. The position, Y, depends on both the time difference between calls and the number of calls. If you write your loop to go 10 times with a delay of 200ms, and then change it to 20 times with a delay of 100ms, it will end up in a considerably different position.

Example. Both loops run for the same real-time (or very close), and have the same initial conditions. However, one makes twice as many calls to Gravity() in that time, so it goes about twice as far.
Turing:

procedure Gravity (var yVel : int, var t : int)
    yVel -= round(9.8 * ((Time.Elapsed - t) / 1000) ** 2)
end Gravity

View.Set ("offscreenonly")

var yVel : int := 100
var t : int

t := Time.Elapsed
yVel := 100
for i : 0 .. 10
    Draw.Oval (100 + i, 200 + yVel, 10, 10, brightred)
    Gravity (yVel, t)
    View.Update
    Time.DelaySinceLast ( 200 )
end for

t := Time.Elapsed
yVel := 100
for i : 0 .. 20
    Draw.Oval (200 + i, 200 + yVel, 10, 10, brightblue)
    Gravity (yVel, t)
    View.Update
    Time.DelaySinceLast ( 100 )
end for


Your code would be "correct" if you assigned the value of Y based on some initial value, rather than modifying Y at each iteration. Then, you would have a scaling of the equation d = v1 * dt + 1/2 * a * t ^ 2, one of the basic kinematics equations. However, this only works if the object falls indefinitely without hitting anything and stopping.

You could also make the code correct (and more useful) if you tracked the object's location and velocity. Then, on each iteration, you could modify the velocity by (v = a * dt), and then position by (d = v * dt).
Amarylis




PostPosted: Tue Mar 20, 2012 11:44 am   Post subject: RE:Tiny Gravity procedure

Whoops, messed up my graphs Razz

Just to make sure that doesn't happen again, please someone confirm if the bellow statement is true:

constant acceleration causes a y=mx+b line for velocity resulting in a parabola for displacement

Pardon the bit of code that was incorrect Razz I'll be sure to fix that as soon as I can
mirhagk




PostPosted: Wed Mar 21, 2012 7:39 pm   Post subject: RE:Tiny Gravity procedure

Yes that is correct Amarylis.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: