feanor91
04-09-2008 16:43:50
Hi everybody.
I use Newton to detect camera collision.
First the code that I use to enable newton :
The init sub :
The update sub :
The get opsition, orientation and callback func :
I got it and adapt it from different exemples found here an there.
So All works fine, exept for gravity. With a gravity of -9.8 and a mass of 90kg, my body didn't fall on the ground as a corpse of 90Kg. I must set a gravity of -1500 to see my camera gentlly goes down to the ground as it is a sheet of paper...And if I put a more heavy gravity, frction on ground are so important that my cam didn't move at all.
So my question is, why or what I made wrong?
I use Newton to detect camera collision.
First the code that I use to enable newton :
The init sub :
Private Sub InitNewton(ByVal Langue)
Dim CamNode As ArrayList = New ArrayList 'Le node de la caméra / camera node
Dim CamViewNode As ArrayList = New ArrayList
Dim CamBody As ArrayList = New ArrayList 'Le corps physique de la caméra / physical cam body
Dim CamColl As CollisionPrimitives.Ellipsoid 'Le type de collision / collision type
If Langue = "FR" Then
myfrminfo.lblInfo.Text = "Initialisation du monde physique....Merci de patienter."
ElseIf Langue = "DE" Then
myfrminfo.lblInfo.Text = "Initialisation der körperlichen Welt(Leute).... Bitte sich gedulden."
ElseIf Langue = "ES" Then
myfrminfo.lblInfo.Text = "Inicialización del mundo(gente) fÃsico.... Gracias por tener paciencia."
ElseIf Langue = "IT" Then
myfrminfo.lblInfo.Text = "Inizializzazione del mondo fisico....Grazie essere paziente.."
ElseIf Langue = "PO" Then
myfrminfo.lblInfo.Text = "Inicialização do mundo fÃsico....Lhe agradeça ser paciente."
Else
myfrminfo.lblInfo.Text = "Physical world initialisation....Please wait"
End If
myfrminfo.Refresh()
'Initialisation de Newton pour la gestion des collision caméras
'Newton init to handle cam colision
If myWorld Is Nothing Then
myWorld = New World 'le monde physique / physical world
'et initialisation de sa taille, par défaut elle est de 100 sur chaque axes.
'and init of it's size by default it's 100 on each axes
myWorld.SetWorldSize(New Mogre.Vector3(-100000, -100000, -100000), New Mogre.Vector3(100000, 100000, 100000))
End If
myWorld.SetMinimumFrameRate(_NewtonUpdateFps)
TimeUpdate = 1 / _NewtonUpdateFps
'Création du monde physique pour Newton / creating newton physical world
Dim stat_col As MogreNewt.CollisionPrimitives.TreeCollisionSceneParser = New MogreNewt.CollisionPrimitives.TreeCollisionSceneParser(myWorld)
stat_col.ParseScene(_OgreSceneManager.RootSceneNode, False) 'Utilisation du parser de scène Newton, attention, à ce jour (juillet 2008), le parmètre true, au mieux, ne fonctionne pas et au pire plante le programme.
'use of newton scene parser. Beware if true is use, physical world is not well initialised
'Création et paramétrage du body pour le monde / creation and initialisation of body world
Dim bod As MogreNewt.Body = New MogreNewt.Body(myWorld, stat_col)
stat_col.Dispose()
bod.AttachToNode(_OgreSceneManager.RootSceneNode)
bod.SetPositionOrientation(New Mogre.Vector3(0.0, 0.0, 0.0), Quaternion.IDENTITY)
For i As Integer = 0 To Scene.CameraList.Count - 1
_OgreCamera = Scene.CameraList(i)
'Collision de la caméra / camera collision
Dim PosCam As Mogre.Vector3 = _OgreSceneManager.GetSceneNode(_OgreCamera.Name).Position 'on récupère la position de la caméra / cam position
Dim OrientCam As Quaternion = _OgreSceneManager.GetSceneNode(_OgreCamera.Name).Orientation 'on récupère l'orientation de la caméra / cam orientation
Dim localcambody As MogreNewt.Body
CamNode.Add(Nothing)
CamBody.Add(Nothing)
CamViewNode.Add(Nothing)
CamNode(i) = _OgreSceneManager.RootSceneNode.CreateChildSceneNode("CamNode" + i.ToString) 'on crè un Scennode / scenenode creation
CamNode(i).SetScale(_CamSize) 'on le met à la taille de la caméra / applying cam size
CamColl = New CollisionPrimitives.Ellipsoid(myWorld, _CamSize) 'on crè le système de collision / collision system creation
localcambody = New Body(myWorld, CamColl) 'on l'applique au body représentant la caméra / applying it to cam body
CamColl.Dispose() 'on libère la collision / freeing collision
localcambody.AttachToNode(CamNode(i)) 'et on l'attache au noeud créé précédement / attach it to node
'calcul de l'inertie de la caméra / inertia camera
Dim cam_inertia As Mogre.Vector3 = MomentOfInertia.CalcEllipsoidSolid(_CammMass, _CamSize)
localcambody.SetMassMatrix(_CammMass, cam_inertia)
AddHandler localcambody.ForceCallback, AddressOf camera_force_callback 'la fonction appelée lors de l'update du monde physique / function call at each physical update
localcambody.AutoFreeze = False 'Important, sinon, impossible de déplacer le body... / important else body don't move
CamBody(i) = localcambody
'Création d'un "UP Vector" pour empécher le body de tomber / creation of a "UP Vector" so body can't fall
Dim uv2 As BasicJoints.UpVector = New BasicJoints.UpVector(myWorld, CamBody(i), Mogre.Vector3.UNIT_Y)
'Création d'un scenenode 1 unités au dessus du camnode pour y mettre la caméra
'Creation of a scene node 1 units above camnode to put camera in
CamViewNode(i) = CamNode(i).CreateChildSceneNode("CamViewNode" + i.ToString, New Mogre.Vector3(0, 1, 0)) 'création du scenenode / scene node creation
_OgreSceneManager.RootSceneNode.DetachObject(_OgreCamera) 'on détache la caméra du rootscenenode (créé par OSMLoader) / detaching camera from root scene node (create by OSM Loader)
CamViewNode(i).AttachObject(_OgreCamera) 'et on attache la caméra au viewnode / attachin cam to viewnode
CamBody(i).SetPositionOrientation(PosCam, OrientCam) 'Sert à récupérer la position et orientation initiale de la caméra pour positionner son corp. / Puting body in initial cam position and orientation
InitialCamBodyOrient = get_body_orientation(CamBody(i))
InitialCamBodyPos = get_body_position(CamBody(i))
Dim StructCam As New Camera
StructCam.Camera = _OgreCamera
StructCam.CamNode = CamNode(i)
StructCam.CamViewNode = CamViewNode(i)
StructCam.CamBody = CamBody(i)
ListeCamera.Add(StructCam)
Next
_CurrentCamBody = CamBody(0)
CurrentCamNode = CamNode(0)
CurrentCamViewNode = CamViewNode(0)
_OgreCamera = Scene.CameraList(0)
'MogreNewt.Debugger.Instance.Init(_OgreSceneManager) 'et on initialise le debugger newton. / newton debugger initialisation
End Sub
The update sub :
Private Sub UpdateNewton(ByVal TimeSinceLastFrame As Single)
'La fonction d'update du monde physique / function to update physical world
If _TimeFactor <> 0 Then
TimeElapsed = 3.5 * TimeSinceLastFrame * _TimeFactor
End If
'If TimeElapsed > TimeUpdate Then 'si le temps passé depuis le dernier update est plus grand que le temp minimum choisi, on se rattrape / if time passed since last update greater than minimum time, running after time
' While TimeElapsed > TimeUpdate
' myWorld.Update(TimeUpdate)
' TimeElapsed -= TimeUpdate
' End While
'Else 'sinons / else...
If TimeElapsed < TimeUpdate Then
'Le temps passé est plus petit que le temps minimum, on ne fait rien / time elapsed smaller than minimum time
Else
myWorld.Update(TimeUpdate) 'sinon, on update / else updating
End If
'End If
End Sub
The get opsition, orientation and callback func :
Private Function get_body_position(ByVal bod As Body) As Mogre.Vector3 'retourne la position du body / return body position
Dim orient As Quaternion
Dim pos As Mogre.Vector3
bod.GetPositionOrientation(pos, orient)
Return pos
End Function
Private Function get_body_orientation(ByVal bod As Body) As Quaternion ' retourne l'orientation du body / return body orientation
Dim orient As Quaternion
Dim pos As Mogre.Vector3
bod.GetPositionOrientation(pos, orient)
Return orient
End Function
Private Sub camera_force_callback(ByVal Corps As Body)
'Cette procédure est appelée à chaque update du monde physique.
'On y applique la gravité ainsi que la rotation sur l'axe des X
'this proc is called by each physical worl update
'we put gravity force and X rotation
If _GravityOnOff Then
Dim masse As Single
Dim inertie As Mogre.Vector3
Dim Force As Mogre.Vector3 = New Mogre.Vector3(0, _Gravity, 0)
'on récupère la position et l'orientation du corp passé en paramètre / gettinf position and orientation of param body
Dim posBody As Mogre.Vector3 = get_body_position(_CurrentCamBody) * Mogre.Vector3.NEGATIVE_UNIT_Z
Dim OrientBody As Quaternion = get_body_orientation(_CurrentCamBody)
Corps.GetMassMatrix(masse, inertie) 'on récupère sa masse et son inertie / getting it's mass and inertia
'on y applique la gravité / applying gravity
Force *= masse
Corps.AddForce(Force) 'on applique la force de gravité / applying forces
End If
Corps.Omega = New Mogre.Vector3(0, camera_rotation_x, 0) 'pour la rotation sur X / rotation on X
End Sub
I got it and adapt it from different exemples found here an there.
So All works fine, exept for gravity. With a gravity of -9.8 and a mass of 90kg, my body didn't fall on the ground as a corpse of 90Kg. I must set a gravity of -1500 to see my camera gentlly goes down to the ground as it is a sheet of paper...And if I put a more heavy gravity, frction on ground are so important that my cam didn't move at all.
So my question is, why or what I made wrong?