Joint on the edge of the circle - cocos2d-iphone

The green square is the box2d ground.
The bigger circle is connected to the ground by revolute joint, and can be rotated.
All the circles are b2_dynamicBody
I want to joint the smaller circles on the edge or border of the bigger circle as shown below.
Please tell me how can i achieve it and what kind of joint i have to use?
Also when i rotate big circle the small circle should stick at its place.

Go through this code....
// create joint between hook big circle
wheelSprite = [[CCSprite alloc] initWithFile:#"heroWheel.png"];
wheelSprite.visible = FALSE;
wheelSprite.scale = 1.0*hero_size_factor;
wheelSprite.rotation = 0.0;
wheelSprite.anchorPoint = CGPointMake(0.5,0.5);
[layer addChild:wheelSprite z:5];
ballBodyDef.userData = wheelSprite;
//create hook between big circle
hook = world->CreateBody(&ballBodyDef);
b2PolygonShape hookShape;
hookShape.SetAsBox(0.2,0.1);
b2FixtureDef hookDef;
hookDef.shape=&hookShape;
hookDef.density=0.5f*hero_size_factor;
hookDef.friction = 0.0f;
hookDef.restitution = 0.0f;
hookDef.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&hookDef);
//create circle shape fixture
b2CircleShape wheel1,wheel2,wheel3,wheel4;
float wheelRadius = hero_width/4.0f;
//Create fixture with above shape
b2FixtureDef ballShapeDef1,ballShapeDef2,ballShapeDef3,ballShapeDef4;
wheel1.m_p = b2Vec2(wheelRadius, wheelRadius);
wheel1.m_radius = wheelRadius;
ballShapeDef1.shape = &wheel1;
ballShapeDef1.density = 0.0f;
ballShapeDef1.friction = 0.0f;
ballShapeDef1.restitution = 0.0f;
ballShapeDef1.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef1);
wheel2.m_radius = wheelRadius;
wheel2.m_p = b2Vec2(-wheelRadius, -wheelRadius);
ballShapeDef2.shape = &wheel2;
ballShapeDef2.density = 0.0f;
ballShapeDef2.friction = 0.0f;
ballShapeDef2.restitution = 0.0f;
ballShapeDef2.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef2);
wheel3.m_radius = wheelRadius;
wheel3.m_p = b2Vec2(wheelRadius,-wheelRadius);
ballShapeDef3.shape = &wheel3;
ballShapeDef3.density = 0.0f;
ballShapeDef3.friction = 0.0f;
ballShapeDef3.restitution = 0.0f;
ballShapeDef3.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef3);
wheel4.m_radius = wheelRadius;
wheel4.m_p = b2Vec2(-wheelRadius,wheelRadius);
ballShapeDef4.shape = &wheel4;
ballShapeDef4.density = 0.0f;
ballShapeDef4.friction = 0.0f;
ballShapeDef4.restitution = 0.0f;
ballShapeDef4.filter.groupIndex=heroGroupIndex;
hook->CreateFixture(&ballShapeDef4);
//Create Revolute Joints between hook and big circle
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.bodyA = hook;
revoluteJointDef.bodyB = Leg;
revoluteJointDef.collideConnected = false;
revoluteJointDef.localAnchorA.Set(0.0,0.0);
revoluteJointDef.localAnchorB.Set(0.0,-(hero_height - 0.1));
revoluteJointDef.referenceAngle = 0;
revoluteJointDef.maxMotorTorque = 5.0*hero_size_factor;
Hero_Motor = (b2RevoluteJoint*)world->CreateJoint(&revoluteJointDef);

Related

Clarification needed on LookAt matrix calculation in DX9

