LevelHelper Car Design Революционная проблема соединения анкеров

Я пытаюсь повторить эффект по этой ссылке Боковое движение автомобиля Я использую levelHelper для дизайна автомобиля. но не в состоянии воспроизвести эффект. Я думаю, что я делаю ошибку в суставах. Вот код, который я пытаюсь

#include "GamePlayScene.h"#include "LevelManager.h"#include "Constants.h"#include "iostream"void GamePlayScene::InitPhysics()
{
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
m_pB2World = new b2World(gravity);
m_pB2World->SetAllowSleeping(true);
m_pB2World->SetContinuousPhysics(true);
m_pDebugDraw = new GLESDebugDraw( LevelHelperLoader::pixelsToMeterRatio() );

PTM_RATIO = LevelHelperLoader::pixelsToMeterRatio();
m_pB2World->SetDebugDraw(m_pDebugDraw);m_pLoader= new LevelHelperLoader("track7.plhs");
m_pLoader->addObjectsToWorld(m_pB2World,this);

m_pB2World->ClearForces();

//m_pB2World->DestroyJoint()

LHSprite * carBody = m_pLoader->spriteWithUniqueName("CarBody");
m_pCarBody = carBody->getBody();

m_pCarBody->GetFixtureList()->SetDensity(CAR_BODY_DENSITY);
m_pCarBody->ResetMassData();
m_pCarBody->GetFixtureList()->SetFriction(CAR_BODY_FRICTION);
m_pCarBody->GetFixtureList()->SetRestitution(CAR_BODY_RESTITUTION);

LHSprite * chasis = m_pLoader->spriteWithUniqueName("Chasis");
m_pChasis = chasis->getBody();

m_pChasis->GetFixtureList()->SetDensity(CHASIS_DENSITY);
m_pChasis->ResetMassData();
m_pChasis->GetFixtureList()->SetFriction(CHASIS_FRICTION);
m_pChasis->GetFixtureList()->SetRestitution(CHASIS_RESTITUTION);

//m_pChasis->remove

LHSprite * frontWheel = m_pLoader->spriteWithUniqueName("FrontWheel");
m_pFrontWheel = frontWheel->getBody();

m_pFrontWheel->GetFixtureList()->SetDensity(FRONT_WHEEL_DENSITY);
m_pFrontWheel->ResetMassData();
m_pFrontWheel->GetFixtureList()->SetFriction(FRONT_WHEEL_FRICTION);
m_pFrontWheel->GetFixtureList()->SetRestitution(FRONT_WHEEL_RESTITUTION);

LHSprite * rearWheel = m_pLoader->spriteWithUniqueName("RearWheel");
m_pRearWheel = rearWheel->getBody();

m_pRearWheel->GetFixtureList()->SetDensity(REAR_WHEEL_DENSITY);
m_pRearWheel->ResetMassData();
m_pRearWheel->GetFixtureList()->SetFriction(REAR_WHEEL_FRICTION);
m_pRearWheel->GetFixtureList()->SetRestitution(REAR_WHEEL_RESTITUTION);

LHSprite * frontDamper = m_pLoader->spriteWithUniqueName("FrontDamper");
m_pFrontDamper = frontDamper->getBody();

m_pFrontDamper->GetFixtureList()->SetDensity(FRONT_DAMPER_DENSITY);
m_pFrontDamper->ResetMassData();
m_pFrontDamper->GetFixtureList()->SetFriction(FRONT_DAMPER_FRICTION);
m_pFrontDamper->GetFixtureList()->SetRestitution(FRONT_DAMPER_RESTITUTION);

LHSprite * rearDamper = m_pLoader->spriteWithUniqueName("RearDamper");
m_pRearDamper = rearDamper->getBody();

m_pRearDamper->GetFixtureList()->SetDensity(REAR_DAMPER_DENSITY);
m_pRearDamper->ResetMassData();
m_pRearDamper->GetFixtureList()->SetFriction(REAR_DAMPER_FRICTION);
m_pRearDamper->GetFixtureList()->SetRestitution(REAR_DAMPER_RESTITUTION);

b2PolygonShape * shape = (b2PolygonShape *)m_pChasis->GetFixtureList()->GetShape();

//shape->SetAsBox(

LHJoint * frontPrismJoint = frontDamper->jointWithUniqueName("FrontDamperChasis");
LH_JOINT_TYPE type2 = frontPrismJoint->getType();

if(type2 == LH_PRISMATIC_JOINT)
{
//m_pFrontPrismJoint = (b2PrismaticJoint *)frontPrismJoint;
//b2PrismaticJointDef prismJointDef;

frontPrismJointDef = new b2PrismaticJointDef();

//b2Vec2 anchor = m_pFrontDamper->GetWorldCenter() - m_pChasis->GetWorldCenter();

frontPrismJointDef->Initialize(m_pChasis,m_pFrontDamper, m_pFrontDamper->GetWorldCenter(), b2Vec2(0, 1));

frontPrismJointDef->enableLimit = FRONT_PRISM_JOINT_ENABLE_LIMIT;
frontPrismJointDef->enableMotor = FRONT_PRISM_JOINT_ENABLE_MOTOR;
frontPrismJointDef->lowerTranslation = FRONT_PRISM_JOINT_LOWERTRANSLATION;
frontPrismJointDef->upperTranslation = FRONT_PRISM_JOINT_UPPERTRANSLATION;
frontPrismJointDef->maxMotorForce = FRONT_PRISM_JOINT_MAXMOTORFORCE;

//b2Joint * joint = (b2Joint *)frontPrismJoint;
//frontPrismJointDef->

m_pFrontSpring = (b2PrismaticJoint *)m_pB2World->CreateJoint(frontPrismJointDef);

//m_pB2World->DestroyJoint(joint);
}

LHJoint * rearPrismJoint = rearDamper->jointWithUniqueName("RearDamperChasis");
type2 = rearPrismJoint->getType();

if(type2 == LH_PRISMATIC_JOINT)
{
//m_pRearPrismJoint = (b2PrismaticJoint *)rearPrismJoint;

rearPrismJointDef = new b2PrismaticJointDef();

rearPrismJointDef->Initialize(m_pChasis, m_pRearDamper, m_pRearDamper->GetWorldCenter(), b2Vec2(0, 1));

rearPrismJointDef->enableLimit = REAR_PRISM_JOINT_ENABLE_LIMIT;
rearPrismJointDef->enableMotor = REAR_PRISM_JOINT_ENABLE_MOTOR;
rearPrismJointDef->lowerTranslation = REAR_PRISM_JOINT_LOWERTRANSLATION;
rearPrismJointDef->upperTranslation = REAR_PRISM_JOINT_UPPERTRANSLATION;
rearPrismJointDef->maxMotorForce = REAR_PRISM_JOINT_MAXMOTORFORCE;

m_pRearSpring = (b2PrismaticJoint *)m_pB2World->CreateJoint(rearPrismJointDef);
}

LHJoint * frontRevoluteJoint = frontWheel->jointWithUniqueName("FrontDamperWheel");
//frontWheel->jo
LH_JOINT_TYPE type = frontRevoluteJoint->getType();

if(type == LH_REVOLUTE_JOINT)
{
m_pFrontMotor = (b2RevoluteJoint *)frontRevoluteJoint;

frontRevoluteJointDef = new b2RevoluteJointDef();

frontRevoluteJointDef->Initialize(m_pFrontDamper, m_pFrontWheel, m_pFrontWheel->GetWorldCenter());

frontRevoluteJointDef->motorSpeed = FRONT_REVOLUTE_JOINT_MOTORSPEED;
frontRevoluteJointDef->maxMotorTorque = FRONT_REVOLUTE_JOINT_MAXMOTORTORQUE;
frontRevoluteJointDef->enableMotor = FRONT_REVOLUTE_JOINT_ENABLE_MOTOR;
m_fFrontMotorSpeed = FRONT_REVOLUTE_JOINT_MOTORSPEED;
m_fFrontMaxMotorTorque = FRONT_REVOLUTE_JOINT_MAXMOTORTORQUE;
m_pFrontMotor = (b2RevoluteJoint *)m_pB2World->CreateJoint(frontRevoluteJointDef);
}

LHJoint * rearRevoluteJoint = rearWheel->jointWithUniqueName("RearDamperWheel");
type = rearRevoluteJoint->getType();

if(type == LH_REVOLUTE_JOINT)
{
//m_pRearRevoluteJoint = (b2RevoluteJoint *)rearRevoluteJoint;

rearRevoluteJointDef = new b2RevoluteJointDef();

rearRevoluteJointDef->Initialize(m_pRearDamper, m_pRearWheel, m_pRearWheel->GetWorldCenter());// - b2Vec2(0.1f,0.0f));

rearRevoluteJointDef->motorSpeed = REAR_REVOLUTE_JOINT_MOTORSPEED;
rearRevoluteJointDef->maxMotorTorque = REAR_REVOLUTE_JOINT_MAXMOTORTORQUE;
rearRevoluteJointDef->enableMotor = REAR_REVOLUTE_JOINT_ENABLE_MOTOR;
m_fRearMotorSpeed = REAR_REVOLUTE_JOINT_MOTORSPEED;
m_fRearMaxMotorTorque = REAR_REVOLUTE_JOINT_MAXMOTORTORQUE;
//revoluteJointDef.
m_pRearMotor = (b2RevoluteJoint *)m_pB2World->CreateJoint(rearRevoluteJointDef);

//m_fMotorSpeed = m_pRearMotor->GetMotorSpeed();
}}

