forums

TNT Basic Forums > Programming Q&A
An easy way to get Theta?
< Last Thread     Next Thread >
Author
Thread        Post A Reply

08-31-2003 08:00

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

Sorry for yet another math question, but this one has stumped me for quite a while. How do you get the Theta (The angle between two other angles measured in degrees) as quickly and painlessly as possible? Are there any shortcuts to it?

08-31-2003 14:11

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Am I incredibly stupid or is it ( angle1 + angle2 ) / 2 ?

08-31-2003 18:43

Posted by:
Wil Hostman

Find more posts by Wil Hostman

That's bisection....

If He wants the difference:

// this section finds the difference, but won't count around the 0.
If A1 < a2
theta = a2 - a1
else if a1> a2
theta = a1 - a2
end if
// this section corrects for crossing the zero, by taking the inverse angle
if theta > 180
theta = 360 - theta
end if

08-31-2003 20:14

Posted by:
JAS

Click Here to Email JAS   Find more posts by JAS

Curious. What would this be used for?

08-31-2003 21:52

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

Thanks a bunch Will. I don't think I would have EVER figured that out...

JAS, I needed this so that I could make fluid motion for a couple heat seeking missiles in my game. I used Dunlab's angles example to find out what angle the missile needed to face to get to the target, however I wanted the missile to smoothly turn towards the target, not just snap instantly toward the target.

That's why I needed this function, the missile will now turn faster towards the correct direction depending on how far off it is from the target.

09-01-2003 01:34

Posted by:
JAS

Click Here to Email JAS   Find more posts by JAS

Impressive! Good Luck!

09-01-2003 05:28

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

I have been playing around with the code posted above by Wil Hostman, and I ran into a problem. It doesn't seem to be able to find the shortest distance when both angles are on opposite sides of the 0. My missile starts "snaking" when it's target is around the 0 angle. Weird.

09-01-2003 10:49

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

After hours of searching I ended up finding this.

'INT theta
theta=a1-a2
theta=ABS (MOD (theta,360))
IF theta > 180 THEN theta=360-theta"
"

This code gives the correct amount of rotation to get to the destination, however it will only count in positive numbers. I now need to know if there is a way to take two angles, and spit out the "direction" required to rotate to get to that destination. Any ideas on this one anyone?

09-01-2003 18:26

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Shouldn't that be simpler? Why not just take the difference between the two angles?

int difference=a2-a1 'this means that a1+diffference=a2

09-01-2003 20:07

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

That wouldn't work because it doesn't account for the fact that angles wrap around at 0 and 360. It's complicated and I don't understand the concept very well, but I do know that simply subtracting the two angles does not work in this case.

09-02-2003 02:14

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

But the rotate sprite command will accept any number, negative or over 360.
Try it, I think it will work.

09-02-2003 02:50

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

I already tried that method, however I just went back and double checked to make sure just taking angle1 and subtracting it by angle2 will work. It doesn't. I'm throwing together some example code to show exactly what I am trying to accomplish here.

09-02-2003 03:18

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

http://home.earthlink.net/~cubefusion/downloads/theta.sit

Do you see how the missile rotates when it is near the 0/360 mark? It stops and turns around even when the closest direction is just a couple degrees away.

If you remove the mark command from these two lines here...

' theta=ABS (MOD (theta,360))
' IF theta > 180 THEN theta=360-theta


The missile will cross over the 0/360 barrier, but it will only rotate in one direction. I now need a way to figure out which direction to rotate the missile.

09-03-2003 04:01

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

Here, this seems to work:

PROCEDURE FindTheta(FLOAT a1,FLOAT a2)
FLOAT theta
theta=a1-a2
'Unrem these two lines
theta=(MOD (theta,360))
IF theta > 180
theta=360-theta
else if theta<-180
theta=theta+360
end if

END PROC (theta)

09-03-2003 05:06

Posted by:
CubeFusion

Location:
California, USA

Find more posts by CubeFusion

That works perfectly eekaydee, Thank you!

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.