FP Camera

Tubulii

03-07-2010 16:58:56

Hi,

I converted the code for a First-Person-Camera to VB but it does not work like exspected.
The camera falls onto the ground but I cannot move it(= the camera, WASD)

Public Class Player
Public noclip As Boolean
Private mSceneMgr As Mogre.SceneManager
Private mWorld As MogreNewt.World
Private mCamera As Mogre.Camera
Private mRotate As Single = New Single()
Private mMove As Single = New Single()
Private jump_power As Single = New Single()
Private poo2 As Vector3 = New Vector3()
Private acel As Vector3 = New Vector3()
Private force As Vector3 = New Vector3()
Private gravity As Single = New Single()
Private camera_rotation_x As Single = New Single()
Private camera_rotation_y As Single = New Single()
Private camera_rotation_xa As Single = New Single()
Private camera_rotation_ya As Single = New Single()
Private y_rotation_cont As Single = New Single()
Private x_rotation_cont As Single = New Single()
Private y_limit_a As Single = New Single()
Private y_limit_b As Single = New Single()
Private mCamNode As SceneNode
Private mViewNode As SceneNode
Private cam_height As Single = New Single()
Private fps_speed As Single = New Single()
Private bod As MogreNewt.Body
Private size As Mogre.Vector3 = New Mogre.Vector3()
Private mDirection As Vector3 = New Vector3()
Private direction As Vector3 = New Vector3()
Private stra As Vector3 = New Vector3()
Private jump As Boolean
Private no_movement As Boolean
Private MyVel As Mogre.Vector3 = New Mogre.Vector3()
Private inertia As Mogre.Vector3 = New Mogre.Vector3()
Private WithEvents _game As Game
Public Sub New(ByVal Game As Game, ByRef scene As Mogre.SceneManager, ByRef world As MogreNewt.World, ByRef cam As Mogre.Camera)
If scene Is Nothing Then
Throw New Exception("SceneMgr : assertion failed at LoadMap::init")
End If
mSceneMgr = scene
_game = Game
If world Is Nothing Then
Throw New Exception("World : assertion failed at PhysObject::init")
End If
mWorld = world
If cam Is Nothing Then
Throw New Exception("World : assertion failed at PhysObject::init")
End If
Dim ent As Mogre.Entity

mCamera = cam
mCamNode = mSceneMgr.RootSceneNode().CreateChildSceneNode()
mCamNode.Position = (New Vector3(0, 100, 0))
mViewNode = mCamNode.CreateChildSceneNode()

mViewNode.AttachObject(mCamera)
mDirection = Vector3.ZERO
noclip = False
no_movement = True
mRotate = 0.13
mMove = 250
gravity = 10
y_rotation_cont = 0
y_limit_a = 90
y_limit_b = -90
jump_power = 5
fps_speed = 3
size = New Vector3(2, 2, 2)
mCamNode.SetScale(size)
Dim col As MogreNewt.Collision = New MogreNewt.CollisionPrimitives.Ellipsoid(mWorld, size, 1111)
bod = New MogreNewt.Body(mWorld, col)
bod.AttachNode(mCamNode)
bod.SetPositionOrientation(mCamNode.Position(), Mogre.Quaternion.IDENTITY)
col = Nothing
inertia = 40 * size
bod.SetMassMatrix(40, inertia)
AddHandler bod.ForceCallback, AddressOf camera_force_callback
'bod.setCustomForceAndTorqueCallback(Of Player)(Player.camera_force_callback, Me) ' add the previous defined callback function as the body custom force and torque callback
'bod->setStandardForceCallback();
bod.AutoSleep = (0)
Dim uv2 As MogreNewt.BasicJoints.UpVector = New MogreNewt.BasicJoints.UpVector(mWorld, bod, Vector3.UNIT_Y)
jump = False
End Sub
Public Sub camera_force_callback(ByVal Bod As MogreNewt.Body, ByVal Timestep As Single, ByVal threadID As Integer)

Dim camPlayer As Player = CType(Bod.UserData(), Player)
Dim mass As Single = New Single
Dim yaw As Mogre.Radian = New Mogre.Radian()
Dim inertia As Mogre.Vector3 = New Mogre.Vector3()
Dim qb As Mogre.Quaternion = New Mogre.Quaternion()
Bod.GetMassMatrix(mass, inertia)
force = New Vector3(0, gravity, 0)
qb = mViewNode.Orientation()
direction = (New Quaternion(New Degree(x_rotation_cont), Vector3.UNIT_Z) * Vector3.NEGATIVE_UNIT_Z)
Dim V0 As Mogre.Vector3 = Bod.Velocity()
Dim V1 As Mogre.Vector3 = direction * mDirection
acel = (V1 - V0)

