Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
Can you describe what the problem is? – Byte56 Feb 21 at 18:56
I try to create bicycle. – Siddharth Feb 21 at 19:02
Please I add more detail in question. – Siddharth Feb 21 at 19:05
"It doesn't look good" is kind of a hard problem to solve. Maybe you can be more specific than that? – Byte56 Feb 21 at 19:11
Only this thing affect the game play. So what next I can provide? – Siddharth Feb 21 at 19:13
show 4 more comments

closed as not a real question by Byte56, mh01, Sean Middleditch, Trevor Powell, Josh Petrie Feb 22 at 16:18

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

Are you writing this for windows phone? if so i suggest you use Farseer you can add joints to the bicycle frame and wheel and apply gravity relative to the direction the device is being held. I am not sure if i exactly answered your question but Most people would go for Farseer in this situation. The engine is similar in terms of how you implement it if not better(less lines of code to achieve what you want in this situation). If this doesn't Answer your question then could you provide some detail as to what the "output doesn't look good" is maybe.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.