A Graph of gravity + friction motion

For the discussion of math. Duh.

Moderators: gmalivuk, Moderators General, Prelates

BasV
Posts: 4
Joined: Fri Apr 02, 2010 8:41 pm UTC

A Graph of gravity + friction motion

Postby BasV » Fri Apr 02, 2010 8:58 pm UTC

Hello people,

I'm making a flash game in which players throw projectiles to each other - these projectiles have horizontal and vertical speeds: hspeed and vspeed, an x and y coordinate, and set gravity and friction. The motion of the object is updated each frame with this simple code:

Code: Select all

vspeed += GRAVITY;
         
x += hspeed;
y += vspeed;
         
hspeed *= LINEAR_DAMPING;
vspeed *= LINEAR_DAMPING;


Now for the computer-controlled enemy, I wish to be able to calculate the landing spot of a thrown projectile. Right now, I'm doing this with a function that 'simulates' the movement of a projectile. However, I'd like this to be one simple math function, like x = hspeed*friction^n etc.

So far, I've come up with these functions:

Code: Select all

x(0) = xstart
x(n) = xstart + hspeed * sum(LINEAR_DAMPING^(n-1))

y(0) = ystart
y(n) = ystart + (y(n-1)*LINEAR_DAMPING + GRAVITY)


I've also come up with a non-recursive function for y, but I'm not sure if it was correct so I decided not to post it here.

My main question is - is there any way to make these function non-recursive, and not have any sum in it?

Can I rewrite

Code: Select all

LINEAR_DAMPING^0 .. LINEAR_DAMPING^n


to anything 'direct' ?

In the end, I'll need something like y = ..x..
so I can calculate the y coordinate just by having the x coordinate. I could really use a push in the right direction. My maths knowledge isn't THAT bad, but I just can't get on with this..

Thanks!
Last edited by BasV on Fri Apr 02, 2010 11:13 pm UTC, edited 2 times in total.

User avatar
Torn Apart By Dingos
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

Re: A Graph of gravity + friction motion

Postby Torn Apart By Dingos » Fri Apr 02, 2010 10:14 pm UTC

BasV wrote:Can I rewrite

Code: Select all

LINEAR_DAMPING^0 .. LINEAR_DAMPING^n


