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

Post Reply  Post Oekaki 
 

Posted By Message

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
22nd February, 2010 at 16:36:19 -

This is a problem I've been trying to figure out, on-and-off for about the last 3 years (I've even posted to ask for help before). Anyway, here goes...

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

I have two points - point A, and point B.
I have a spaceship which starts at point A, and has to travel to point B.
The spaceship has an initial heading, turns at a constant rate, and moves at a constant speed.
Because of this, the spaceship will move in a circular arc, as shown in the diagram below.

What I need to be able to find is:
* The length of the arc (drawn in red) - from which I can get the speed to move
* The angle "f" - from which I can get the rate of turn

Image

As you can see, I've tried to do the maths myself, but it's not working out right.
Can anyone spot the flaw(s) in my logic?

I'm also not sure how I'm going to make it work with reverse speeds...

btw: Bezier curves are no good for this, because I will want to limit the maximum turning rate and acceleration.

Any help very much appreciated, as this has been holding me back for ages

 
n/a

aphant



Registered
  18/05/2008
Points
  1242
22nd February, 2010 at 17:58:09 -

Eyeballing it, angle F looks like its 80°. Measuring it in Photoshop, 81.3°.

The arc looks like it's about 216 pixels, after quickly measuring it in Photoshop.

Question: Why didn't you just use different values and tweak them until you got the desired result?

 

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
22nd February, 2010 at 18:07:00 -

Sorry, that was just a diagram to help illustrate the maths.
In the game, the spaceship (point A) moves & rotates, and you drag point B with the mouse.

