The Daily Click ::. Forums ::. Klik Coding Help ::. Steering Problem
 

Post Reply  Post Oekaki 
 

Posted By Message

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
8th April, 2009 at 13:06:30 -

To illustrate what I'm trying to achieve:
http://cid-b1e7ee094271bbda.skydrive.live.com/self.aspx/Public/Guidance7.mfa

For anyone who's played Sean O'Connors "Critical Mass", I'm trying to recreate the ship control system from that game.

I have a vehicle using a custom "racecar" type movement. It has an initial speed and angle. It also has a limit on the maximum acceleration/deceleration and turning rates.

The game is kind of turn-based - every turn, you set the vehicles acceleration and steering rate, which the vehicle then follows for a fixed period of time. Acceleration and steering rate are both fixed once the vehicle is actually moving (it can't turn right and then go straight during the same turn, for example).

Here's where it gets really tricky...
I want the player to be able to point to a destination with the mouse. I then want the game to calculate the nearest point to the destination that the vehicle is able to reach in one turn (given the steering/accel constraints), and the turning rate & acceleration used to get there.

My current version actually tests out every possible combination of acceleration and turning rate, and uses whichever gets closest. The problem with that is that it's *very* slow, and also not pixel-perfect (it can only use whole-number acceleration and turing rate values).

So,
* Can anyone think of a better way to go about this?
* Ideally, is there a mathematical solution? Like a formula I could use somehow to instantly calculate the optimum acceleration & turning?

It's something I've been trying to figure out for absolutely ages (pretty much since I first played Critical Mass infact).
I'd be hugely grateful to anyone who can help me with this

 
n/a

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
8th April, 2009 at 20:18:40 -

I'll try another question...

I have this really long formula that starts like this:

(Cos(A+(R*1))*(V+(T*1))) + (Cos(A+(R*2))*(V+(T*2))) + (Cos(A+(R*3))*(V+(T*3))) + (Cos(A+(R*4))*(V+(T*4)))....

and continues all the way up to ....(Cos(A+(R*50))*(V+(T*50)))

Is there some way to simplify this?

 
n/a

-J-



Registered
  05/10/2008
Points
  228

VIP MemberThe Cake is a Lie
9th April, 2009 at 11:44:17 -

I'm not sure if it would work but you could try this formula:

25*[ (Cos(A+(R*1))*(V+(T*1))) + (Cos(A+(R*50))*(V+(T*50))) ]

It's based on this formula: Sn = (n\2)*(a+l)
Where Sn is the sum of the first n terms of a progression, n is the amount of terms in the progression, a is the first term and l is the last term.

It only works if each "part" in your equation increments in the same amount each time though.

As a quick example:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

can be shortened to...

5*(1+10) = 55


Hope it helps



Edited by -J-

 
n/a ...

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
9th April, 2009 at 13:51:14 -

Wow, thanks a lot

It all works perfectly if R=0.
If I change R though, it starts going wrong
Do you think it's possible that you've just made a small slip somewhere, or am I going to have to start over?

 
n/a

-J-



Registered
  05/10/2008
Points
  228

VIP MemberThe Cake is a Lie
10th April, 2009 at 02:26:04 -

Well the Sn = (n/2)*(a+l) formula will only work for equations where the same value is added in each part, like this:

3 + 7 + 11 + 15... etc

The numbers in that equation are going up by exactly 4 each time, and an equation like that is called an Arithmetic Progression (or AP). I tried tried replacing A, R, V & T in your equation with the numbers 1, 2, 3, & 4 and found that your really long equation wasn't an AP because the difference between each section was not always the same, but this entirely depended on the values of A, R, V & T so it might have been a fluke that it worked for you when R was 0.

Is there any way you could simplify this: (Cos(A+(R*1))*(V+(T*1))) + (Cos(A+(R*50))*(V+(T*50))) ?

I'm beginning to think you might have to start again on a different path, the Cos function will always be the tiniest bit inaccurate because the method used to find the Cos value involves an infinite equation, so the computer just takes the first 3 or 4 parts of that equation and still gives a very accurate result; it's basically like rounding the number 0.8234629873642346 to 0.8234630000000000 in which you can see the slight inaccuracy. Perhaps because we are dealing with a lot of equations and essentially 100 uses of the Cos function, the inaccuracy could be multiplying and throwing off the final value...

That's a pretty far fetched idea though so it's most likely not the case.

I really don't know how you could simplify your really long equation now... you might have to try something different.

The best advice I could give would be to try and rearrange this part: (Cos(A+(R*1))*(V+(T*1))) somehow and see if you can get everything inside the Cos() brackets, it might make it a lot easier.

Hope this helps a little

 
n/a ...

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
10th April, 2009 at 03:12:19 -

Thnaks a lot for your help.
I think I see the problem now.

There is some error caused by the approximation of Cos(A), but it's not enough for me to be worried about - eg. The expression returns 8600 instead of 8800.

The real problem though, is that it's not an "arithmetic progression" as you put it.
It's actually quite obvious once you know what A,V,R, and T stand for, and what the expression does - I should really have told you that before.

A = angle
V = velocity
R = rotation (change in angle per "step")
T = thrust (change in velocity per "step")

The expression itself is to find the change in the objects X-Coordinate (I'd basically just replace Cos with Sin for the Y-Axis).
If you can picture it, any change in angle (ie. if R is not = 0) is bound to change the rate of acceleration along one axis - it's only the increase in straight-line speed that is constant.

I'm not happy with it, but this is the best I've been able to come up with so far:
http://cid-b1e7ee094271bbda.skydrive.live.com/self.aspx/Public/Steering.exe

It's very slow, and nowhere near "pixel perfect" precise. It's so frustrating because it really doesn't seem like it should be this tough

I may have to just replace the "drag & drop" interface for a simpler system where you control acceleration and turning directly (like in "Flight Commander 2" / "Over The Reich" / "Achtung Spitfire" etc). That would be pretty clunky though.

Edited by Sketchy

 
n/a

CodeCannon

of DringleTopia

Registered
  13/04/2006
Points
  208
10th April, 2009 at 12:53:25 -

Maybe some kind of spline interpolation could help?
http://www.doc.ic.ac.uk/~dfg/AndysSplineTutorial/index.html

MMF sometimes handles numbers as integers when instead you want floats. You could multiply certain expressions by 1.0 to make them floats. Maybe this will help the jumping effect.

If you find the solution, please post it here, I've also been wondering about something similar.

Edited by CodeCannon

 
_
CodeCannon
http://www.dringletopia.da.ru/

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
10th April, 2009 at 15:01:15 -

Someone else also suggested Bézier curves to me, so I think that might be the way to go.
I've haven't got it quite figured out yet, but I'll let you know if/when I do.
It's pretty confusing so far

The problem I had with the arrow "jumping" was only partly to do with me using integers instead of floats. I could have used floats, but that would make it too slow (every decimal place of precision, would require another 10x as many calculations, the way I was doing it).

 
n/a

-J-



Registered
  05/10/2008
Points
  228

VIP MemberThe Cake is a Lie
11th April, 2009 at 05:06:24 -

Bezier curves might be the way to go indeed
I don't know how hard it would be to get a perfect curve like in this though:
http://cid-b1e7ee094271bbda.skydrive.live.com/self.aspx/Public/Guidance7.mfa

It might get a bit confusing if you need lots of points to get the curve right like this:
Image


I havn't really worked with beziers much so I don't know what could happen... I guess it's worth a try

 
n/a ...

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
11th April, 2009 at 12:11:41 -

At the moment, I have two problems with using Bézier curves.

1.) I don't know where to put the "helper points", or even how many I need.
2.) Even if I could make a curve that looks right, how do I get an acceleration and turn rate from that?