I am going trough these 2 methods to calculate the lookat matrix
D3DXMatrixLookAtLH
zaxis = normal(At - Eye)
xaxis = normal(cross(Up, zaxis))
yaxis = cross(zaxis, xaxis)
xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
-dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1
D3DXMatrixLookAtRH
zaxis = normal(Eye - At)
xaxis = normal(cross(Up, zaxis))
yaxis = cross(zaxis, xaxis)
xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
dot(xaxis, eye) dot(yaxis, eye) dot(zaxis, eye) 1
why is the translation for RH not multiplied by -1?
TL;DR: The Microsoft Doc page for D3DXMatrixLookAtRH is wrong. I've filed a PR to fix it.
In the actual implementation of D3DXMath, it's clear that in both cases the Translation should be -dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) for both LH & RH.
The only difference is the zaxis computation.
D3DXMATRIX* WINAPI D3DXMatrixLookAtRH
( D3DXMATRIX *pOut, const D3DXVECTOR3 *pEye, const D3DXVECTOR3 *pAt,
const D3DXVECTOR3 *pUp )
{
D3DXVECTOR3 XAxis, YAxis, ZAxis;
// Compute direction of gaze. (-Z)
D3DXVec3Subtract(&ZAxis, pEye, pAt);
D3DXVec3Normalize(&ZAxis, &ZAxis);
// Compute orthogonal axes from cross product of gaze and pUp vector.
D3DXVec3Cross(&XAxis, pUp, &ZAxis);
D3DXVec3Normalize(&XAxis, &XAxis);
D3DXVec3Cross(&YAxis, &ZAxis, &XAxis);
// Set rotation and translate by pEye
pOut->_11 = XAxis.x;
pOut->_21 = XAxis.y;
pOut->_31 = XAxis.z;
pOut->_41 = -D3DXVec3Dot(&XAxis, pEye);
pOut->_12 = YAxis.x;
pOut->_22 = YAxis.y;
pOut->_32 = YAxis.z;
pOut->_42 = -D3DXVec3Dot(&YAxis, pEye);
pOut->_13 = ZAxis.x;
pOut->_23 = ZAxis.y;
pOut->_33 = ZAxis.z;
pOut->_43 = -D3DXVec3Dot(&ZAxis, pEye);
pOut->_14 = 0.0f;
pOut->_24 = 0.0f;
pOut->_34 = 0.0f;
pOut->_44 = 1.0f;
return pOut;
}
D3DXMATRIX* WINAPI D3DXMatrixLookAtLH
( D3DXMATRIX *pOut, const D3DXVECTOR3 *pEye, const D3DXVECTOR3 *pAt,
const D3DXVECTOR3 *pUp )
{
D3DXVECTOR3 XAxis, YAxis, ZAxis;
// Compute direction of gaze. (+Z)
D3DXVec3Subtract(&ZAxis, pAt, pEye);
D3DXVec3Normalize(&ZAxis, &ZAxis);
// Compute orthogonal axes from cross product of gaze and pUp vector.
D3DXVec3Cross(&XAxis, pUp, &ZAxis);
D3DXVec3Normalize(&XAxis, &XAxis);
D3DXVec3Cross(&YAxis, &ZAxis, &XAxis);
// Set rotation and translate by pEye
pOut->_11 = XAxis.x;
pOut->_21 = XAxis.y;
pOut->_31 = XAxis.z;
pOut->_41 = -D3DXVec3Dot(&XAxis, pEye);
pOut->_12 = YAxis.x;
pOut->_22 = YAxis.y;
pOut->_32 = YAxis.z;
pOut->_42 = -D3DXVec3Dot(&YAxis, pEye);
pOut->_13 = ZAxis.x;
pOut->_23 = ZAxis.y;
pOut->_33 = ZAxis.z;
pOut->_43 = -D3DXVec3Dot(&ZAxis, pEye);
pOut->_14 = 0.0f;
pOut->_24 = 0.0f;
pOut->_34 = 0.0f;
pOut->_44 = 1.0f;
return pOut;
}
Both D3DXMatrixLookAtRH and D3DXMatrixLookAtLH are part of "D3DXMath", the math library included in D3DX9 the D3DX10. These helper libraries are deprecated as is the DirectX SDK itself. The recommendation is to use DirectXMath instead.
If you must use D3DX9 for some reason, note you can avoid all the complicated issues covered on Microsoft Docs mixing the legacy DirectX SDK and modern versions of Visual Studio by just using the Microsoft.DXSDK.D3DX NuGet instead. New projects should move to any of the various replacements. See this blog post for more details.

Car body's angle not changing box2D C++

