Hi all,
I'm trying to make an orbit camera around a ship that is moving and rotating. With Right mouse down the mouse rotates the ship, and A or E key. QSZD key (azerty) moves the ship. With Alt key the camera moves.
My problem is that if the ship is in its initial position and orientation, the orbit cam works nicely. But, once the ship is not in initial orientation, the orbit cam is not in the same axis system. I tried many thing and read all MoveEntity, RotateEntity, etc descriptions but I can't figure out how to do.
Thank you for your help ! :)
(Many things in my code need to be optimized, its the begining of the project and I'm new to Blitz3D.)
;Freelancer by Tabb
; Camera position, angle values
Global fCam_x#,fCam_y#,fCam_z#,fCam_pitch#,fCam_yaw#,fCam_roll#,fCam_pitch2#,fCam_yaw2#,fCam_roll2# ; Current
Global fDest_cam_x#,fDest_cam_y#,fDest_cam_z#,fDest_cam_pitch#,fDest_cam_yaw#,fDest_cam_roll# ; Destination
Global mzs# = 0,mxs#,mys#
Global fAngle_cam# = 0,fHeight_cam# = 1, fZoom_distance_cam#=-4
; Graphics values
Const iWidth=1280,iHeight=720,iDepth=32
Graphics3D iWidth,iHeight
SetBuffer BackBuffer()
light=CreateLight()
tex=LoadTexture("tex0.bmp")
sphere=LoadMesh( "geosphere.3ds" )
PositionEntity sphere,0,0,7
ScaleEntity sphere,100,100,100
ScaleTexture tex,0.2,0.2
EntityTexture sphere,tex
; ---------------------------------------------------------------
; Player ...
; ---------------------------------------------------------------
Global ship = CreatePivot ()
Global shipMesh = LoadMesh ("msh/ship.x", ship)
MoveEntity shipMesh, -2.5, 0, -1
ScaleEntity shipMesh, 0.5, 0.5, 0.5
EntityRadius ship, 1.6
EntityShininess shipMesh, 1
; ---------------------------------------------------------------
; Attach camera to player ...
; ---------------------------------------------------------------
camera=CreateCamera()
pivot_cam=CreatePivot()
CameraViewport camera,0,0,iWidth,iHeight
PositionEntity camera, 0, EntityY (pivot_cam) + 1, -4
EntityParent pivot_cam, ship
EntityParent camera, pivot_cam
PositionEntity ship, 10, 50, -200
; ---------------------------------------------------------------
; Main loop ...
; ---------------------------------------------------------------
While Not KeyHit(1)
;ship zoom
If KeyDown(74)=True Or MouseZSpeed() < 0 Or KeyDown(78)=True Or MouseZSpeed() > 0 Then ship_zoom(camera,ship)
If EntityDistance (camera,ship) > 20 Then MoveEntity camera,0,0,0.1
If EntityDistance (camera,ship) > 30 Then MoveEntity camera,0,0,5
;fZoom_distance_cam = EntityDistance (camera,ship)
; Rotate cam arround ship
If KeyDown(56)=True Then rotate_camera(camera,ship,pivot_cam)
; centering view
If KeyDown(46)=True Then
;PositionEntity camera, EntityX(ship), EntityY(ship), EntityZ(ship)
;PositionEntity pivot, EntityX(ship), EntityY(ship), EntityZ(ship)
RotateEntity pivot_cam,EntityPitch(ship),EntityYaw(ship),EntityRoll(ship),False
;PointEntity camera, ship,0
;MoveEntity camera, 0,0,-1
EndIf
; Ship rotate
If MouseDown(2) Or KeyDown(16) Or KeyDown(18) Then rotate_ship()
; Ship move
If KeyDown(17)=True Or KeyDown(31)=True Or KeyDown(57)=True Or KeyDown(45)=True Or KeyDown(32)=True Or KeyDown(30)=True Then move_ship(ship)
; Rest mouse position to centre of screen
MoveMouse iWidth/2,iHeight/2
UpdateWorld
RenderWorld
;Text 320,500,"Movement & Rotation"
Text 50,460,"X "+EntityX(camera)
Text 50,440,"Y "+EntityY(camera)
Text 50,420,"Z "+EntityZ(camera)
Text 50,400,"cam yaw "+EntityYaw(pivot_cam)
Text 50,380,"cam pitch "+EntityPitch(pivot_cam)
Text 50,360,"cam roll "+EntityRoll(pivot_cam)
Text 50,340,"ship yaw "+EntityYaw(ship)
Text 50,320,"ship pitch "+EntityPitch(ship)
Text 50,300,"ship roll "+EntityRoll(ship)
Flip
Wend
End
Function rotate_ship()
If MouseDown(2) Then
TurnEntity ship,0,-MouseXSpeed()/10,0
TurnEntity ship,MouseYSpeed()/10,0,0
EndIf
If KeyDown(16) Then
TurnEntity ship,0,0,1
ElseIf KeyDown(18) Then
TurnEntity ship,0,0,-1
EndIf
End Function
Function rotate_camera(camera,ship,pivot_cam)
fHeight_cam = fHeight_cam + MouseYSpeed()*0.25
fAngle_cam = fAngle_cam - MouseXSpeed()*0.25
TurnEntity pivot_cam,fHeight_cam,fAngle_cam,0
PointEntity camera,ship,fCam_roll
fAngle_cam=fAngle_cam/2
fHeight_cam=fHeight_cam/2
End Function
Function ship_zoom(camera,ship)
;ship zoom
If KeyDown(74)=True Or MouseZSpeed() < 0 Then
If EntityDistance (camera,ship) < 20 Then MoveEntity camera,0,0,-0.1
EndIf
If KeyDown(78)=True Or MouseZSpeed() > 0 Then
If EntityDistance (camera,ship) > 2 Then MoveEntity camera,0,0,0.1
EndIf
If EntityDistance (camera,ship) > 20 Then MoveEntity camera,0,0,0.1
If EntityDistance (camera,ship) > 30 Then MoveEntity camera,0,0,5
fZoom_distance_cam = EntityDistance (camera,ship)
End Function
Function move_ship(ship)
; Ship move
; Forward/backwards - destination camera move z values
If KeyDown(17)=True Then fDest_cam_z=1
If KeyDown(31)=True Then fDest_cam_z=-1
; Up/down - destination camera move y values
If KeyDown(57)=True Then fDest_cam_y=1
If KeyDown(45)=True Then fDest_cam_y=-1
; Strafe - destination camera move x values
If KeyDown(32)=True Then fDest_cam_x=1
If KeyDown(30)=True Then fDest_cam_x=-1
; Current camera move x, y and z values
fCam_z=fCam_z+((fDest_cam_z-fCam_z)/5)
fCam_y=fCam_y+((fDest_cam_y-fCam_y)/5)
fCam_x=fCam_x+((fDest_cam_x-fCam_x)/5)
; Move ship
MoveEntity ship,fCam_x,fCam_y,fCam_z
fDest_cam_x=0 : fDest_cam_y=0 : fDest_cam_z=0
End Function
|