Example pic: http://www.windowsgames.co.uk/images/pic_crit6.gif
You drag the end of the arrow to choose where the ship should move (within speed/turning constraints.

 
n/a

Hernan



Registered
  04/03/2003
Points
  707

VIP Member
22nd February, 2010 at 18:10:44 -

Taking a quick glance at the drawing:
arc length= R * c (in radians)
the angle f = c
Which is what you wrote next to it I see. I don't see any mistakes, so it's best to check for other errors. Like radians/degree screwups or calcluting the angle in the wrong quadrant.

(edit: R means circle radius obviously)

Edited by Hernan

 
This space is for rent

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
22nd February, 2010 at 22:40:52 -

Thanks.
You were right - after some careful inspection of my code, I noticed there was a "* 2" missing.
I don't use radians at all - that's the only thing stopping me from doing it all in Lua (the trig functions in Lua use radians, and it's too much hassle converting back and forth).

Anyway, that's the easy part done. Now just need to add turn/acceleration limiting, and make it work for reverse movements aswell...

Incase anyone's curious:
http://cid-b1e7ee094271bbda.skydrive.live.com/self.aspx/Public/MoveDemo.mfa

Edited by Sketchy

 
n/a

Xgoff



Registered
  22/02/2009
Points
  135
23rd February, 2010 at 03:52:06 -


Originally Posted by Sketchy
(the trig functions in Lua use radians, and it's too much hassle converting back and forth)


well you could always just redefine math.cos and math.sin to use degrees:

do

local degtorad = math.pi / 180
local oldsin, oldcos = math.sin, math.cos
function math.sin (angle)
return oldsin(angle * degtorad)
end
function math.cos (angle)
return oldcos(angle * degtorad)
end
end

near the top of your code... or you could just define new cos/sin functions. whatever

EDIT: also i think the code tag needs fixed, the line spacing is ridiculous

Edited by Xgoff

 
Image

Muz



Registered
  14/02/2002
Points
  6499

VIP MemberI'm on a BoatI am an April FoolHonored Admin Alumnus
23rd February, 2010 at 07:04:59 -

Dude, use radians. It's a lot easier than degrees. Once you figure out radians, you'll never go back to degrees, it just makes more sense.

2 pi = full circle (360 deg). 1 pi = semi-circle(180 deg). pi/2 = quarter of a circle (90 deg). Don't calculate the value.. just use pi as a constant.


To get angles, think of the starting point as pointing right. The angle is counted anti-clockwise. So, from your starting point pointing up, turn your head so that it points right. Final heading is pointing downwards... it's like three quarters of a circle.

So, f = 3/4 * 2*pi radians [3/4 of circle]

If you want to calculate it clockwise,
f = -1/4 * 2*pi radians [1/4 of circle, clockwise]


For AB:
Circumference = 2 * pi * radius
Part of a circumference = 2 * pi * Section * radius

Length of AB = 2*pi*(c/2*pi)*radius
Length of AB = c*radius [assuming c is in radians]

 
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.

Image

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
23rd February, 2010 at 17:10:56 -

Thanks guys.
I know I should really learn about radians, but I'm lazy, and I already know all the trig formulas using degrees.

Xgoff's idea of writing new trig functions for Lua is very cunning though - I think I'll try that


 
n/a

Muz



Registered
  14/02/2002
Points
  6499

VIP MemberI'm on a BoatI am an April FoolHonored Admin Alumnus
24th February, 2010 at 10:11:37 -

Pshh.. if you're lazy, learn radians. Seriously, it's a lot easier. 360 degrees is some arbitrary number. There's no reason or purpose for it, it's not even an easy range of values to guess. I don't get why you guys put so much effort in avoiding it, it only takes like 10 minutes to learn

 
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.

Image

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
24th February, 2010 at 16:41:22 -

I'm pretty sure it takes longer than 10 minutes, or it would be taught in primary school
Besides, everything else in MMF uses degrees, not radians.

 
n/a

Xgoff



Registered
  22/02/2009
Points
  135
24th February, 2010 at 20:14:04 -

one big draw of using degrees rather than radians in this case is that radians are floating-point, while in most cases degrees will be integers. integers are nice because they don't suffer precision problems* like floating-point generally does. of course that's not to say you'll never use decimal degrees, but 360 degrees is usually enough for most purposes.

you could get around radian precision issues in lua by making an "exactRadian" object or something that actually stores the numerator and denominator as integers, and when you need the decimal value it will calculate the (n*pi)/d value on the fly. of course this is probably more effort than it's worth since fraction -> float conversion is easy but a (likely inexact) float -> fraction conversion is somewhat more difficult. you'd pretty much need to find/implement a whole fraction-manipulation system to make these more than just slightly usable.

mathematically radians are more elegant but this isn't always the case when trying to work with them in code

* they will if they overflow their range but this isn't likely to happen given how huge a double's integer range is and how small the degree range is

 
Image

Muz



Registered
  14/02/2002
Points
  6499

VIP MemberI'm on a BoatI am an April FoolHonored Admin Alumnus
26th February, 2010 at 04:28:19 -

I could teach you in 10 minutes if I had a piece of paper. I'm pretty sure they can teach it, along with wherever you learn angles. Just that the teachers are too used to degrees. It's even easier than Centigrades and meters, because it's completely instinctive Maybe I'll write an article for you guys later with pictures and stuff.

Eh, floating point gets very accurate. MMF uses doubles, which is way more accurate. It's pixel perfect to at least 1000 pixels away, at least with my engine. Way more accurate than you get from just 360 degrees. The code in MMF/Lua should handle overflow and stuff, so it shouldn't happen.

 
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.

Image

Xgoff



Registered
  22/02/2009
Points
  135
26th February, 2010 at 05:16:13 -


Originally Posted by Muz
I could teach you in 10 minutes if I had a piece of paper. I'm pretty sure they can teach it, along with wherever you learn angles. Just that the teachers are too used to degrees. It's even easier than Centigrades and meters, because it's completely instinctive Maybe I'll write an article for you guys later with pictures and stuff.

Eh, floating point gets very accurate. MMF uses doubles, which is way more accurate. It's pixel perfect to at least 1000 pixels away, at least with my engine. Way more accurate than you get from just 360 degrees. The code in MMF/Lua should handle overflow and stuff, so it shouldn't happen.


well when you're dealing with floating point, .1 + .2 may or may not equal .3

it's true in many cases that loss of precision probably isn't going to hurt you but when you add == and ~= into the mix, things change

Image

Edited by Xgoff

 
Image
   

Post Reply



 



Advertisement

Worth A Click