MattaU
22-01-2008 21:26:45
the following code is used to attach a node to a body, however the node does not move, also the mouse look only works vertically.
any help would be greatly appreciated
any help would be greatly appreciated
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Mogre;
using System.Drawing;
using MogreNewt;
namespace FPSgame
{
class physicsPlayer : worldnode
{
int camera_rotation_x;
int camera_rotation_y;
Vector3 cam_size = new Vector3(10, 10, 10);
int gravity = -100;
SceneNode cam_node;
public Body cam_body;
SceneNode cam_view_node;
float cam_height;
int jump_power = 5;
int fps_speed = 3;
bool no_movement = true;
int x_sensibility = 5;
int y_sensibility = 5;
int y_rotation_cont = 0;
int y_limit_a = 15;
int y_limit_b = -40;
public MOIS.Keyboard inputKeyboard;
public MOIS.Mouse inputMouse;
public World _World;
Camera camera;
SceneManager mgr;
public physicsPlayer(int x, int y, int z, int roll, int pitch, int yaw, SceneManager Mgr, Camera portcam, MOIS.Keyboard inputkeyboard, MOIS.Mouse inputmouse,World _world)
{
mgr = Mgr;
_World = _world;
_World.SetWorldSize(new Vector3(-100000, -100000, -100000), new Vector3(100000, 100000, 100000));
Vector3 cam_size = new Vector3(10, 10, 10);
float cam_mass = 40;
MogreNewt.Debugger.Instance.Init(mgr);
camera = portcam;
inputKeyboard = inputkeyboard;
inputMouse = inputmouse;
cam_node = mgr.RootSceneNode.CreateChildSceneNode("testnodename");
cam_node.Scale(cam_size); // define it's scale
Collision cam_collision = new MogreNewt.CollisionPrimitives.Ellipsoid( _World, cam_size ); // create a collision for the body
cam_body= new MogreNewt.Body( _World, cam_collision ); // create the body
cam_body.AttachToNode(cam_node);
cam_body.AutoFreeze = false;
// attach the node to the body. Now, when the body moves the node moves too
Vector3 cam_inertia = MogreNewt.MomentOfInertia.CalcEllipsoidSolid( cam_mass, cam_size ); // calculate the inertia ov the body
cam_body.SetMassMatrix(cam_mass, cam_body.Intertia);
this.cam_body.ForceCallback += new ForceCallbackHandler(camera_force_callback);
MogreNewt.BasicJoints.UpVector uv2 = new MogreNewt.BasicJoints.UpVector(_World,cam_body,Vector3.UNIT_Y); // create an upvector. This forces the body to rotate just in the X and Z axes.
cam_view_node = mgr.RootSceneNode.CreateChildSceneNode(new Vector3(0,5,0)); // crete a child 5 units above the cam_node
cam_view_node.AttachObject(camera); // attach the camera to the view_node.
}
void camera_force_callback(Body body)
{
float mass;
Vector3 inertia;
body.GetMassMatrix(out mass, out inertia);
Vector3 force = new Vector3(0,gravity,0);
force *= mass;
body.AddForce(force);
body.Omega = new Vector3(0, camera_rotation_x, 0);
}
Vector3 get_body_position(Body bod) // get the position of the body
{
Quaternion orient;
Vector3 pos;
bod.GetPositionOrientation(out pos,out orient);
return pos;
}
Quaternion get_body_orientation(Body bod) // get the orientation of the body
{
Quaternion orient;
Vector3 pos;
bod.GetPositionOrientation( out pos, out orient);
return orient;
}
protected virtual void HandleInput(FrameEvent evt)
{
inputKeyboard.Capture();
inputMouse.Capture();
no_movement = true; // for the moment there ins't movement
BasicRaycast cam_ray = new BasicRaycast (_World,get_body_position(cam_body), get_body_position(cam_body) + Vector3.NEGATIVE_UNIT_Y * 2000 ); // throw a ray from the body to 2000 units below to calculate the // height of the body regard to the floor.
if (cam_ray.HitCount > 0)
{
cam_height = cam_ray.FirstHit.mDistance *10000; // calculale the camera height.
}
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_W))
{
Vector3 direction = get_body_orientation(cam_body) * Vector3.NEGATIVE_UNIT_Z;
cam_body.Velocity = (cam_body.Velocity * new Vector3(0, 1, 0) + direction * 30 * fps_speed);
no_movement = false;
}
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_S))
{
Vector3 direction = get_body_orientation(cam_body) * Vector3.UNIT_Z;
cam_body.Velocity = (cam_body.Velocity*new Vector3(0,1,0) + direction*30*fps_speed);
no_movement = false;
}
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_A))
{
Vector3 direction = get_body_orientation(cam_body) * Vector3.UNIT_X;
cam_body.Velocity = (cam_body.Velocity * new Vector3(0, 1, 0) + direction * 30 * fps_speed);
no_movement = false;
}
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_D))
{
Vector3 direction = get_body_orientation(cam_body) * Vector3.NEGATIVE_UNIT_X;
cam_body.Velocity = (cam_body.Velocity * new Vector3(0, 1, 0) + direction * 30 * fps_speed);
no_movement = false;
}
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_SPACE))
{
if ((cam_height > 10) && (cam_height < 22))
{
cam_body.Velocity = (cam_body.Velocity * new Vector3(1, 0, 1) + new Vector3(0, 40, 0) * jump_power);
}
}
if (inputKeyboard.IsKeyDown(MOIS.KeyCode.KC_LSHIFT))
{
fps_speed = 3;
}
else
{
fps_speed = 1;
}
camera_rotation_x = -inputMouse.MouseState.X.rel * x_sensibility / (100 * (int)evt.timeSinceLastFrame + 1); // this is used in the defined callback function to rotate the camera in the X axes, with mouse
camera_rotation_y = -inputMouse.MouseState.Y.rel * y_sensibility / (100 * (int)evt.timeSinceLastFrame + 1); // to rotate the camera in the Y axes, with mouse
cam_view_node.Pitch( new Degree(camera_rotation_y) ); // roate the view node
y_rotation_cont += camera_rotation_y; // calculate the total of the rotations we have done
if (y_rotation_cont > y_limit_a || y_rotation_cont < y_limit_b) // if the total is bigger or smallest than the limits it will be reseted to it's previous value
{
cam_view_node.Pitch(new Degree(-camera_rotation_y));
y_rotation_cont -= camera_rotation_y;
}
if (no_movement) cam_body.Velocity = (cam_body.Velocity * new Vector3(0, 1, 0)); // if there is no_movement then we set the velocity in the X and Z axes to 0. If you want to add some inertia change the X //and Z values to another
}
public void update(FrameEvent evt, List<worldnode> world)
{
HandleInput(evt);
}
}
}