void GamePlayScene::ccTouchesBegan(cocos2d::CCSet *pTouches, CCEvent *pEvent)
{

CCTouch * touch = (CCTouch *)(pTouches->anyObject());
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);

if(location.x > mSize.width/2)
{
bIsAcceleration = true;
bIsBrake = false;

m_pFrontMotor->EnableMotor(true);
m_pRearMotor->EnableMotor(true);

//m_pFrontWheel->SetLinearVelocity(b2Vec2(-20.0f, 0.0f));

direction = 1;
oppositeDirection = -1;
}
else
{
m_pFrontMotor->EnableMotor(true);
m_pRearMotor->EnableMotor(true);
bIsAcceleration = false;
bIsBrake = true;
direction = -1;
oppositeDirection = 1;

//m_pRearWheel->SetLinearVelocity(b2Vec2(25.0f, 0.0f));
}

}
void GamePlayScene::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
//bIsAcceleration=true;
}

void GamePlayScene::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
bIsAcceleration=false;
bIsBrake = false;

//direction *= -1;
//m_pFrontMotor->EnableMotor(false);
}

void GamePlayScene::clickOnBackwardButn(CCObject * pSender)
{
bIsAcceleration = false;
m_pFrontMotor->EnableMotor(false);
m_pRearMotor->EnableMotor(false);

//m_fMotorSpeed = 0.0;
m_pRearMotor->SetMotorSpeed(-50.0f);
m_pFrontMotor->SetMotorSpeed(-50.0f);

m_pRearMotor->SetMaxMotorTorque(50.0f);
m_pFrontMotor->SetMaxMotorTorque(50.0f);
//m_pRearWheel->SetLinearVelocity(b2Vec2(-(10.0f),0.0f));
}

