Hey everyone. I need some help here. I've set up some code to make a cube rotate, following the mouse. Basically, it's similar to a turret. Later, i'll add some code to make the cube fire bullets. The cube will face the same direction the mouse faces, and thus, you can aim by putting the mouse over the target. But i'm having some trouble. The cube seems to bob itself every time i try to move in certain locations, and when you face certain directions, it wont aim correctly
The code is in DBP
Controls:
Move the mouse around the screen to move the cube
Move the mouse to the edge of the screen to scroll around.
If anyone could help me, or explain the reason why this is occuring, your help would be greatly appreciated.
Rem Project: test_rotation
Rem Created: 2/4/2004 5:50:38 PM
Rem ***** Main Source File *****
sync on
sync rate 60
make object cube 1, 50
position object 1, 0, 0, 0
color object 1, rgb(200, 200, 200)
set object light 1, 1
set object ambient 1, 0
x# = 0
y# = 0
z# = -400
position camera x#, y#, z#
point camera 0, 0, 0, 0
bearing# = 0
azimuth# = 0
distance# = 300
mousedistancez#=1000
make matrix 1, 1000,1000,50,50
position matrix 1,-500,0,-500
randomize matrix 1,20
update matrix 1
do
mousex#=0
mousey#=0
if mousey()<=50 then mousey#=-2
if mousex()<=50 then mousex#=-2
if mousey()>=429 then mousey#=2
if mousex()>=589 then mousex#=2
bearing# = wrapvalue(bearing# - mousex#)
azimuth# = wrapvalue(azimuth# - mousey#)
x# = distance# * sin(azimuth#) * cos(bearing#)
z# = distance# * sin(azimuth#) * sin(bearing#)
y# = distance# * cos(azimuth#)
position camera x#, y#, z#
point camera 0, 0, 0, 0
set cursor 0, 0
print "x: ";x#;" y: ";y#;" z: ";z#
print "bearing: ";bearing#
print "azimuth: ";azimuth#
print "distance: ";distance#
print "mousex: ";mousex()
print "mousey: ";mousey()
pick screen mousex(),mousey(),mousedistancez#
pd#=get pick distance()
print "Distance from camera: ";pd#
px#=get pick vector x()
py#=get pick vector y()
pz#=get pick vector z()
wx#=px#+camera position x()
wy#=py#+camera position y()
wz#=pz#+camera position z()
anglex#=-atan(py#/pz#)
angley#=atan(px#/pz#)
print "xrotation: ";anglex#
print "yrotation: ";angley#
rotate object 1,anglex#,angley#,0
sync
loop