i'm making a side-scrolling car game with box2D.
I'm currently working on the car and it seems that i'm stuck.
The chassis of my car isn't rotating when for example the car is trying to climb a hill. I don't know if its normal or if i should set the angle of the body.
Here's a quick video that shows the problem : https://streamable.com/d802n
This is my code :
b2BodyDef carBox = b2BodyDef();
carBox.position = b2Vec2(bodyCenterPosition.x, bodyCenterPosition.y);
carBox.type = b2_dynamicBody;
car = game->getWorld()->CreateBody(&carBox);
b2PolygonShape carPolygon = b2PolygonShape();
carPolygon.SetAsBox(bodySize.x, bodySize.y);
b2FixtureDef carFix = b2FixtureDef();
carFix.density = 0.0f;
carFix.shape = &carPolygon;
car->CreateFixture(&carFix);
b2PolygonShape headPolygon = b2PolygonShape();
headPolygon.SetAsBox(headSize.x, headSize.y);
for (int i = 0; i < 8; i++) {
headPolygon.m_vertices[i].x -= 8.0f / RATIO;
headPolygon.m_vertices[i].y -= 24.0f / RATIO;
}
b2FixtureDef headFix = b2FixtureDef();
headFix.density = 0.0f;
headFix.shape = &headPolygon;
car->CreateFixture(&headFix);
b2CircleShape circleShape;
circleShape.m_radius = 0.35f;
circleShape.m_p.SetZero();
b2FixtureDef fd;
fd.shape = &circleShape;
fd.density = 1.0f;
fd.friction = 0.9f;
b2BodyDef wheel1Def;
wheel1Def.type = b2_dynamicBody;
wheel1Def.position = b2Vec2(backWheelCenterPosition.x, backWheelCenterPosition.y);
backWheel = game->getWorld()->CreateBody(&wheel1Def);
backWheel->CreateFixture(&fd);
b2BodyDef wheel2Def;
wheel2Def.type = b2_dynamicBody;
wheel2Def.position = b2Vec2(frontWheelCenterPosition.x, frontWheelCenterPosition.y);
frontWheel = game->getWorld()->CreateBody(&wheel2Def);
frontWheel->CreateFixture(&fd);
b2WheelJointDef springDef1;
springDef1.dampingRatio = 50.0f;
springDef1.maxMotorTorque = 1.0f;
springDef1.frequencyHz = 15.0f;
springDef1.motorSpeed = 0.0f;
springDef1.enableMotor = true;
springDef1.Initialize(car, backWheel, backWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
backSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);
springDef1.Initialize(car, frontWheel, frontWheel->GetPosition(), sfVecToB2Vec(sf::Vector2f(0.0f, 1.0f)));
frontSpring = (b2WheelJoint*) game->getWorld()->CreateJoint(&springDef1);
It is normal for a body with a fixture having a zero density to behave like an infinite mass. Sounds like that's not what you intended however.
Looking at the code presented, we see that the carFix.density and headFix.density are being set to 0.0f. Set these to a positive non-zero value, like 1.0f, and the dynamic body they're created on should behave more like a physical mass would. That's assuming all of the other fixtures created on that body also have a density that's greater than zero as well.
For a tutorial on fixtures and density, I'd recommend taking a look at iforce2d's Box2D C++ tutorials - Fixtures.
Hope this solves the problem or at least helps.
By the way, I loved the artwork shown in the video!

Box2D - Firing bullet from rotating gun

