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.

Code Snippets / [DBP] 2D rotate to point

Author
Message
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 12th Jan 2008 09:16 Edited at: 12th Jan 2008 09:17
Due to popular demand(from my fan club, i.e. the dude in the mirror) I'm posting this code here. In 2D, this code will turn(a line in this example) toward the mouse at 1 degree per frame maximum, this is useful for homing missiles, basic AI and what not.



This is the same code but showing how you'd implement a missile, or something that follows a point with a limited turn speed:



The angle between the grey lines is the Euler(global) angle between the observer(sphere) and the target(mouse), angle between the white lines is the local angle between the observer's direction to the target.

Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 14th Jan 2008 18:56
This worked great for me. One thing that it allowed me to easily implement was sight cones, and being able to tell whether another object is to the left or right of the source object's facing angle is is fantastic.

I'm using the 2D turn-to function for 3D, since the 3D model is only going to be rotated on a single axis anyway.


Come see the WIP!
Rabid Wombat
16
Years of Service
User Offline
Joined: 29th Feb 2008
Location:
Posted: 29th Feb 2008 07:30
Dark Coder: Amazing setup!! I was wondering if you would help me adapt that code for a turn based setting. I am building a turn based space fighter and I am have difficulty getting the sprites to turn face correctly. Because it is turn based the game loop does not continue until the rotation is complete. How can I make that work if I have the x and y positions of both attacker and target?

Login to post a reply

Server time is: 2024-11-22 13:39:22
Your offset time is: 2024-11-22 13:39:22