Setting the mass
cherry12345
26-06-2009 10:57:24
Hi,
I have one little(I hope;)) problem. I'm doing something like this(I'm trying to set the mass to player body):
Ogre::Real mass = -10;
Ogre::Vector3 inertia = OgreNewt::MomentOfInertia::CalcEllipsoidSolid(mass,size);
body->setMassMatrix( mass, inertia );
body->setStandardForceCallback();
Everyting is okey. But, when I change the mass(for example on 100000), Body behave same. For every value(exclude 0) in my game, body behave same. Any ideas?
deadvirus
26-06-2009 12:38:29
Well, the only force applied is gravity (setStandardForceCallback), with does not depends on the mass. So the body will behave always in the same way, regardless of the mass. I think...
nullsquared
27-06-2009 01:46:30
Physics engines don't simulate air resistance and all the other little real-world details. Therefore, a more massive object won't fall any faster. (however, it will behave properly when it comes to less massive objects trying to push it around)
cherry12345
29-06-2009 19:26:20
So, how can I add some force to my object? I tried, by using a ->addForce(...), but nothing changed. My Intelisense tell me: "This function is only valid inside ForceCallback function". How I must to implement the ForceCallback function? I have to declare and define that, and transfer the Pointer of forceCallback function to some Newton's function?
Virginie
30-06-2009 12:29:46
You should use :
OgreNewt::CollisionPrimitives::Box *l_BoxCollision = new OgreNewt::CollisionPrimitives::Box(m_World, Ogre::Vector3(1,1,1));
OgreNewt::CollisionPtr l_BoxCollisionPtr(l_BoxCollision);
Ogre::Vector3 inertie,offset;
l_BoxCollision->calculateInertialMatrix(inertie,offset);
m_BodyCollision = new OgreNewt::Body(m_World,l_BoxCollisionPtr,-1);
Ogre::Real mass = -10;
m_BodyCollision->setMassMatrix(mass ,mass *inertie);
kallaspriit
30-06-2009 14:18:50
What's up with the negative mass (and thus also inertia)?
Virginie
01-07-2009 08:44:05
I don't know but a negative mass don't exist in real world so I think it's bad

I haven't reflective when I wrote your negative mass ^^
deadvirus
01-07-2009 16:33:31
So, how can I add some force to my object? I tried, by using a ->addForce(...), but nothing changed. My Intelisense tell me: "This function is only valid inside ForceCallback function". How I must to implement the ForceCallback function? I have to declare and define that, and transfer the Pointer of forceCallback function to some Newton's function?
Yeah, to add force you need to define a force callback. I assume you're using newton 1.53, so you first need to create a function like this (assuming your class is named MyClass):
void MyClass::forceCallback(OgreNewt::Body* body)
{
//here you can do something like body->addLocalForce(.....);
}
That function parameter needs to be only that one!
Then, where you create the body for example you do (inside MyClass):
body->setCustomForceAndTorqueCallback<MyClass>(&MyClass::forceCallback, this);
cherry12345
03-07-2009 21:37:06
Thanks a lot!
I meet a another problem. I'm trying to make a platform game, but I have no idea how can I implement a Tpp camera. Can anybody share me with his code, or some leads?
P.S. Sorry for my english
deadvirus
04-07-2009 00:18:03
Thanks a lot!
I meet a another problem. I'm trying to make a platform game, but I have no idea how can I implement a Tpp camera. Can anybody share me with his code, or some leads?
P.S. Sorry for my english 
What is a Tpp camera?
cherry12345
04-07-2009 21:25:18
TPP - Third-person perspective, but I already done it