-------------------------------------------------------------------------------------------------------------------

I've read the wikipedia page http://en.wikipedia.org/wiki/B%C3%A9zier_curve (and others) on Bézier Curves, many times, and given the 3 or more points, I understand how to draw a curve.

Image
The problem is that I only know 2 points - P0 and P2. How do I find P1?

Is P1 just the point you'd end up at without any rotation or acceleration? If so, I already got as far as drawing that curve.
Even having got a curve though, I don't understand how you can find acceleration & turning from it.

I thought maybe (if I have 50 frames per turn):
* P0 = start point
* P2 = end point (mouse)
* P1 = end point if turning & accel. = 0
* Q0 = 1/50th of the way from P0 to P1
* Q1 = 1/50th of the way from P1 to P2
* B = 1/50th of the way from Q0 to Q1
* Accel. = distance from P0 to B, minus original speed
* Turning = angle from P0 to B, minus original angle

Where have I gone wrong (or is just all completely wrong)?

Edited by Sketchy

 
n/a

-J-



Registered
  05/10/2008
Points
  228

VIP MemberThe Cake is a Lie
11th April, 2009 at 16:12:31 -

I think your method of finding the Acceleration and Turning would work. As for finding P1, I had an idea that it could be something like this:

Lol It's complicated but I think this will accurately give P1:

I hope this GIF makes sense:

Image

