Need some help for a movement equation

A place for people with an interest in developing new shmups.
Post Reply
mobaladje
Posts: 5
Joined: Wed Mar 10, 2010 10:01 pm

Need some help for a movement equation

Post by mobaladje »

Hello.
I'm trying to code the ennemies pathes in my game and I have some problem.
I'd like to get the equation for the kind of movement :
- the sprite goes from x1,y1 to x2,y2 in t seconds with a speed from s1 to s2.

How can I get instant x,y and speed between 0 and t seconds ?

Thanks a lot.
Regards.
User avatar
worstplayer
Posts: 861
Joined: Sun Jun 17, 2007 6:48 pm
Location: Slovakia

Re: Need some help for a movement equation

Post by worstplayer »

The explanation is a bit unclear. From what I understand you have an object moving from x1y1 to x2y2 in time t, and you want position and speed in any given moment between 0 and t? Or you have object moving from x1y1 to x2y2 accelerating from s1 to s2, and you want speed and position in any moment? Please elaborate.
"A game isn't bad because you resent it. A game is bad because it's shitty."
mobaladje
Posts: 5
Joined: Wed Mar 10, 2010 10:01 pm

Re: Need some help for a movement equation

Post by mobaladje »

What I would prefer it's an equation for the first case : an object moving from x1y1 to x2y2 in time t, and I want position and speed in any given moment between 0 and t.
User avatar
worstplayer
Posts: 861
Joined: Sun Jun 17, 2007 6:48 pm
Location: Slovakia

Re: Need some help for a movement equation

Post by worstplayer »

OK, in that case the speed is constant, and it's a distance between x1y1 and x2y2 divided by time

Code: Select all

s=sqr((x1-x2)^2+(y1-y2)^2)/t
and you can get position at any time by interpolating between positions

Code: Select all

pos=t/currenttime

dx = x2 - x1
dy = y2 - y1

x = x1 + (dx * pos)
y = y1 + (dy * pos)
Or, alternatively, you can get new position by adding speed you just calculated to x1y1

Code: Select all

length=s*time

direction=atan2(x2-x1,y2-y1)

x=x1+(length*sin(direction))
y=y1+(length*cos(direction))
Either method works (maybe, didn't actually test it), choose whichever you want.
"A game isn't bad because you resent it. A game is bad because it's shitty."
mobaladje
Posts: 5
Joined: Wed Mar 10, 2010 10:01 pm

Re: Need some help for a movement equation

Post by mobaladje »

Thanks a lot.
But in fact, i think you understand my problem better than me and I misunderstood your first question.
What I want is : an object moving from x1y1 to x2y2 accelerating from s1 to s2. And I want to know how long it takes (t) and x and y in any given moment between 0 and t.
Ex-Cyber
Posts: 1401
Joined: Thu Oct 25, 2007 12:43 am

Re: Need some help for a movement equation

Post by Ex-Cyber »

mobaladje wrote:Thanks a lot.
But in fact, i think you understand my problem better than me and I misunderstood your first question.
What I want is : an object moving from x1y1 to x2y2 accelerating from s1 to s2. And I want to know how long it takes (t) and x and y in any given moment between 0 and t.
You can't really get anywhere unless you define the acceleration/force function. Let's say you want to specify a constant rate of acceleration, call it a (not sure exactly how you want to define this wrt s2 or whatever). Then

speed at a given time: s(a, t) = s1 + a*t
distance from starting point: d(a, t) = (1/2)*a*t^2 + s1*t

I'm too tired right now to have even a little confidence in translating this to x(t) and y(t), but it should be some fairly straightforward trig for someone who is actually awake.

Things get more complicated if you want a itself to be a function of time and/or position. It's still a tractable problem, but not for me after 3 drinks and just before bed. Actually, I'm not 100% convinced this is correct, but it should be a start. Anyway, I'm not sure why you'd want to do this rather than just defining the X and Y acceleration as an interval to add to the current X and Y positions.

edit: to elaborate a bit more on what I mean by that last sentence, suppose you have a sprite with coordinates x and y. Then dx and dy are the corresponding accelerations per unit time (whatever you're measuring time in). Suppose the update time in those time units has been stored in the variable dt (could be constant if you're on old-school hardware, or variable if you're coding flash or something where the time step is not guaranteed). Suppose you have a function to do the appropriate collision detection and sum up all the force fields applied to the object on the x and y axes as fx and fy (if needed; e.g. gravity might just be a constant, or you might just set an object's dx and dy when it's spawned and let fx and fy be zero) Then you can basically do something like:

dx += fx*dt/mass;
dy += fy*dt/mass;
x += dx*dt;
y += dy*dt;

This isn't precise enough for a serious simulation, but I hope it gets the basic idea across.
mobaladje
Posts: 5
Joined: Wed Mar 10, 2010 10:01 pm

Re: Need some help for a movement equation

Post by mobaladje »

Thanks for your help.
I'm trying to code the path of my ships like that : the ship starts at sx, sy with a speed s=0, go to x1,y1 and its speed must be s1 when it arrives at x1,y1 and now it goes to x2,y2 and ts speed must be s2 when it arrives.
It's very easy with s = s1 = s2 = a fix value but with different speed values, it's really complicated.
Ixmucane2
Posts: 761
Joined: Mon Jan 19, 2009 3:26 pm
Location: stuck at the continue prompt

Re: Need some help for a movement equation

Post by Ixmucane2 »

We can choose to have a constant acceleration (a) and solve for movement duration T given initial and final positions x1 and x2 and velocities v1 and v2:

v(0)=v1
v(t)=v1+a*t
v(T)=v2

so a=(v2-v1)/T.

On the other hand

x2-x1=integral from 0 to T of v(t) dt

that is, the objects goes from x1 to x2 in the same time T it takes to accelerate from v1 to v2.

The integral neatly splits into
x2-x1=v1*T + 0.5 a*T*T
x2-x1=v1*T+0.5* (v2-v1)*T
x2-x1= 0.5*(v1+v2)*T
that is, on average the speed is the mean of v1 and v2 because the acceleration is constant.

In the end,
T=2*(x2-x1)/(v1+v2)
a=2*(v2-v1)*(v2+v1)/(x2-x1)
x(t)=x1+v1*t+0.5*a*t*t

However, this works as intended only in one dimension (two unknowns, T and a, and two equations). In two or more dimensions there are too many constraints: two more equations but one added unknown (the acceleration component).
This means that either positions and velocities must be "lucky" and give the same T, or the object will have the prescribed horizontal and vertical positions and velocities at the same time but not at the same time on both axes (adding unknowns: a separate T for each dimension), or the most natural approach: position, the usually important quantity, can be prescribed on each axis at the cost of not constraining velocity.
mobaladje
Posts: 5
Joined: Wed Mar 10, 2010 10:01 pm

Re: Need some help for a movement equation

Post by mobaladje »

It's the solution I was looking for.
Thanks a lot to everyone.
Post Reply