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:
rem Init app
sync on : sync rate 60
backdrop on
set text transparent
rem CAMERA SETTING
autocam off
cam_x# = 340
cam_y# = 794
cam_z# = 355 rem 440
position camera cam_x#,cam_y#,cam_z#
rotate camera 90,0,0
rem VARIABLES SETTING
man_x# = 500 rem green cube x
man_z# = 500 rem green cube z
target_x# = 240 rem red cube x
target_z# = 688 rem red cube z
actual_angle# = 0 rem green cube angle
target_angle# = 0 rem angle between green and red cube
rem SETTING MAN CUBE (the green cube!)
make object box 1,50,5,50
color object 1,rgb(0,255,0)
rotate object 1,0,actual_angle#,0
position object 1,man_x#,1,man_x#
rem SETTING TARGET (the red cube)
make object box 2,50,5,50
color object 2,rgb(255,0,0)
rotate object 1,0,target_angle#,0
position object 2,target_x#,1,target_z#
DO
rem CALCULATING THE ANGLE BETWEEN THE GREEN AND THE RED CUBE
dx# = target_x#-man_x#
dz# = target_z#-man_z#
target_angle#=wrapvalue(atanfull(dx#,dz#))
rem MAKING THE GREEN CUBE ROTATE TO THE ANGLE BETWEEN GREEN AND RED CUBE
IF actual_angle# < target_angle#
actual_angle# = actual_angle# + 1
ENDIF
IF actual_angle# > target_angle#
actual_angle# = actual_angle# - 1
ENDIF
rotate object 1,0,wrapvalue(actual_angle#),0
rem MOVING THE RED CUBE (the target)
if upkey() = 1
rem ogg_z_pos = ogg_z_pos + 10
target_z# = target_z# +4 rem donna
endif
if downkey() = 1
rem ogg_z_pos = ogg_z_pos - 10
target_z# = target_z# -4 rem donna
endif
if rightkey() = 1
rem ogg_x_pos = ogg_x_pos + 10
target_x# = target_x# +4 rem donna
endif
if leftkey() = 1
rem ogg_x_pos = ogg_x_pos - 10
target_x# = target_x# -4 rem donna
endif
position object 2, target_x#,1, target_z#
rem TEXT ON SCREEN
text 10,10,"man angle = "+str$(actual_angle#)
text 10,30,"target_angle# "+str$(target_angle#)
text 10,70,"man_x "+str$(man_x#)
text 10,80,"man_z "+str$(man_z#)
text 10,110,"target_x "+str$(target_x#)
text 10,120,"target_z "+str$(target_z#)
text 10,150,"dx# "+str$(dx#)
text 10,160,"dz# "+str$(dz#)
rem Update screen
sync
rem End loop
loop
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!