Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / yet another mathematical help

Author
Message
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 24th Aug 2004 20:23 Edited at: 24th Aug 2004 21:14
sorry for bugging ya again, while they still teach this stuff at school , the stuff i learn are quite different then the problem i need to solve in my game.

I tried finding a site that will explain these issues, but i haven't found any.

The problem goes like this :

There's a ship, which the angle (Y angle) between it, and the target, is for example 30 units.
Given intial speed,top speed,constant acceleration,distance from target, and constant turning speed.
How long will it take for the ship to face the target ?

so the simple stuff i learn in school like: time=distance/speed won't do much good here.

There's one more thing, it is quite plausable, that if the distance is short enough, the ship will never face the target, as it is going in circles. When that's the case, which i guess time#=indefinatly, i would need to know what speed(constant,with no acceleration) should be, for the ship to face the target.

Please help.
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 24th Aug 2004 21:26 Edited at: 24th Aug 2004 21:28
Hi again Zokomoko,
This one definetly is a bit tougher than your last question. One query before I put pen to paper:
When you say constant turning speed, what exactly do you mean? Constant angular speed? {I assume this, but just checking)

Also, I assume the angle Y is the angle between direction the ship is pointing to the direction of the target.
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 24th Aug 2004 21:29 Edited at: 24th Aug 2004 22:02
Yes

Thx so much for helping.

