if cam.W = 1
cam.z# = cam.z# + 0.5
endif
if cam.A = 1
cam.x# = cam.x# - 0.5
endif
if cam.S = 1
cam.z# = cam.z# - 0.5
endif
if cam.D = 1
cam.x# = cam.x# + 0.5
endif
cam.y# = 0
move camera cam.z#
rem position camera cam.x#, cam.y#, cam.z#
cam.x# = 0
cam.y# = 0
cam.z# = 0
mx = mx + mousemovex()
my = my + mousemovey()
xrotate camera my/2
yrotate camera mx/2
You are only increasing the x and z values by 1 for every cycle the key is pressed. Which is on the right track, but ideally you will want to increase them by a
ratio that is determined by the angle of the camera.
Start by thinking of the new coordinate as a vertex on a 90 degree angle triangle. Its first vertex is the camera's current position (x#,z#), the second vertex is the point we are trying to calculate (newx#,newz#) and the angle between the horizontal leg and the longest leg is the camera's Y angle:
What you need is Trigonometry for Sine=sin() and Cosine=cos():
First rule is:
sin( angle# ) = opposite# / hypotenuse#
Use algebra to find opposite:
opposite# = hypotenuse# * sin( angle# )
Secondly:
cos( angle# ) = adjacent# / hypotenuse#
Algebra again for the adjacent:
adjacent# = hypotenuse# * sin( angle# )
As you can see from the picture the horizontal leg is the same length as the distance into the x axis that we need to calculate, so:
adjacent# = newx# - x#
so
newx# = x# + adjacent#
The same holds true for the opposite, or the distance on the z axis:
opposite# = newz# - z#
so
newz# = z# + opposite#
So now we just plug in the trig portion to end up with:
newx# = x# + (hypotenuse# * sin( angle# ))
newz# = z# + (hypotenuse# * cos( angle# ))
So uh, well, what is the hypotenuse#? Well if the hypotenuse is zero then the new point will just equal the old point. Right? And if the hypotenuse is one, then newx# will be sin(angle#) and newz# will be cos(angle#). So in effect the hypotenuse is the distance we want to move.
So what actually happens when the angle changes? Well since
sin(angle#) is a ratio of the opposite to the hypotenuse, it is pretty apparent that the angle will change when the opposite leg changes in length and the hypotenuse remains the same. So in reverse, if the angle changes, the opposite will change with it according to the ratio of
sin(angle#). Same is true for cosine, only with adjacent and hypotenuse being the players.
So how can you use this in your code? Just like this:
set display mode 1024, 768, 16
sync on : sync rate 120
type camctrl_type
W as integer
A as integer
S as integer
D as integer
a#
x#
y#
z#
endtype
cam as camctrl_type
load object "Model.x", 1
load image "Model_Tex.png", 1
Texture Object 1,1
position object 1,0,0,0
hide mouse
position mouse 400,300
do
cam.W = keystate(17)
cam.A = keystate(30)
cam.S = keystate(31)
cam.D = keystate(32)
cam.x# = camera position x()
cam.y# = camera position y()
cam.z# = camera position z()
cam.a# = camera angle y()
moveSpeed# = 0.5
if cam.W = 1
cam.x# = cam.x# + moveSpeed# * sin( cam.a# )
cam.z# = cam.z# + moveSpeed# * cos( cam.a# )
endif
if cam.A = 1
cam.x# = cam.x# + moveSpeed# * sin( cam.a# - 90.0 )
cam.z# = cam.z# + moveSpeed# * cos( cam.a# - 90.0 )
endif
if cam.S = 1
cam.x# = cam.x# - moveSpeed# * sin( cam.a# )
cam.z# = cam.z# - moveSpeed# * cos( cam.a# )
endif
if cam.D = 1
cam.x# = cam.x# - moveSpeed# * sin( cam.a# - 90.0 )
cam.z# = cam.z# - moveSpeed# * cos( cam.a# - 90.0 )
endif
cam.y# = 0
rem move camera cam.z#
position camera cam.x#, cam.y#, cam.z#
mx = mx + mousemovex()
my = my + mousemovey()
xrotate camera my/2
yrotate camera mx/2
set cursor 0,0
print "FPS: ", screen fps()
sync
loop