to anything 'direct' ?
Yes, this is a geometric series (look this up on wikipedia, there's a very simple derivation for the formula). If d=LINEAR_DAMPING, then [math]d^0+...+d^n=\dfrac{1-d^{n+1}}{1-d}.[/math]

BasV
Posts: 4
Joined: Fri Apr 02, 2010 8:41 pm UTC

Re: A Graph of gravity + friction motion

Postby BasV » Fri Apr 02, 2010 10:32 pm UTC

Torn Apart By Dingos wrote:Yes, this is a geometric series


Thanks a lot. I found it on wikipedia. I've looked for it but I didn't know the english term for this type of equation. (Don't think I'd even know in my own native language but anyway :P). I'll get a lot further now!

edit: It's working like a charm.

The only remaining problem now is to do the same to the y coordinate, which has the extra constant GRAVITY in it.

User avatar
squareroot1
Posts: 172
Joined: Fri Nov 06, 2009 8:27 pm UTC

Re: A Graph of gravity + friction motion

Postby squareroot1 » Sat Apr 03, 2010 12:43 am UTC

If you are looping this in order:
v += g
x += h
y += v
h *= d
v *= d

Current frame is n.
x(0) = x0
y(0) = y0

x(n) = x0 + h + hd + hd^2 + ... hd^(n-1) = x0 + h(1-d^n)/(1-d)

y(n) = y0 + (v+g) + (v+g)d + g + (v+g)d^2 + gd + g + (v+g)d^3 + gd^2 + gd + g ... (v+g)d^(n-1) + gd^(n-2) + gd^(n-3) + ... + g
Collect all the (v+g) terms first, then note we are summing over g times a partial geometric series.
y(n) = y0 + (v+g)(1-d^n)/(1-d) + g ∑ (1-d^k)/(1-d) from k=1 to n-1
Pull out the 1/(1-d).
y(n) = y0 + (v+g)(1-d^n)/(1-d) + g/(1-d) * ∑ (1-d^k) from k=1 to n-1
Break the summation, 1+1+1...1 is just (n-1). Note we have a geometric series left, so substitute in and subtract off the extra (k=0) term.
y(n) = y0 + (v+g)(1-d^n)/(1-d) + g/(1-d) * ((n-1)+(d^n-1)/(1-d)+1)
Simplify.
y(n) = y0 + (v+g)(1-d^n)/(1-d) + g(d^n+(1-d)n-1)/(1-d)^2

You may get some difference between the two approaches, as the dampening gets compounded in the looping approach and likely underflows your storage format fairly quickly. That would cause deviations from the path predicted by the second approach, which would also underflow due to the d^n terms, so whatever, enjoy.

Edit: Added text to the block of math so it can be understood better.

BasV
Posts: 4
Joined: Fri Apr 02, 2010 8:41 pm UTC

Re: A Graph of gravity + friction motion

Postby BasV » Sat Apr 03, 2010 12:02 pm UTC

Thanks a lot, I'm gonna study and implement this. One thing I noticed:

squareroot1 wrote:x(n) = x0 + h(1-d^n)/(1-d)


I used this:

[math]x(n) = x0 + h(1-d^{n+1})/(1-d)[/math]

based on

Torn Apart By Dingos wrote:[math]d^0+...+d^n = (1-d^{n+1})/(1-d).[/math]


I don't really understand why there's a difference in your equation.

Philonoist
Posts: 1
Joined: Sat Apr 03, 2010 6:40 pm UTC

Re: A Graph of gravity + friction motion

Postby Philonoist » Sat Apr 03, 2010 7:00 pm UTC

I have another formulation of the solution, our equation is [imath]v_{n+1}=dv_n+g[/imath]
We can rewrite it: [imath]a_{n+1}=v_{n+1}+c = d(v_n+c)=da_n[/imath] for [imath]c=\frac{g}{d-1}[/imath]
Which means, [imath]v_n=a_n-c=a_0d^n-c=(v_0+c)d^n-c[/imath]
We have [imath]y=\sum_i v_i = (v_0+c)\frac{d^n-1}{d-1} - cn[/imath]
Or, [imath]y= (v_0+\frac{g}{d-1})\frac{d^n-1}{d-1} - \frac{g}{d-1}n[/imath]
Notice that u should put [imath]v_0=v+g[/imath]

Maybe this will help a bit for the understanding...

User avatar
squareroot1
Posts: 172
Joined: Fri Nov 06, 2009 8:27 pm UTC

Re: A Graph of gravity + friction motion

Postby squareroot1 » Sat Apr 03, 2010 11:56 pm UTC

BasV wrote:I don't really understand why there's a difference in your equation.

The final term of the sum has a different power.
d^0+...+d^(n-1)+d^n = (1-d^(n+1))/(1-d)
vs
h[d^0+...+d^(n-1)] = h(1-d^n)/(1-d)

Philonoist wrote:[imath]y= (v_0+\frac{g}{d-1})\frac{d^n-1}{d-1} - \frac{g}{d-1}n[/imath]
Notice that u should put [imath]v_0=v+g[/imath]

I expected there would be a simpler form, but had a party to get ready for. I just now went through the algebra to confirm the two results agree.
I'm curious through, did you just notice the value for c or is there a process to eliminate the constant term in a linear recursion (other than assume c exists and solve for its value)?

BasV
Posts: 4
Joined: Fri Apr 02, 2010 8:41 pm UTC

Re: A Graph of gravity + friction motion

Postby BasV » Sun Apr 04, 2010 10:26 am UTC

Thank you both for your input. This is helping me a lot!


Return to “Mathematics”

Who is online

Users browsing this forum: No registered users and 7 guests