Yes, you're right about the Y angle.
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 25th Aug 2004 02:01
Sorry I haven't got back to you. Been a busy day. I have to say that this one is very tough indeed and it may need numerical integration since I'm not sure there's an easy analytical formula. There probably is one, but it's about 10 times tougher to come up with than your first problem which was simply a straight line. If one does exist, I hope I can find it tonight and I'll post it tommorrow, otherwise, it's integration I'm afraid :-( .
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 25th Aug 2004 02:30 Edited at: 25th Aug 2004 02:30
what is numerical integration ? (i'm not familiar with all the mathmetical english terms)

I don't know what that is, but at least it's doable (since you suggested).

My first thought on this, was to do a simple "time=turningspeed/totalangle".
But since the ship will move when it's turning, it will change its angle also, the solution would be adding the amount of angle change to the equation.
But figuring the amount of change is the tricky thing.
matt rochon
21
Years of Service
User Offline
Joined: 15th Mar 2003
Location: Canada
Posted: 25th Aug 2004 05:02 Edited at: 25th Aug 2004 05:03
if you just want to know the time it takes to face the target and the target is a static object just use the turning speed and the total distance to turn as the angles will always be the same
Mentor
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 25th Aug 2004 06:37
thats not strictly true since if you move as you turn then the angle is going to change to the target, one way would be to scale the speed down the nearer the target is, so that you can complete the turn before you overshoot, the other way needs me to look up the math, but you can solve it for a circle with the ship at a tangent to the edge and the target at the edge as well, will look it up tommorow if you havent solved it by then, <bookmark this post>

Mentor.

PC1: P4 3ghz, 1gig mem, 2x160gig hd`s, Radeon 9800pro w cooler (3rd gfx card), 6 way speakers.
PC2: AMD 2ghz, 512mb ram, FX5200 ultra, 16 bit SB.
Mini ATX cases suck.
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 25th Aug 2004 13:32 Edited at: 25th Aug 2004 13:40
It looks it's going to be a really complicated equation.
So maybe that can be spared.
I'll tell you why I need it for, and maybe there is a better, more efficent way to do it.

The AI needs to go from its position to target.
(1)If the FPS is low, it will miss the target (it the distance between them).
(2)If the target is too close, it will circle around the target.


The two solutions I came up with are:
(1)The AI will predict how much time it will take to reach the target. If it has passed that time, then position him at target's location.
(2)if the time is infinite, lower the speed.

Maybe there're more efficent solutions then the ones i came up.
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 25th Aug 2004 18:59 Edited at: 25th Aug 2004 19:09
I've had a good think about this, and I just don't think there's an analytical solution with a nice easy to use formula at the end of it sorry, so here goes with the integration (at least my explanation). I think Phaelax suggested something like this for your last problem, but this time I think you need it.

ok, the way integration works is by chopping up the time sequence into little bits and adding the motion together as the particle moves. If we chop the bits of time into small segments, dt#, and wanted to follow the motion of the particle for a total time time#, then we may use something like this (for 1-D motion, i.e. in a straight line)


If you wanted to do like you did before and cut the acceleration once it reached a maximum speed then you could modify this to



If that makes sense, then I'll come back and finish it off. Oh yeah, that's the other thing. Is your motion restricted to a 2-D plane or is it fully 3-D? If it's fully 3-D, then it complicates things further
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 25th Aug 2004 19:58
the motion could be considered restricted to 2-d plane.
you can go up and down, but it doesn't affect the 2-d plane.

I understand what you mean by numerical intergration.

something like this:
`-------------------------
store ship position and anlges
repeat
move ship to the target
until distance between ship to target is low
time=time has passed (it won't be a probelm getting the time)
restore ship position and angles
`-------------------------

this is good to get the total time it will take to reach target, but it doesn't solve problem num 2.
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 25th Aug 2004 20:46
ok, I think I know how to do it in my head, but I'm a little tied down with work at the moment again sorry, so hopefully I can write down what I'm doing and post it here a little later. I might send a diagram, since it might help explain the terms.

I assume you want to know
1) if you know the linear speed and the angular speed, will you be able to turn and face the target i.e. what is the maximum speed you can travel to ensure you will face the target rather than go in circles around it.
2) if you are accelerating, at what point should you stop accelerating in order to ensure you face the target.
Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 25th Aug 2004 21:05
Yes, exactly.
That would completly solve the problem .
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 27th Aug 2004 04:19 Edited at: 27th Aug 2004 04:28
*edit* - just realised, that what I've written below doesn't actually answer your initial question - which was how long does it take to reach the target. What it should do (if it's correct) though, is enable the computer to control the ships velocity and angular velocity so that it reaches the target in the least possible time. Hope that would be useful.


Woah - good physics question! I just had a scribble - and I agree with what's been said above, that it's not as easy as the last one! However - give this a try.

##disclaimer - don't blame me if this goes completely wrong!##

Assuming that:

Y# is the angle between the ships direction, and the line joining the ship to the target

w# is the ship's angular velocity (in degrees per second),

d# is the distance between the ship and the target,

v# is the ships actual current velocity (speed).

a# is the ships acceleration

Then, for the ship to intercept it's target, the required velocity (speed) of the ship, which we'll call "RV#" should be equal to this value:

RV#=(w#*(180-2*(90-Y#)))/(d#*(90-Y#)).

You could then include an IF - THEN statement into the program loop like this:



then on the next program loop, the values of Y# and d# will have changed, and it will recalculate and adjust the velocity accordingly.

Now, I know you said it had a constant angular velocity, but for added realism, you could also make the object turn quicker or slower by adjusting the angular velocity between 0 and the maximum allowed value. You'd need to define a constant 'angular accleleration' if you haven't already "aa#"- ie. how quickly the angular velocity is able to increase. Then add this into the previous if statemets:



Hope that works. I'm reasonably confident that the programming logic is correct - but I'm 50-50 on whether I derived the equation correctly! Let me know anyone if you want to see my derivation.



Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 27th Aug 2004 05:27
Thx for trying to help.

Now, to your equation.

RV#=(w#*(180-2*(90-Y#)))/(d#*(90-Y#)).

i'm assuming by RV# meaing the required velocity, is it the maximum velocity calculated from the start of the movement (i.e it's suppose to be calculated one time)?

So let me ask if i get this correctly:
one possibility is that the RV# represent the maximum velocity of which the ship could travel without circling, from the point in time it starts moving toward target.
or, RV# is set to be calculated every loop,
according to a specific loop's ship's speed and anlge, it will define for that loop, the maximum velocity for that ship.

Most probable it's the second, so i'll test it now.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 27th Aug 2004 05:59 Edited at: 27th Aug 2004 06:04
The second - calculated each loop so that the velocity and turn speed are gradually adjusted. I'm still a bit uncertain whether this is going to work, though! If it doesn't, I'll show you the derivation and someone may be able refine it.

p.s. - have you tried looking on the web under nautical navigation principles? This is the sort of calculation I expect a ship's navigator would have to know how to do.


Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 27th Aug 2004 14:47
It doesn't work.
When the ship faces the target, the outcome is 0.
When the ship doesn't face the target, the outcome is between 0-9.

- i'll try now.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 27th Aug 2004 15:30



Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 27th Aug 2004 16:39
there's still hope.
you should post how you got to the equation, and maybe we'll see how to correct it
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 27th Aug 2004 18:09 Edited at: 27th Aug 2004 18:12
Ah - I missed out the 'sines': I think it should be:

RV#=(w#*sin(-2*Y#))/(d#*sin(90-Y#))

give it a try - and I'll bang out a bit of code to try it my end as well.

I'll post the derivation too, but I'll need to write it in neat because it's all scribbles at the moment!


Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 27th Aug 2004 18:34 Edited at: 27th Aug 2004 19:15
Sorry for the delay. Works been getting on top of me here . I see Ric has appeared once again to do battle (yes, we have differing answers again
I've got point 1 figured out, i.e. the maximum linear speed so that you can face the target

Using the same notation as Ric, the required speed is

RV# <= d#*w#/(2*sin(Y#))

If the ship is travelling exactly at this speed, i.e. will turn around just enough to hit/reach the target, then the time it will take is

time# = 2*Y#/w#

Note this time is just for this special case, in general, you'll have to use integration as I said earlier

I have a derivation written out, but the scanner is a bit tied up here at the moment.

I guess this kind of works with point 2 also. You keep accelerating until you either reach this velocity and hence stop accelerating, or you reach your maximum speed. Note that you have to check this formula at every time step in the integration since both d# and Y# will change as you move around. What you need now is a little bit of code to get it working. I'll be back (I hope )
Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 27th Aug 2004 18:44 Edited at: 27th Aug 2004 18:58
Just noticed that Ric has defined the Angular velocity in degrees per second whereas it should be in radians per second, as I have considered it. But since DB works in degrees, then I guess we should keep things in degrees, but remembering the conversion factors. There are 2*pi radians in a circle so 1 rad = 57.3 degrees. Therefore

w#(degrees per second) = w#(radians per second)*57.3

Just to make sure we get our notation and definitions correct.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 27th Aug 2004 19:23
Hi Code - Yes I forgot to convert to radians! How did you do your derivation - using a tangent to a circular path?

I assumed it's path to the target is the tangent to a circle whose radius is continually decreasing, and where the angular velocity of the ship's rotation should equal the angular velocity of the circle it sweeps out. Then I applied trig to the triangle joining the points of the circle. I'll post it later so we can see where we differed.




Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 27th Aug 2004 20:19
Hi Ric
The way I did it was to calculate the radius of the circle that the ship would follow
r#=v#/w#
and then calculate the component of the radius vector towards the target
s#=r#sin(Y#)
If the ship is to face the target, then the circle must come inside the starting point and the target, which happens if
2*s#<=d#
2*v#*sin(Y#)/w# =< d#
v# <= d#*w#/(2*sin*(Y#))

Does that make sense? I would draw and scan in a diagram, but the computer with the scanner is taken at the moment . I tried integrating over an accelerating ship, but I think it's not possible, and at the very least, stupidely difficult.
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 28th Aug 2004 00:43 Edited at: 28th Aug 2004 01:13
Yes - that makes perfect sense. In fact, I did it in a slightly more long winded way, and having double checked it, I neglected to carry forward a negative sign. (Typical lack of sleep type error!) With that corrected, guess what......



It's the same (The same result from two seperate methods has to be reassuring!)

I tried to write a bit of code to test it (it'll also be useful for a project I'm doing too) - but I couldn't figure out how to return the angle Y in code. Any ideas anyone? Zoko - how did you get it?


Code Machine
20
Years of Service
User Offline
Joined: 21st Jul 2004
Location: United Kingdom
Posted: 28th Aug 2004 01:26 Edited at: 28th Aug 2004 01:27
Great! Yeah, it is nice when 2 separate methods get the same answer.

About the angle problem, in 2-D it is fairly straight forward to get the angle I think. You can use the dot product of two vectors. If you have a velocity vector
v = vxi + vyj
and a displacement vector between the ship and the target
r = rxi + ryj
and we know the magnitude of the 2 vectors
i.e. |v| = sqrt(vx^2 + vy^2)
and |r| = sqrt(rx^2 + ry^2)
then the dot product is
v.r = vx*rx + vy*ry = |v||r|sin(T)
therefore
sin(T) = (vx*rx + vy*ry)/( |v||r| )
T = arcsin((vx*rx + vy*ry)/( |v||r|) )

This should work I feel, although maybe quite long-winded since I'm sure there are some simpler ways of doing it. I think I remember seeing some DBPro commands which do dot products, but this isn't terribly expensive computationally. Right, hometime
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 28th Aug 2004 02:49
Well, I think I got the angle out as:

Y=90-objectangle+atan((z1-z2)/(x1-x2))

But having tried to put the whole thing into code (below), I can't get it working - mind you, I haven't done a degrees to radians conversion. But anyway, I think I'm giving up for the moment. I hope you have better luck Zoko.




Zokomoko
22
Years of Service
User Offline
Joined: 23rd Nov 2002
Location:
Posted: 28th Aug 2004 03:53
get angle Y :

function bearing(o1,o2)
a#=atanfull(object position x(o2)-object position x(o1),object position z(o2)-object position z(o1))
b#=-wrapvalue(object angle y(o1)-a#)
if b# < -180 then b#=360.0+b#
endfunction b#

what are radians ?
Thx for the diagram, but we only start learning about trig this year, but having dealing with them (sin,cos) somewhat, i've a fairly good idea what they are and how they can be used.

Quote: "d#*w#/(2*sin*(Y#))"

will this equation give the top speed without circling ?
but it doesn't include acceleration ?
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 28th Aug 2004 08:10
Quote: "d#*w#/(2*sin*(Y#))

will this equation give the top speed without circling ?
but it doesn't include acceleration ? "


Yes, in theory. And yes, your ship will be accelerating or decelerating.

It should give you the required speed to intercept the target at any particular time. You then recalculate the new required velocity each loop (because the distance and angle will be continually changing), and program the computer to adjust the velocity of the ship (ie. accelerate or decelerate) to match the required velocity.

Having said that, I still can't get it to work.

Btw, radians are another unit for measuring angles in a circle, instead of degrees. 1 radian is about 57 degrees. One of the equations of circular motion that we used above (ang.velocity = velocity/radius) is derived from another equation which assumes the angle of a circle is in these radian units. DBPro is set to use degrees, so a conversion factor is probably needed in the equation somewhere. Probably ehy I cann't get it working.

hmmmm..........

try RV#=d#*w#/(2*(sin*(Y#/57)*57))

I'm guessing a bit, Code Machine might be able to correct that if it's wrong.




Login to post a reply

Server time is: 2024-11-27 08:38:04
Your offset time is: 2024-11-27 08:38:04