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.

DarkBASIC Professional Discussion / Rotating object1 to let him face object2!

Author
Message
Alduce
22
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 26th Oct 2013 15:46 Edited at: 26th Oct 2013 15:50
Hi all!
I made a little code to make my green cube rotate to face a red cube.

1) My green cube has an angle
2) I calculate an angle between the green cube and the red cube: the target angle
3) I make rotate my green cube to the target angle.

Here the code:



You can notice my program work fine until the target_angle# switch from 359° to 0° or from 0° to 359...
when there is this switch so my green cube don't choosing the nearest way to rotate to face the red cube but it is choosing the most distant way (to rotate in the opposite way..) to reach the target angle!...
I know this is a damn basic question but today simply I can't to archive this!!

Please help!
I need a code modify to let my green cube always choosing the near way to reach target angle!
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 26th Oct 2013 17:16 Edited at: 26th Oct 2013 17:29
All you need to do is check if the target angle is ever lesser or greater than the actual angle by 360. If it has then we can assume the target angle has flipped, so we just make our actual angle equal our target angle.

So...


Full code:


For making the angles rotate towards each other over time, look into lerp functions and the percentage of the lerp is the rate you want to move it at. Slerp better for rotation!

"Get in the Van!" - Van B
Alduce
22
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 26th Oct 2013 17:45
Ahhh really really thank you Sasuke!! This thing was making me krazy!
THANK YOU!
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 27th Oct 2013 01:58 Edited at: 27th Oct 2013 03:11
Whoops, made a mistake. All this does is make the actual angle the target angle all the time. Gimme a sec to sort this...

EDIT
This seems to work better as it actually slopes towards the target rather than just stays on the target:


"Get in the Van!" - Van B
Alduce
22
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 27th Oct 2013 09:12 Edited at: 27th Oct 2013 09:15
Interesting different version!
Now I just thinking what of these 2 I need to use..
and I think second example is more near from what I had in my mind.

Sasuke, can you modify your last code with a 360° format instead the 180/-180°?
I trying to make this modify with just funny results...
Seems angles are my worst ghosts...
Sasuke
19
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 27th Oct 2013 10:14
For rotation like this we need it in the range of -180/180 because it's easier to work out direction and the math to deal with that direction. When it comes to Euler angles, combining/translating them is a pain. Quaternions are the best for this type of rotation but difficult to understand especially if your just looking at code. (there's some great vids on youtube that explain them)

Anyway, if you want a result from the range 0-360, just wrapvalue() the resulting angles, example:


"Get in the Van!" - Van B
Alduce
22
Years of Service
User Offline
Joined: 26th Oct 2002
Location: Rama spaceship
Posted: 27th Oct 2013 11:13
Perfect, just I let the angle calcs made by the 180/-180 method and when I need to make some check angle calculations just I will to use wrapvalue!.

Thanks a lot Sasuke!

Login to post a reply

Server time is: 2025-05-15 23:44:37
Your offset time is: 2025-05-15 23:44:37