force.x = acel.x
'force.y = acel.y;
force.z = acel.z
force *= mass
Bod.AddForce(-force)
Bod.AddForce(New Vector3(0, 1, 0))
bod.Omega = (New Vector3(0, camera_rotation_x, 0)) 'to rotate camera in x (this also don't work)
'bod->setPositionOrientation(get_body_position(bod),mViewNode->getOrientation()); - this don't work:(
End Sub

Public Function get_body_orientation(ByRef bod As MogreNewt.Body) As Mogre.Quaternion

Dim orient As Quaternion = New Quaternion()
Dim pos As Vector3 = New Vector3()

bod.GetPositionOrientation(pos, orient)

Return orient

End Function
Public Function get_body_position(ByRef bod As MogreNewt.Body) As Mogre.Vector3

Dim orient As Quaternion = New Quaternion()
Dim pos As Vector3 = New Vector3()

bod.GetPositionOrientation(pos, orient)

Return pos

End Function
Public Sub FCPress(ByVal arg As MOIS.KeyEvent) Handles _game._KeyPressed
If noclip Then
Select Case arg.key
Case MOIS.KeyCode.KC_UP, MOIS.KeyCode.KC_W
mDirection.z = -fps_speed * 30

Case MOIS.KeyCode.KC_DOWN, MOIS.KeyCode.KC_S
mDirection.z = fps_speed * 30

Case MOIS.KeyCode.KC_LEFT, MOIS.KeyCode.KC_A
mDirection.x = -fps_speed * 30

Case MOIS.KeyCode.KC_RIGHT, MOIS.KeyCode.KC_D
mDirection.x = fps_speed * 30

Case Else
End Select
Else
'FPS Mode
Select Case arg.key
Case MOIS.KeyCode.KC_UP, MOIS.KeyCode.KC_W
mDirection.z = -fps_speed * mMove * 30

Case MOIS.KeyCode.KC_DOWN, MOIS.KeyCode.KC_S
mDirection.z = fps_speed * mMove * 30

Case MOIS.KeyCode.KC_LEFT, MOIS.KeyCode.KC_A
mDirection.x = -fps_speed * mMove * 30

Case MOIS.KeyCode.KC_RIGHT, MOIS.KeyCode.KC_D
mDirection.x = fps_speed * mMove * 30

Case Else
End Select
End If
End Sub
Const ROTATE As Single = 0.003
Public Sub MouseMove(ByVal arg As MOIS.MouseEvent) Handles _game._MouseMove
camera_rotation_x = -mRotate * arg.state.X.rel
camera_rotation_y = -mRotate * arg.state.Y.rel
camera_rotation_xa = -mRotate * arg.state.X.abs
camera_rotation_ya = -mRotate * arg.state.Y.abs
mViewNode.Yaw(New Degree(camera_rotation_x), Node.TransformSpace.TS_WORLD)
mViewNode.Pitch(New Degree(camera_rotation_y), Node.TransformSpace.TS_LOCAL)
y_rotation_cont += camera_rotation_y
x_rotation_cont += camera_rotation_x
If x_rotation_cont >= 360 Then
x_rotation_cont = 0
End If
If y_rotation_cont > y_limit_a OrElse y_rotation_cont < y_limit_b Then
mViewNode.Pitch(New Degree(-camera_rotation_y))
y_rotation_cont -= camera_rotation_y
End If
End Sub
Public Sub FCRelease(ByVal arg As MOIS.KeyEvent) Handles _game._KeyReleased

Select Case arg.key
Case MOIS.KeyCode.KC_UP, MOIS.KeyCode.KC_W
mDirection.z = 0
bod.Velocity = New Vector3(0, 0, 0)

Case MOIS.KeyCode.KC_DOWN, MOIS.KeyCode.KC_S
mDirection.z = 0
bod.Velocity = (New Vector3(0, 0, 0))

Case MOIS.KeyCode.KC_LEFT, MOIS.KeyCode.KC_A
mDirection.x = 0
bod.Velocity = (New Vector3(0, 0, 0))

Case MOIS.KeyCode.KC_RIGHT, MOIS.KeyCode.KC_D
mDirection.x = 0
bod.Velocity = (New Vector3(0, 0, 0))

Case Else
Exit Select
End Select ' switch

End Sub
Public Sub FCUpdate(ByVal evt As FrameEvent) Handles _game._FrameStarted

If noclip Then
acel = New Vector3(0, 0, 0)
bod.Velocity = New Vector3(0, 0, 0)
mViewNode.Translate(mDirection * evt.timeSinceLastFrame, Node.TransformSpace.TS_LOCAL)
mCamNode.Position = (mViewNode.Position())
Dim pos As Vector3 = New Vector3()
Dim orient As Mogre.Quaternion = New Mogre.Quaternion()
bod.GetPositionOrientation(pos, orient)
bod.IsFreezed = True
bod.SetPositionOrientation(mCamNode.Position(), Mogre.Quaternion.IDENTITY)
Else
bod.IsFreezed = False
End If
End Sub


End Class


Note: the strange camera movement is not the problem, I have already a solution for that and the Game class is my creation
Source: Here