Imagine that the Green line is the distance between P0 and P2 and it is also the angle between those points, lets say that it is 40 degrees. Note that the purple dot is the EXACT centre of that green line. Now imagine the green line moves back to form a rectangle with 90 degree angles, and P1 is the part of the green line that the arrow is looking at directly.
When P1 meets the centre point of the green line, that's where it should stop.

That sounded a bit confusing..

Basically, the distance between P1 and P2, and the distance between P1 and P0, should be the same; and P1 should be directly in the "Line of sight" of the arrow (the arrow always points at P1).

Anyway, with this method, because the green line changes angle based on where P2 is, you should always get a good curve:

Image

And it will work whichever way the arrow is facing:

Image


Lol I hope all this helps!



Edited by -J-

 
n/a ...

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
11th April, 2009 at 17:09:10 -

So...
Angle from P0 to P1 = Ships Initial Heading
Distance from P0 to P1 = Distance from P1 to P2

XP3 = (XP1 + XP2) / 2
YP3 = (YP1 + YP2) / 2

Image

A couple of potential problems though:

1.)
I still can't see how to calculate the position of P1.
We can't use trigonometry/pythagoras, because the only length we know is P0->P3, and we need either P0->P1 or P1->P3.

2.)
Should P1 move depending on the initial speed of the object?
The actual shape of the curve would definitely be different, but that might not matter as we really only care about the first frame?


Thanks again for your help

 
n/a

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
11th April, 2009 at 17:52:33 -

Correction to my previous post - the shape of the curve actually changes depending on acceleration, rather than just the initial speed.
The pink line represents the vehicle moving at constant speed.
The blue line represents the vehicle decelerating.

-------------------------------------------------------------------------------------------------------

I've been loooking on wikipedia again, and found a page on tringulation: http://en.wikipedia.org/wiki/Triangulation
I *think* this might work for calculating P1...


Find distance P0 -> P2;

D02 = Sqr(((XP0-XP2) pow 2) + ((XP0-XP2) pow 2))


Find angle P0 -> P1, and angle P1 -> P2 (they're the same);

Angle = Abs( <use extension> - Heading)


Find distance P3 -> P1;

D13 = D02 / (1/Tan(Angle)) * 2)


Find distance P0 -> P1;

D01 = Sqr(((D02 / 2) pow 2) + (D13 pow 2))


Find coordinates of P1;

XP1 = XP0 + (Cos(Heading) * D01)
YP1 = YP0 - (Sin(Heading) * D01)

Edited by Sketchy

 
n/a

-J-



Registered
  05/10/2008
Points
  228

VIP MemberThe Cake is a Lie
12th April, 2009 at 00:21:06 -

I just thought I'd make an example to see if that method worked, and yeah it does

To find the distance between P0 and P1 I just halved the distance between P0 and P2.

Vitalized:



The acceleration/deceleration thing isn't perfect but I guess it'd be something like that.

And here's the events:

Image

And here's the mfa itself :
http://sites.google.com/site/julianmsmithies/Home/P1forSketchy.mfa?attredirects=0


I hope the vitalized thing worked...

Edited by -J-

 
n/a ...

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1971

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
12th April, 2009 at 01:43:09 -

The Vitalize example worked ok (in IE atleast - not if FireFox for some reason).

It certainly draws a nice curve, but there's still a major problem.
It can't be used to provide actual values for Acceleration & Turning still (see my modified version - click to plot actual movement).

http://cid-b1e7ee094271bbda.skydrive.live.com/self.aspx/Public/P1forSketchy%20Modified.mfa

Your acceleration/deceleration value needs to be replaced with something directly related to the initial speed of the object. Otherwise, the line drawn will bear no resemblance to the path actually travelled by the object.

I like that your version is so simple
In one of my version, this is what I ended up with, for calculating the X coordinate of the first "step":

X = (((((X_Target( "Data" )-(X_Start( "Data" )+(Cos(_Angle( "Data" ))*_Speed( "Data" )*_Frames( "Data" ))))/_Frames( "Data" ))+(X_Start( "Data" )+(Cos(_Angle( "Data" ))*_Speed( "Data" )*_Frames( "Data" ))))-((((X_Start( "Data" )+(Cos(_Angle( "Data" ))*_Speed( "Data" )*_Frames( "Data" )))-X_Start( "Data" ))/_Frames( "Data" ))+X_Start( "Data" )))/_Frames( "Data" ))+((((X_Start( "Data" )+(Cos(_Angle( "Data" ))*_Speed( "Data" )*_Frames( "Data" )))-X_Start( "Data" ))/_Frames( "Data" ))+X_Start( "Data" ))

 
n/a
   

Post Reply



 



Advertisement

Worth A Click