i have a little trouble as title said: i can't figure out how to shoot the bullet toward the direction the gun is pointing at.
Here's short version code, for my bullet firing:
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.bullet = true;
bd.position = gun->GetPosition();//bullet start at the middle of the gun
m_bullet = m_world->CreateBody(&bd);
m_bullet->CreateFixture(&fd);
m_bullet->ApplyLinearImpulseToCenter( ??? ,true);
At first i thought first param is the direction you want the body go toward so i put in gun->GetWorldPoint(b2Vec2(0.0f,-5.0f)) (middle of the gun's muzzle). Big miss! After a while i thought i should try put in the vector of current gun's rotation degrees b2Vec2 vect = b2Vec2(cos(angle * PI/180), sin(angle * PI/180)); but then the bullet won't fly at all. Now i am all out of ideas. Please, some light.
Full version of code:
public:
b2Body* m_bullet = NULL;
b2Body* gun;
b2RevoluteJoint* joint1;
b2FixtureDef fd;
TestingStuff()
{
{
//body
b2CircleShape circle1;
circle1.m_radius = 1.6f;
fd.shape = &circle1;
fd.density = 1.0f;
fd.filter.groupIndex = -1;
b2BodyDef bd1;
bd1.type = b2_staticBody;
bd1.position.Set(-5.0f, 9.0f);
b2Body* body1 = m_world->CreateBody(&bd1);
body1->CreateFixture(&fd);
//gun
b2PolygonShape box;
box.SetAsBox(0.5f, 5.0f);
fd.shape = &box;
fd.density = 1.0f;
fd.filter.groupIndex = -1;
b2BodyDef bd2;
bd2.type = b2_dynamicBody;
bd2.position.Set(-5.0f, 8.0f);
gun = m_world->CreateBody(&bd2);
gun->CreateFixture(&fd);
//joint
b2RevoluteJointDef jd1;
jd1.Initialize(gun, body1, bd1.position);
jd1.enableMotor = true;
jd1.maxMotorTorque = 90;
jd1.motorSpeed = 180 * DEGTORAD;//DEGTORAD=0.0174532925199432957f
joint1 = (b2RevoluteJoint*) m_world->CreateJoint(&jd1);
}
}
void Keyboard(int key)
{
switch (key)
{
case GLFW_KEY_COMMA:
if (m_bullet != NULL)
{
m_world->DestroyBody(m_bullet);
m_bullet = NULL;
}
{
//bullet
b2CircleShape shape;
shape.m_radius = 0.25f;
fd.shape = &shape;
fd.density = 1;
fd.restitution = 0.05f;
fd.filter.groupIndex = -1;
b2BodyDef bd;
bd.type = b2_dynamicBody;
bd.bullet = true;
bd.position = gun->GetPosition();
m_bullet = m_world->CreateBody(&bd);
m_bullet->CreateFixture(&fd);
m_bullet->ApplyLinearImpulseToCenter( ??? ,true);
}
break;
}
}
After a good sleep i found the solution after few more trial and error.
//bullet
float degAngle = joint1->GetJointAngle() * RADTODEG + 180;
b2Vec2 vect = b2Vec2(sin(degAngle* (b2_pi / 180)) * 10, cos(degAngle* (b2_pi / 180)) * 10);
m_bullet->ApplyLinearImpulseToCenter(vect ,true);
The * 10 is to increase the impulse, make the bullet fly faster and farther, for the sake of testing i just make it 10. Notice that this code is just for testing, if you want to make it more realistic, increase the impulse as well as make the bullet go toward the muzzle instead of go toward the vector it had been fired at.

Camera matrix invalid when aligned with up vector

I'm currently implementing cameras on my engine and I'm having an issue when the camera is looking from top to the floor (example, eye position 0.,0.,50. and target is 0.,0.,0.) my up vector is 0.,0.,1..
Then when I do the maths, the crossproduct of position and up gives 0.,0.,0. and then the view is screwed and nothing is rendered. If I move the camera, everything works as expected.
How can I solve this?
if (node==NULL || target==NULL) return;
node->Update();
eye=node->GetWorldMatrix()*GRPVECTOR(0.0,0.0,0.0); //converts from matrix to vector my vector transformation
target->Update();
obj=target->GetWorldMatrix()*GRPVECTOR(0.0,0.0,0.0);
GRPVECTOR ev;
GRPVECTOR z;
GRPVECTOR x_tmp;
GRPVECTOR x;
GRPVECTOR y;
ev = eye - obj;
ev.Normalize();
z=ev;
x_tmp.CrossProduct(&up,&z);
if (x_tmp.GetLengthf()==0.0f)
return; //my view is screwed, I return
x_tmp.Normalize();
x=x_tmp;
y.CrossProduct(&z,&x);
this->viewmatrix.matrix[0][0] = x.vector[0];
this->viewmatrix.matrix[0][1] = y.vector[0];
this->viewmatrix.matrix[0][2] = z.vector[0];
this->viewmatrix.matrix[0][3] = 0.0f;
this->viewmatrix.matrix[1][0] = x.vector[1];
this->viewmatrix.matrix[1][1] = y.vector[1];
this->viewmatrix.matrix[1][2] = z.vector[1];
this->viewmatrix.matrix[1][3] = 0.0f;
this->viewmatrix.matrix[2][0] = x.vector[2];
this->viewmatrix.matrix[2][1] = y.vector[2];
this->viewmatrix.matrix[2][2] = z.vector[2];
this->viewmatrix.matrix[2][3] = 0.0f;
this->viewmatrix.matrix[3][0] = -x.vector[0] * eye.vector[0] + -x.vector[1] * eye.vector[1] + -x.vector[2] * eye.vector[2];
this->viewmatrix.matrix[3][1] = -y.vector[0] * eye.vector[0] + -y.vector[1] * eye.vector[1] + -y.vector[2] * eye.vector[2];
this->viewmatrix.matrix[3][2] = -z.vector[0] * eye.vector[0] + -z.vector[1] * eye.vector[1] + -z.vector[2] * eye.vector[2];
this->viewmatrix.matrix[3][3] = 1.0f;
GRPMATRIX Translate;
Translate.BuildTranslationMatrix(-obj.vector[0],-obj.vector[1],-obj.vector[2]);
this->viewmatrix.GetMulplicationMatrix(&this->viewmatrix,&Translate);

Bullet vehicle wheels fall behind and wrong chassis pitch direction

I have created a bullet vehicle with a attached turret at the top like this:
gEngineForce = 0.f;
gBreakingFrontForce = 0.f;
gBreakingBackForce = 0.f;
maxEngineForce = 20000.f;
minEngineForce = -2000.f;
maxBreakingFrontForce = 4000.f;
maxBreakingBackForce = 600.f;
gVehicleSteering = 0.f;
steeringIncrement = 0.002f;
steeringClamp = 0.6f;
wheelRadius = 0.5f;
wheelWidth = 0.4f;
wheelFriction = 50;
suspensionStiffness = 10.f;
suspensionDamping = 1.3f;
suspensionCompression = 4.4f;
rollInfluence = 0.1f;//1.0f
btScalar suspensionRestLength(0.6);
btVector3 wheelDirectionCS0(0, -1, 0);
btVector3 wheelAxleCS(-1, 0, 0);
btTransform tr;
tr.setIdentity();
chassisShape = new btBoxShape(btVector3(1.f, 0.5f, 2.f));
bulletCollisionShapes.push_back(chassisShape);
compound = new btCompoundShape();
bulletCollisionShapes.push_back(compound);
btTransform chassisTrans;
chassisTrans.setIdentity();
//localTrans effectively shifts the center of mass with respect to the chassis
chassisTrans.setOrigin(btVector3(0, 1.3, 0));
compound->addChildShape(chassisTrans, chassisShape);
tr.setOrigin(btVector3(0, 0.f, 0));
//m_carChassis = CreateRigidBody(2000, tr, compound);
//m_carChassis->setDamping(0.2,0.2);
m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth, wheelRadius, wheelRadius));
m_carChassis = CreateRigidBody(2000, tr, compound);
// create turret
turretShape = new btBoxShape(btVector3(0.4f, 0.2f, 0.8f));
bulletCollisionShapes.push_back(turretShape);
btTransform turretTrans;
turretTrans.setIdentity();
turretTrans.setOrigin(btVector3(0.0f, 0.7f, 0.0f));
turretBody = CreateRigidBody(1, turretTrans, turretShape);
// add some data to build constraint frames
btVector3 axisA(0.f, 1.f, 0.f);
btVector3 axisB(0.f, 0.f, 0.f);
btVector3 pivotA(0.f, 1.f, 0.f);
btVector3 pivotB(0.f, 0.f, 0.f);
hinge = new btHingeConstraint(*m_carChassis, *turretBody, pivotA, pivotB, axisA, axisB);
//hinge->setLimit(-SIMD_HALF_PI * 0.5f, SIMD_HALF_PI * 0.5f);
hinge->enableAngularMotor(true, 0, 1);
// add constraint to world
bt_dynamicsWorld->addConstraint(hinge, true);
{
btTransform something;
something.setIdentity();
something.setOrigin(btVector3(0, 10, 0));
m_carChassis->setWorldTransform(something);
m_vehicleRayCaster = new btDefaultVehicleRaycaster(bt_dynamicsWorld);
m_vehicle = new btRaycastVehicle(m_tuning, m_carChassis, m_vehicleRayCaster);
m_carChassis->setActivationState(DISABLE_DEACTIVATION);
bt_dynamicsWorld->addVehicle(m_vehicle);
float connectionHeight = 1.2f;
//choose coordinate system
m_vehicle->setCoordinateSystem(rightIndex, upIndex, forwardIndex);
bool isFrontWheel = true;
btVector3 connectionPointCS0(1 - (-0.8*wheelWidth), connectionHeight, 3 * 1 - wheelRadius);
m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);
connectionPointCS0 = btVector3(-1 + (-0.8*wheelWidth), connectionHeight, 3 * 1 - wheelRadius);
m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);
isFrontWheel = false;
connectionPointCS0 = btVector3(-1 + (-0.8*wheelWidth), connectionHeight, -3 * 1 + wheelRadius);
m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);
connectionPointCS0 = btVector3(1 - (-0.8*wheelWidth), connectionHeight, -3 * 1 + wheelRadius);
m_vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, m_tuning, isFrontWheel);
for (int i = 0; i < m_vehicle->getNumWheels(); i++)
{
btWheelInfo& wheel = m_vehicle->getWheelInfo(i);
wheel.m_suspensionStiffness = suspensionStiffness;
wheel.m_wheelsDampingRelaxation = suspensionDamping;
wheel.m_wheelsDampingCompression = suspensionCompression;
wheel.m_frictionSlip = wheelFriction;
wheel.m_rollInfluence = rollInfluence;
}
}
int wheelIndex = 2;
m_vehicle->applyEngineForce(gEngineForce, wheelIndex);
m_vehicle->setBrake(gBreakingBackForce, wheelIndex);
wheelIndex = 3;
m_vehicle->applyEngineForce(gEngineForce, wheelIndex);
m_vehicle->setBrake(gBreakingBackForce, wheelIndex);
wheelIndex = 0;
m_vehicle->setSteeringValue(gVehicleSteering, wheelIndex);
m_vehicle->setBrake(gBreakingFrontForce, wheelIndex);
wheelIndex = 1;
m_vehicle->setSteeringValue(gVehicleSteering, wheelIndex);
m_vehicle->setBrake(gBreakingFrontForce, wheelIndex);
This is how I render the vehicle:
// render wheels
btScalar mwheel[16];
for (int i = 0; i<m_vehicle->getNumWheels(); i++){
//synchronize the wheels with the (interpolated) chassis worldtransform
m_vehicle->updateWheelTransform(i, true);
//draw wheels
m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(mwheel);
RenderWheel(m_wheelShape, mwheel);
}
// render car chassis
btScalar mchassis[16];
m_vehicle->getChassisWorldTransform().getOpenGLMatrix(mchassis);
RenderBox(chassisShape, mchassis);
// render turret
btScalar mturret[16];
// get chassis and turret transforms
btTransform chassisTransform = m_vehicle->getChassisWorldTransform();
//btTransform turretTransform = compound->getChildTransform(1);
btTransform turretTransform = turretBody->getWorldTransform();
// multiply transforms to get updated turret transform
//turretTransform *= chassisTransform;
turretTransform.getOpenGLMatrix(mturret);
RenderBox(turretShape, mturret);
Using the arrow keys I apply break or acceleration forces like this:
bullet.gEngineForce = bullet.maxEngineForce;
bullet.gBreakingFrontForce = 0.f;
bullet.gBreakingBackForce = 0.f;
etc.
The camera is attached to the turret on top of the car, therefore attached to the car itself.
The issue is that when the car starts moving and as it gain speed, the wheels seem to fall behind and the chassis going forward. I would like make the wheels and the chassis more tight, but playing around with the car suspension settings did not help.
Here is a video demonstrating this behaviour: click
I have also noticed another strange behaviour related to the way the car pitches when moving, breaking and turning.
Increasing the gravity accentuates this behaviour. when I accelerate, the chassis pitches forward and when I brake it goes backwards. This is obviously wrong as it should be the other way around. Same happens when turning left, the car pitches left instead of right, and the other way around.
Here is another video demonstrating this behaviour: click
i had this kind of problem. it turned out that wheels were rendered using transformation not from the same frame as chassis. i.e. from frame n-1 while chassis used frame n.
things werent apparent bcause wheel meshes were child nodes of chassis node, as it was how it was implemented by me in rendering engine, while in bullet physics, wheel coordinates are global, the same way chassis is.
but that wasnt all, when converting wheel coords from global to local vehicle space, there was accuracy loss that, still caused problems. it was fixed by making wheel meshes as global nodes.