TGC Codebase Backup



RTS Visible Unit Rotation by Pyro_guy

29th Mar 2005 9:54
Summary

A peice of code used to slowly rotate objects towards the 3dCursor, like units in RTS games for example.



Description

A peice of code used to slowly rotate objects towards the 3dCursor, like units in RTS games for example.

I was having trouble getting objects to rotate through the shortest angle to reach the desired rotation, this code solves that problem.

The code uses a dummy object to find the desired angle by pointing it in the direction of the cursor. You could, however, use the ATANFULL method, but the 'Dummy' method has been used here for simplicity.

Special Thanks to:
Chris K, WOLF, Van-B, RiiDii, Benjamin, Aura
Forum topic here: http://forum.thegamecreators.com/?m=forum_view&t=50979&b=1&p=0



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    `RTS visible Unit Rotation code
`----------
`Submitted by Pyro_Guy : Gunhavok@gmail.com
`----------
`Special Thanks to:
`Chris K, WOLF, Van-B, RiiDii, Benjamin, Aura
`Forum topic here: http://forum.thegamecreators.com/?m=forum_view&t=50979&b=1&p=0
`----------

`Basic setup code, set resolution, camera position/rotation etc
autocam off ; set display mode 1024,768,32 ; position camera 500,50,0 ; rotate camera 45,0,0
ink rgb(255,0,0),rgb(0,0,0)

`Make the matrix, so we can see where are objects are in relation to each other
make matrix 1,1000,1000,50,50

`Make, position and color the objects we will use
`Object 1: 'Cursor' object
`Object 2: 'Unit' object
`Object 3: 'Dummy' Object
`------------------------
`Object 2 is semitransparent so you can see the dummy object.
`Normally, the dummy would not be visible
`Object 2 is also offset and scaled, so we can tell which way it is facing.
make object cube 1,1 ; position object 1,500,0,100
make object cube 2,2 ; scale object 2,100,100,200 ; position object 2,500,0,50 ; color object 2,rgb(0,0,255)
offset limb 2,0,0,0,object size x(2)/2 ; ghost object on 2
make object cube 3,1 ; color object 3,rgb(0,255,0) ; position object 3,500,0,50 ; point object 3,500,0,51

`Begin main loop
DO

`Cursor control.
`Use the arrowkeys to move the small cube
`Press Spacebar to point the dummy at the cursor object, and begin the 'Unit' rotation
position object 1,object position x(1)+(rightkey()-leftkey()),0,object position z(1)+(upkey()-downkey())
if spacekey()=1 then point object 3,object position x(1),0,object position z(1)

`Wrap angle values, so angles do not exceed 360+
yrotate object 2,wrapvalue(object angle y(2))
yrotate object 3,wrapvalue(object angle y(3))

`If angle between unit and dummy is greater than 180, rotate right +1 degree
`else, rotate left -1 degree
`you may change the +1 and -1 to the desired speed of rotation
if wrapvalue(object angle y(2)-object angle y(3)) > 180
   yrotate object 2,object angle y(2)+1
else
   yrotate object 2,object angle y(2)-1
endif

`If angle between unit and dummy is less than 2, then set unit angle to dummy angle
`This is to prevent the unit object from 'vibrating' back and forth if it cannot achive the
`exact angle of the dummy. Remove this line to observe what I mean.
`The Value here must be greater than the speed of rotation.
if ABS(object angle y(2)-object angle y(3)) < 2 then yrotate object 2,object angle y(3)

`Display Object and Dummy angles
text 10,10,"OBJECT ANGLE: " +str$(object angle y(2))
text 10,20," DUMMY ANGLE: "+str$(object angle y(3))

`End loop
LOOP