Appling Newton Physics to a FPS camera

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
SenVa
Posts: 38
Joined: Fri Oct 14, 2005 1:00 pm
Location: Mi

Appling Newton Physics to a FPS camera

Post by SenVa »

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
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

I have that problem as well...I thought about using the Camera as a child to the body and apply physics to the body. any more help Plz...this is just an idea...it needs developing by 1 of u pros out there :P
pushpork
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

Well just use your own camera scene node, not the FPS one.

I did it that way and it works very well.

Regards - Xaron
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

Yeah, you'd have to use a regular camera scene node. All you have to do is create a NewtonBody (sphere works great for player controller), put an UpVector constraint on it, then write a force and torque callback for the camera body that responds to key input.
SenVa
Posts: 38
Joined: Fri Oct 14, 2005 1:00 pm
Location: Mi

Post by SenVa »

Thanks for the help. I'll try that when i get back home.
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

so...sphere/ellipsoid...body inside...aswell as camera...then apply movement in callback?
Sounds about right...thnx.
pushpork
Xaron
Posts: 310
Joined: Sun Oct 16, 2005 7:39 am
Location: Germany
Contact:

Post by Xaron »

Let me know, if you need some code snippets. ;)

Regards - Xaron
SenVa
Posts: 38
Joined: Fri Oct 14, 2005 1:00 pm
Location: Mi

Post by SenVa »

Thanks i was able to get it to work... it is kinda flaky but i think i can take it from here...
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post by Baal Cadar »

"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.
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

Also remember to dampen your force and torque to get better control
SenVa
Posts: 38
Joined: Fri Oct 14, 2005 1:00 pm
Location: Mi

Post by SenVa »

Wow that fixed alot... thanks.
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

I'm not sure I did it right...if u post a code snippet and I see where I went wrong...
pushpork
pfo
Posts: 370
Joined: Mon Aug 29, 2005 10:54 pm
Location: http://web.utk.edu/~pfox1

Post by pfo »

This is may camera controller, and it works pretty well:

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");
dir is just a vectror2di with values -1, 0 or 1 for X and Y to control movement
JPulham
Posts: 320
Joined: Sat Nov 19, 2005 12:06 pm

Post by JPulham »

thnx...it cleared a few things up :P
pushpork
KonFused

Post by KonFused »

Pfo -

May I ask a few questions about your camera controller?
It seems to be the easiest to understand example I have seen...

I don't understand "g_vFront" what is that?

and

Where abouts do use the camera controller?
Within "SetMeshTransformEvent" or "ApplyForceAndTorqueEvent"?

Kind Thanks,
Post Reply