2

Box2d/AndEngine で角速度の大きさを設定する方法がわかりません。私はこれを持っています:

最終的な float omegaFactor = (float) 1.0; ballBody.setAngularVelocity((float) ( (Math.PI) * omegaFactor) );

omegaFactor > 0 の場合、ボディは一方向に回転します。omegaFactor を -1.0 に変更すると、ボディは逆方向に回転します。これは良いことです。ただし、大きさを変更しても効果はありません。たとえば、値 1111.0 は 1.0 と同じ動作を示します。body は DynamicBody であることに注意してください。スピンの速度に影響を与えるために何をする必要がありますか? テストコード全体は次のとおりです。

package example.bouncingball;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;

import android.hardware.SensorManager;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

/**
 * Demonstrate some physics
 * Image file: assets/gfx/ball.png 128x128
 *
 */
public class MainActivity extends SimpleBaseGameActivity implements IAccelerationListener {

    // ===========================================================
    // Fields
    // ===========================================================
    private Scene scene;

    private PhysicsWorld mPhysicsWorld;

    private TextureRegion mBallTextureRegion;
    private Sprite mBallSprite;

    private final int LEFT_EDGE = 0;
    private final int CAMERA_WIDTH = 720; // right of screen

    private final int TOP_OF_SCREEN = 0;
    private final int CAMERA_HEIGHT = 480; // bottom of screen

    public EngineOptions onCreateEngineOptions() {

        final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

        final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);

        return engineOptions;

    }

    @Override
    protected void onCreateResources() {

        try {
            BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

            setBallTexture();

        }
        catch (Exception exception) {
            System.out.println ("MainActivity.onCreateResources(): " + exception);
            Debug.d("MainActivity.onCreateResources(): " + exception);
        }
    }

    @Override
    protected Scene onCreateScene() {

        scene = new Scene();

        final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();

        // physics world
        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);

        final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager);
        //final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager); // roof at top of screen
        // y < 0 so as to lift the roof to start the ball off-screen
        final Rectangle roof = new Rectangle(0, -200, CAMERA_WIDTH, 2, vertexBufferObjectManager);
        final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
        final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);

        final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
        PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

        scene.attachChild(ground);
        scene.attachChild(roof);
        scene.attachChild(left);
        scene.attachChild(right);

        scene.registerUpdateHandler(this.mPhysicsWorld);

        // end physics world

        // if we use body.Transform() then x,y do not matter here 
        // except that it appears to flash at the x,y defined here before the transform
        // so set it to the same
        final int startX = CAMERA_WIDTH - 160;
        mBallSprite = new Sprite( startX, 
                TOP_OF_SCREEN - 100,  // off screen
                this.mBallTextureRegion, 
                getVertexBufferObjectManager());

        final Sprite ballFace = new Sprite( startX, TOP_OF_SCREEN - 100, this.mBallTextureRegion, this.getVertexBufferObjectManager());

        // increasing elasticity makes the ball more 'bouncy'
        // if friction is 0, it doesn't roll on the floor, it just spins in one spot
        // if friction is < 0, the ball disappears when it hits the floor, or wall presumably
        final FixtureDef BALL_FIXTURE_DEF = PhysicsFactory.createFixtureDef(
                1,     // density
                0.5f,  // elasticity 
                0.5f); // friction

        final Body ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ballFace, BodyType.DynamicBody, BALL_FIXTURE_DEF);

        // Angular velocity - omega of the object is the change of angle with respect to time
        // Angular velocity is measured in radians per second
        // omegaFactor:  2.0: 2 revolutions per second, clockwise
        //              -0.5: 1/2 revolution per second, counterclockwise
        final float omegaFactor = (float) 1.0;
        ballBody.setAngularVelocity((float) ( (Math.PI) * omegaFactor) ); 

        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mBallSprite, ballBody, true, true));

        scene.attachChild(mBallSprite);

        return scene;
    }

    private void setBallTexture() throws IOException {

        ITexture texture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {

            @Override
            public InputStream open() throws IOException {
                return getAssets().open("gfx/ball.png");
            }
        });

        texture.load();
        this.mBallTextureRegion = TextureRegionFactory.extractFromTexture(texture);

    }

    // IAccelerationListener interface
    @Override
    public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {
    }

    @Override
    public void onAccelerationChanged(final AccelerationData pAccelerationData) {
    }
    // end IAccelerationListener interface

}
4

1 に答える 1

2

私はエミュレータだけを見ていました。実機で見てみるとスピンの方が早い。

于 2012-12-27T20:42:40.623 に答える