. But I have another question: Which function I can check does body colide with other body?
kallaspriit
05-07-2009 09:04:19
Search for collision callbacks, there is an example in OgreNewt too
cherry12345
06-07-2009 15:54:10
Hi,
I need a fast help. I have troubles with collision. My english isn't the best, so I recorded the movie(his size is only 500 kb, so I don't add any screenshots). Short, when player's node fall to the platform from up everything is ok, but when it collide with platform from sides, newton doesn't see a collision.
Link to movie:
http://www.sendspace.com/file/2vgmb7
deadvirus
07-07-2009 00:47:30
Hi,
I need a fast help. I have troubles with collision. My english isn't the best, so I recorded the movie(his size is only 500 kb, so I don't add any screenshots). Short, when player's node fall to the platform from up everything is ok, but when it collide with platform from sides, newton doesn't see a collision.
Link to movie:
http://www.sendspace.com/file/2vgmb7
Are you moving the body by adding forces? Or with setPositionOrientation? If you use this last one, it's normal to see that result... You should use forces to move the bodies, or if you really need to move it "manually" you should check if it collides, and move only if it doesn't.
cherry12345
09-07-2009 09:59:26
Sorry, for problems, but I don't know how I can change my code.
First, I will describe how my game works. I have a Player's class, in which I try to change my code. If Player moves a mouse, I'm doing sth like this:
node->yaw(Degree(-0.13 * e.state.X.rel));
Until, I didn't install a newton everything ok. But now I need to add this line(If I don't do that, the node is backing to the same angle)
body->setPositionOrientation(node->getPosition(),node->getOrientation());
If I good understand, this line causes that the player go into the platform. So, there is a question: How I need change my code without using setPositonOrientation(...)?
deadvirus
10-07-2009 00:07:14
Sorry, for problems, but I don't know how I can change my code.
First, I will describe how my game works. I have a Player's class, in which I try to change my code. If Player moves a mouse, I'm doing sth like this:
node->yaw(Degree(-0.13 * e.state.X.rel));
Until, I didn't install a newton everything ok. But now I need to add this line(If I don't do that, the node is backing to the same angle)
body->setPositionOrientation(node->getPosition(),node->getOrientation());
If I good understand, this line causes that the player go into the platform. So, there is a question: How I need change my code without using setPositonOrientation(...)?
Well, the problem there is that you are "forcing" the body position, and not letting Newton do it's work I guess.
I think that you should not do anything to the node by yourself. You should attach it to the body and only do your stuff on the body (and OgreNewt will move the node for you).
To solve you're problem I don't really know... You could try to only call setPositionOrientation when the mouse really moves (dunno if you're doing this already). You can try to use forces too... But you will need to do something to prevent some "sliding" (like you stop moving the mouse and it still rotates a little bit), and I think you can use a hight rotational damping to prevent this.
kallaspriit
10-07-2009 07:29:35
Deadvirus is right, leave the node alone and use forces to move your character. Have not implemented a character controller myself, but I am guessing you need to implement custom force callback, where you can apply the forces. Its still not so easy, because you will probably get slippery behavior and have to change the frictions dynamically based on different states of the character. And then there is the stair and slope climbing...
There is a character controller custom joint in the new version of Newton, maybe you should check that out
cherry12345
10-07-2009 22:07:23
Thanks for suggestions, I done it by using this part of code:
Ogre::Vector3 curpos, goalpos; // current position and orientation of the rigid body.
Ogre::Quaternion curorient, goalorient; // goal position and orientation of the rigid body.
b->getPositionOrientation( curpos, curorient );
goalpos = node->getPosition();
goalorient = node->getOrientation( );
Ogre::Matrix3 current, goal;
curorient.ToRotationMatrix( current );
goalorient.ToRotationMatrix( goal );
b->setPositionOrientation(curpos,curorient);
Yeah, all collision works, but... body keep rotates. In ForceCallback function I'm using a AddForce in this way:
force = Vector3(0,-9.8,0);
force = force+velocity;
b->addForce(force);
The velocity vector is setting by:
Vector3 force = sceneMgr->getCamera("PlayerCam")->getDerivedDirection();
force.y = 0;
force.normalise();
velocity = Vector3(force.x,0,force.z);
velocity = velocity*10;
What am I doing wrong?
kallaspriit
11-07-2009 09:00:50
It seems you are still setting the position and orientation directly and that should not work too well. You should be able to achieve this with a custom joint telling where you want the body to be in XZ space and let Newton apply needed forces. With custom joints, you can also force the character to face certain direction-angle.
The rotation you currently get is probably from the fact that you are not applying any forces to stop the normal rotation that a body will get after bumping into something.
cherry12345
11-07-2009 10:07:08
The rotation you currently get is probably from the fact that you are not applying any forces to stop the normal rotation that a body will get after bumping into something.
Which forces I need to apply? I already tried this:
b->setTorque(Vector3(0,0,0));
b->setOmega(Vector3(0,0,0));
b->setAngularDamping(Vector3(0,0,0));
b->setLinearDamping(various values);
And body still rotates.
It seems you are still setting the position and orientation directly and that should not work too well. You should be able to achieve this with a custom joint telling where you want the body to be in XZ space and let Newton apply needed forces. With custom joints, you can also force the character to face certain direction-angle.
Maybe some examples?
kallaspriit
12-07-2009 07:58:36
I am afraid Im too busy with my own project-problems, but there seems to be another good example in NewtonSDK 2 custom joints called CustomPickBody, that enables you to set target orientation and position and let Newton figure out how to get there. Have not tried it myself, but seems to be pretty much what you need or atleast use as reference. I'll post that code here in case you dont use NGD2
CustomPickBody.h
//********************************************************************
// Newton Game dynamics
// copyright 2000-2004
// By Julio Jerez
// VC: 6.0
// simple demo list vector class with iterators
//********************************************************************
// CustomPickBody.h: interface for the CustomPickBody class.
//
//////////////////////////////////////////////////////////////////////
#ifndef __CustomPickBody_H__
#define __CustomPickBody_H__
#include "NewtonCustomJoint.h"
class JOINTLIBRARY_API CustomPickBody: public NewtonCustomJoint
{
public:
CustomPickBody (const NewtonBody* body, const dVector& handleInGlobalSpace);
virtual ~CustomPickBody();
void SetPickMode (int mode);
void SetMaxLinearFriction(dFloat accel);
void SetMaxAngularFriction(dFloat alpha);
void SetTargetRotation (const dQuaternion& rotation);
void SetTargetPosit (const dVector& posit);
void SetTargetMatrix (const dMatrix& matrix);
dMatrix GetTargetMatrix () const;
protected:
virtual void SubmitConstrainst (dFloat timestep, int threadIndex);
virtual void GetInfo (NewtonJointRecord* info) const;
int m_pickMode;
int m_autoSlepState;
dFloat m_maxLinearFriction;
dFloat m_maxAngularFriction;
dVector m_localHandle;
dVector m_targetPosit;
dQuaternion m_targetRot;
};
#endif // !defined(AFX_CustomPickBody_H__EAE1E36C_6FDF_4D86_B4EE_855E3D1046F4__INCLUDED_)
CustomPickBody.cpp
//********************************************************************
// Newton Game dynamics
// copyright 2000-2004
// By Julio Jerez
// VC: 6.0
// simple demo list vector class with iterators
//********************************************************************
// CustomPickBody.cpp: implementation of the CustomPickBody class.
//
//////////////////////////////////////////////////////////////////////
#include "CustomJointLibraryStdAfx.h"
#include "CustomPickBody.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CustomPickBody::CustomPickBody(const NewtonBody* body, const dVector& handleInGlobalSpace)
:NewtonCustomJoint(6, body, NULL)
{
dMatrix matrix;
// get the initial position and orientation
NewtonBodyGetMatrix (body, &matrix[0][0]);
m_autoSlepState = NewtonBodyGetSleepState (body);
NewtonBodySetAutoSleep (body, 0);
m_localHandle = matrix.UntransformVector (handleInGlobalSpace);
matrix.m_posit = handleInGlobalSpace;
SetPickMode (1);
SetTargetMatrix (matrix);
SetMaxLinearFriction(1.0f);
SetMaxAngularFriction(1.0f);
}
CustomPickBody::~CustomPickBody()
{
NewtonBodySetAutoSleep (m_body0, m_autoSlepState);
}
void CustomPickBody::SetPickMode (int mode)
{
m_pickMode = mode ? 1 : 0;
}
void CustomPickBody::SetMaxLinearFriction(dFloat accel)
{
dFloat Ixx;
dFloat Iyy;
dFloat Izz;
dFloat mass;
NewtonBodyGetMassMatrix (m_body0, &mass, &Ixx, &Iyy, &Izz);
m_maxLinearFriction = dAbs (accel) * mass;
}
void CustomPickBody::SetMaxAngularFriction(dFloat alpha)
{
dFloat Ixx;
dFloat Iyy;
dFloat Izz;
dFloat mass;
NewtonBodyGetMassMatrix (m_body0, &mass, &Ixx, &Iyy, &Izz);
if (Iyy > Ixx) {
Ixx = Iyy;
}
if (Izz > Ixx) {
Ixx = Izz;
}
m_maxAngularFriction = dAbs (alpha) * Ixx;
}
void CustomPickBody::SetTargetRotation(const dQuaternion& rotation)
{
m_targetRot = rotation;
}
void CustomPickBody::SetTargetPosit(const dVector& posit)
{
m_targetPosit = posit;
}
void CustomPickBody::SetTargetMatrix(const dMatrix& matrix)
{
SetTargetRotation (matrix);
SetTargetPosit (matrix.m_posit);
}
dMatrix CustomPickBody::GetTargetMatrix () const
{
return dMatrix (m_targetRot, m_targetPosit);
}
void CustomPickBody::GetInfo (NewtonJointRecord* info) const
{
strcpy (info->m_descriptionType, "pickBody");
/*
info->m_attachBody_0 = m_body0;
info->m_attachBody_1 = m_body1;
info->m_minLinearDof[0] = -FLT_MAX;
info->m_maxLinearDof[0] = FLT_MAX;
info->m_minLinearDof[1] = -FLT_MAX;
info->m_maxLinearDof[1] = FLT_MAX;
info->m_minLinearDof[2] = -FLT_MAX;
info->m_maxLinearDof[2] = FLT_MAX;
info->m_minAngularDof[0] = -FLT_MAX;
info->m_maxAngularDof[0] = FLT_MAX;
info->m_minAngularDof[1] = 0.0f;
info->m_maxAngularDof[1] = 0.0f;
info->m_minAngularDof[2] = 0.0f;
info->m_maxAngularDof[2] = 0.0f;
info->m_bodiesCollisionOn = 1;
memcpy (info->m_attachmenMatrix_0, &m_localMatrix0, sizeof (dMatrix));
// note this is not a bug
memcpy (info->m_attachmenMatrix_1, &m_localMatrix0, sizeof (dMatrix));
*/
}
void CustomPickBody::SubmitConstrainst (dFloat timestep, int threadIndex)
{
dVector v;
dVector w;
dVector cg;
dMatrix matrix0;
dFloat invTimestep;
// calculate the position of the pivot point and the Jacobian direction vectors, in global space.
NewtonBodyGetOmega (m_body0, &w[0]);
NewtonBodyGetVelocity (m_body0, &v[0]);
NewtonBodyGetCentreOfMass (m_body0, &cg[0]);
NewtonBodyGetMatrix (m_body0, &matrix0[0][0]);
invTimestep = 1.0f / timestep;
dVector p0 (matrix0.TransformVector (m_localHandle));
dVector pointVeloc = v + w * matrix0.RotateVector (m_localHandle - cg);
dVector relPosit (m_targetPosit - p0);
dVector relVeloc (relPosit.Scale (invTimestep) - pointVeloc);
dVector relAccel (relVeloc.Scale (invTimestep * 0.3f));
// Restrict the movement on the pivot point along all tree orthonormal direction
NewtonUserJointAddLinearRow (m_joint, &p0[0], &m_targetPosit[0], &matrix0.m_front[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAccel % matrix0.m_front);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxLinearFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxLinearFriction);
NewtonUserJointAddLinearRow (m_joint, &p0[0], &m_targetPosit[0], &matrix0.m_up[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAccel % matrix0.m_up);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxLinearFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxLinearFriction);
NewtonUserJointAddLinearRow (m_joint, &p0[0], &m_targetPosit[0], &matrix0.m_right[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAccel % matrix0.m_right);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxLinearFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxLinearFriction);
if (m_pickMode) {
dFloat mag;
dQuaternion rotation;
NewtonBodyGetRotation (m_body0, &rotation.m_q0);
if (m_targetRot.DotProduct (rotation) < 0.0f) {
rotation.m_q0 *= -1.0f;
rotation.m_q1 *= -1.0f;
rotation.m_q2 *= -1.0f;
rotation.m_q3 *= -1.0f;
}
dVector relOmega (rotation.CalcAverageOmega (m_targetRot, timestep) - w);
mag = relOmega % relOmega;
if (mag > 1.0e-6f) {
dFloat relAlpha;
dFloat relSpeed;
dVector pin (relOmega.Scale (1.0f / mag));
dMatrix basis (dgGrammSchmidt (pin));
relSpeed = dSqrt (relOmega % relOmega);
relAlpha = relSpeed * invTimestep;
NewtonUserJointAddAngularRow (m_joint, 0.0f, &basis.m_front[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAlpha);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &basis.m_up[0]);
NewtonUserJointSetRowAcceleration (m_joint, 0.0f);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &basis.m_right[0]);
NewtonUserJointSetRowAcceleration (m_joint, 0.0f);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction);
} else {
dVector relAlpha = w.Scale (-invTimestep);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &matrix0.m_front[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAlpha % matrix0.m_front);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &matrix0.m_up[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAlpha % matrix0.m_up);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &matrix0.m_right[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAlpha % matrix0.m_right);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction);
}
} else {
// this is the single handle pick mode, add soem angular frition
dVector relAlpha = w.Scale (-invTimestep);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &matrix0.m_front[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAlpha % matrix0.m_front);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction * 0.025f);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction * 0.025f);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &matrix0.m_up[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAlpha % matrix0.m_up);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction * 0.025f);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction * 0.025f);
NewtonUserJointAddAngularRow (m_joint, 0.0f, &matrix0.m_right[0]);
NewtonUserJointSetRowAcceleration (m_joint, relAlpha % matrix0.m_right);
NewtonUserJointSetRowMinimumFriction (m_joint, -m_maxAngularFriction * 0.025f);
NewtonUserJointSetRowMaximumFriction (m_joint, m_maxAngularFriction * 0.025f);
}
}
cherry12345
12-07-2009 19:46:15
Thanks!
This a lot of code. I must spend a few hours to understand and write a new character controller. Is some way to stop rotation, exclude a rewrite a code?
P.S. Yes, I'm still using a setPositionOrientation(...) function, when the mouse moves.
kallaspriit
12-07-2009 21:34:42
I do think a custom joint is the way to go for a decent character controller.
Also check out the existing character controller code in latest NGD beta (2.03 I believe), it should work out of the box but not sure if exactly as needed. I've heard Julio is planning to improve with further for the next 2.04 version, not sure about that.
Good luck