Appling Newton Physics to a FPS camera
Appling Newton Physics to a FPS camera
I was able to integrate the cube demo into my game but I cant figure out how to set the FPS camera as an object to add gravity and collation detection to it.
Any help would be appreciated.
~SenVa
Any help would be appreciated.
~SenVa
-
pfo
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
-
Baal Cadar
- Posts: 377
- Joined: Fri Oct 28, 2005 10:28 am
- Contact:
"Flakyness" can very well be caused by a wrong inertia. Newton doesn't set inertia value to something sensible itself, like other physic engines do by default. You have to do it yourself.
Inertia vector on the main axes of a homogenous solid ellipsoid is (2/5)MR². R being the the vector of the radius in the three main axes.
Inertia vector on the main axes of a homogenous solid ellipsoid is (2/5)MR². R being the the vector of the radius in the three main axes.
-
pfo
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
-
pfo
- Posts: 370
- Joined: Mon Aug 29, 2005 10:54 pm
- Location: http://web.utk.edu/~pfox1
This is may camera controller, and it works pretty well:
dir is just a vectror2di with values -1, 0 or 1 for X and Y to control movement
Code: Select all
// if user interface is on, play idle animation and return
if (m_bUseUI) {
m_bActorIsMoving = false;
PlayAnimation(L"Idle");
return;
}
// get Newton info from body
core::matrix4 matrix;
float Mass, Ixx, Iyy, Izz;
NewtonBodyGetMatrix(m_pBody, &matrix.M[0]);
NewtonBodyGetMassMatrix(m_pBody, &Mass, &Ixx, &Iyy, &Izz);
// get player direction for movement
core::vector3df force, torque;
core::vector2di dir = GetPlayerDirection();
// calculate player movement force and set movement state
if (dir.X || dir.Y)
{
float moveForce = Mass * 20.0f;
force = core::vector3df(dir.Y * moveForce, 0, dir.X * moveForce);
// rotate force to global coordinates
matrix.rotateVect(force);
m_bActorIsMoving = true;
}
else
m_bActorIsMoving = false;
// calculate player rotation force and camera X angle
core::position2d<s32> pos = g_device->getCursorControl()->getPosition();
g_device->getCursorControl()->setPosition(m_posScreenCenter);
float turnForce = (pos.X - m_posScreenCenter.X) * Mass * 15.0f;
core::vector3df rot(turnForce, 0, 0);
matrix.rotateVect(rot);
core::vector3df front = g_vFront;
matrix.rotateVect(front);
torque = front.crossProduct(rot);
// dampen force applied
core::vector3df velocity, omega;
NewtonBodyGetVelocity(m_pBody, &velocity.X);
core::vector3df damp = velocity * Mass;
force -= damp;
// dampen torque applied
NewtonBodyGetOmega(m_pBody, &omega.X);
damp = omega * Mass * 15.0f;
torque -= damp;
// apply force and torque
NewtonBodyAddForce(m_pBody, &force.X);
NewtonBodyAddTorque(m_pBody, &torque.X);
// check if player wants to jump
if (isPlayerKeyPressed(KEY_SPACE, true)) {
m_nJumpTimeout = PLAYER_JUMP_TIMEOUT;
core::vector3df jumpImpulse(0, 10, 0), p = matrix.getTranslation();
NewtonAddBodyImpulse(m_pBody, &jumpImpulse.X, &p.X);
}
m_nJumpTimeout = m_nJumpTimeout ? m_nJumpTimeout - 1 : 0;
// position player camera
m_nCameraX += (pos.Y - m_posScreenCenter.Y) * PLAYER_CAMERA_SMOOTHNESS;
if (m_nCameraX > 45) m_nCameraX = 45;
if (m_nCameraX < -45) m_nCameraX = -45;
// setup camera
core::vector3df p = core::vector3df(0, 3, 0);
matrix.transformVect(p);
m_pCamera->setPosition(p);
front = g_vFront;
front.rotateYZBy(m_nCameraX, core::vector3df());
matrix.rotateVect(front);
m_pCamera->setTarget(p + (front * 50.0f));
// play walking or idle animation
if (dir.X) PlayAnimation(L"Walk");
else PlayAnimation(L"Idle");
-
KonFused