I am developing side scrolling race game.
For that I create bicycle but it can't work perfect as my assumption. Following are code for it.
public class Cycle {
private float accelerate = 0f, totalElapsedTime = 0f, scaleFactor = 0.6f;
private TextureManager textureManager;
private Sprite frameSprite, wheelSprite;
private Scene mScene;
private PhysicsWorld mPhysicsWorld;
private Body frameBody, frontWheelBody, rearWheelBody;
private Vector2 position;
private RevoluteJoint frontWheelJoint, rearWheelJoint;
private DoodleByke doodleByke;
public Cycle(Vector2 position, Scene mScene, PhysicsWorld mPhysicsWorld,
TextureManager textureManager, DoodleByke doodleByke) {
this.textureManager = textureManager;
this.mScene = mScene;
this.mPhysicsWorld = mPhysicsWorld;
this.position = position;
this.doodleByke = doodleByke;
createFrame();
createWheel();
createJoint();
}
private void createFrame() {
final BoundCamera mCamera = (BoundCamera) doodleByke.getEngine()
.getCamera();
frameSprite = new Sprite(position.x * Utility.widthAspectRatio,
position.y * Utility.heightAspectRatio,
textureManager.frameRegion.getWidth() * Utility.factor
* scaleFactor, textureManager.frameRegion.getHeight()
* Utility.factor * scaleFactor,
textureManager.frameRegion) {
@Override
protected void onManagedUpdate(float pSecondsElapsed) {
// camera movement
if ((frameSprite.getX() + frameSprite.getWidth() * 2f) > (Utility.camera_width * 0.5f))
mCamera.setCenter(
frameSprite.getX() + (frameSprite.getWidth() * 2f),
mCamera.getCenterY());
totalElapsedTime += pSecondsElapsed;
// acceleration
if (totalElapsedTime > 0.1f) {
accelerate = rearWheelJoint.getMotorSpeed();
if (Utility.isAccelerate && accelerate > -40f) {
accelerate -= 2f;
rearWheelJoint.setMotorSpeed(accelerate);
}
totalElapsedTime = 0f;
}
super.onManagedUpdate(pSecondsElapsed);
}
};
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(
(position.x * Utility.widthAspectRatio + frameSprite
.getWidthScaled() * 0.5f)
/ PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
(position.y * Utility.heightAspectRatio + frameSprite
.getHeightScaled() * 0.5f)
/ PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox((25f * Utility.factor)
/ PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
(5f * Utility.factor)
/ PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density = 2f;
fixtureDef.friction = 0.2f;
fixtureDef.restitution = 0.3f;
fixtureDef.shape = polygonShape;
frameBody = mPhysicsWorld.createBody(bodyDef);
MassData frameData = frameBody.getMassData();
frameData.center.set(-1.5f, 0f);
frameBody.setMassData(frameData);
frameBody.setUserData(Constant.USERDATA_FRAME);
frameBody.createFixture(fixtureDef);
frameBody.setActive(false);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
frameSprite, frameBody, true, true));
mScene.attachChild(frameSprite);
}
private void createWheel() {
final FixtureDef FRONT_WHEEL_FIXTUREDEF = PhysicsFactory
.createFixtureDef(2f, 0.1f, 0.8f);
final FixtureDef REAR_WHEEL_FIXTUREDEF = PhysicsFactory
.createFixtureDef(1f, 0.1f, 0.8f);
// front wheel
wheelSprite = new Sprite(
(position.x * Utility.widthAspectRatio + textureManager.wheelRegion.getWidth()
* 0.7f * Utility.factor),
(position.y * Utility.heightAspectRatio + textureManager.wheelRegion
.getHeight() * 0.3f * Utility.factor),
textureManager.wheelRegion.getWidth() * Utility.factor
* scaleFactor, textureManager.wheelRegion.getHeight()
* Utility.factor * scaleFactor,
textureManager.wheelRegion.deepCopy());
frontWheelBody = PhysicsFactory.createCircleBody(mPhysicsWorld,
wheelSprite, BodyType.DynamicBody, FRONT_WHEEL_FIXTUREDEF);
frontWheelBody.setUserData(Constant.USERDATA_WHEEL);
frontWheelBody.setActive(false);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
wheelSprite, frontWheelBody, true, true));
mScene.attachChild(wheelSprite);
// rear wheel
wheelSprite = new Sprite(
position.x
* Utility.widthAspectRatio
- (textureManager.wheelRegion.getWidth() * 0.25f * Utility.factor),
position.y
* Utility.heightAspectRatio
+ (textureManager.wheelRegion.getHeight() * 0.35f * Utility.factor),
textureManager.wheelRegion.getWidth() * Utility.factor
* scaleFactor, textureManager.wheelRegion.getHeight()
* Utility.factor * scaleFactor,
textureManager.wheelRegion.deepCopy());
rearWheelBody = PhysicsFactory.createCircleBody(mPhysicsWorld,
wheelSprite, BodyType.DynamicBody, REAR_WHEEL_FIXTUREDEF);
rearWheelBody.setUserData(Constant.USERDATA_WHEEL);
rearWheelBody.setActive(false);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(
wheelSprite, rearWheelBody, true, true));
mScene.attachChild(wheelSprite);
}
private void createJoint() {
RevoluteJointDef frontWheelDef = new RevoluteJointDef();
frontWheelDef.initialize(frontWheelBody, frameBody,
frontWheelBody.getWorldCenter());
frontWheelDef.localAnchorA.set(0f, 0f);
frontWheelDef.localAnchorB.set(0.95f * Utility.factor * scaleFactor,
0.24f * Utility.factor * scaleFactor);
frontWheelDef.enableMotor = true;
frontWheelDef.collideConnected = false;
frontWheelJoint = (RevoluteJoint) mPhysicsWorld
.createJoint(frontWheelDef);
RevoluteJointDef rearWheelDef = new RevoluteJointDef();
rearWheelDef.initialize(rearWheelBody, frameBody,
rearWheelBody.getWorldCenter());
rearWheelDef.localAnchorA.set(0f, 0f);
rearWheelDef.localAnchorB.set(-0.9f * Utility.factor * scaleFactor,
0.55f * Utility.factor * scaleFactor);
rearWheelDef.collideConnected = false;
rearWheelDef.enableMotor = false;
rearWheelDef.maxMotorTorque = 20f;
rearWheelDef.motorSpeed = 0f;
rearWheelJoint = (RevoluteJoint) mPhysicsWorld
.createJoint(rearWheelDef);
}
public void activeCycle() {
frameBody.setActive(true);
frontWheelBody.setActive(true);
rearWheelBody.setActive(true);
}
public void breakCycle() {
doodleByke.runOnUpdateThread(new Runnable() {
@Override
public void run() {
mPhysicsWorld.destroyJoint(frontWheelJoint);
mPhysicsWorld.destroyJoint(rearWheelJoint);
}
});
}
public Sprite getFrameSprite() {
return frameSprite;
}
public RevoluteJoint getRearWheelJoint() {
return rearWheelJoint;
}
public Body getFrameBody() {
return frameBody;
}
public float getAccelerate() {
return accelerate;
}
public void setAccelerate(float accelerate) {
this.accelerate = accelerate;
}
}
Following is the fixture definition of the terrain.
fixtureDef.density = 1f;
fixtureDef.restitution = 0.1f;
fixtureDef.friction = 0.4f;
Also there is no wheel joint implementation in AndEngine, this is the main problem in creating any vehicle.
EDIT : Based on accelerometer data I move my bicycle. I applied and test applyAngularImpulse and setAngularVelocity to cycle frame body. But the game output does not look good. Like other market game I don't able to create this one. My game was so easy to play though there is no fault of designer. So how to make game hard and become interesting for player. I this my bicycle was not properly constructed and accelerometer data provided might be wrong
So please guide me to create cycle.