void GamePlayScene::clickOnForwardButn(CCObject * pSender)
{
m_pFrontMotor->EnableMotor(true);
m_pRearMotor->EnableMotor(true);
bIsAcceleration = true;

//m_pFrontWheel->SetLinearVelocity(b2Vec2(15.0f + m_fVelocityAcceleration,0.0f));
}

void GamePlayScene::draw()
{
CCLayer::draw();
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );

kmGLPushMatrix();

m_pB2World->DrawDebugData();
kmGLPopMatrix();

}

void GamePlayScene::update(float dt)
{b2Vec2 position=m_pFrontWheel->GetPosition();
int x=position.x*32.0f;
int y=position.y*32.0f;
this->setPositionX(480.0f*90/100 + -x);if(bIsAcceleration)
{m_pFrontMotor->SetMotorSpeed(30 * M_PI * direction);
m_pFrontMotor->SetMaxMotorTorque(34);
m_pRearMotor->SetMotorSpeed(30 * M_PI * direction);
m_pRearMotor->SetMaxMotorTorque(24);//m_pChasis->ApplyTorque(20 * direction);
}
else if(bIsBrake)
{m_pFrontMotor->SetMotorSpeed(30 * M_PI * direction);
m_pFrontMotor->SetMaxMotorTorque(24);
m_pRearMotor->SetMotorSpeed(30 * M_PI * direction);
m_pRearMotor->SetMaxMotorTorque(34);

//m_pCarBody->ApplyTorque(30 * direction);

}
else
{

m_pFrontMotor->SetMotorSpeed(0.0f);
m_pFrontMotor->SetMaxMotorTorque(0.5f);
m_pRearMotor->SetMotorSpeed(0.0f);
m_pRearMotor->SetMaxMotorTorque(0.5f);
}

/*if(m_fMotorSpeed >= 50.0f)
{
m_fMotorSpeed = 50.0f;
}*/m_pFrontSpring->SetMaxMotorForce(10 + abs(100 * pow(m_pFrontSpring->GetJointTranslation(), 2)));
m_pFrontSpring->SetMotorSpeed(-4*pow(m_pFrontSpring->GetJointTranslation(), 1));

m_pRearSpring->SetMaxMotorForce(30 + abs(100 * pow(m_pRearSpring->GetJointTranslation(), 2)));
m_pRearSpring->SetMotorSpeed((m_pRearSpring->GetMotorSpeed() - 10*m_pRearSpring->GetJointTranslation())*0.4);

int32 velocityIterations = 8;
int32 positionIterations = 6;
m_pB2World->Step(dt, velocityIterations, positionIterations);

}

void GamePlayScene::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]