android - box2d 角速度

标签 android box2d andengine

我不明白有关在 Box2d/AndEngine 中设置角速度大小的信息。我有这个:

最终 float omegaFactor = (float) 1.0; ballBody.setAngularVelocity((float) ( (Math.PI) * omegaFactor) );

当 omegaFactor > 0 时, body 朝一个方向旋转。如果我将 omegaFactor 更改为 -1.0, body 会向相反方向旋转,这很好。然而,改变幅度没有效果。例如,值 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

}

最佳答案

我只在模拟器上查看。当我在实际设备上查看时,旋转速度更快。

关于android - box2d 角速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13159561/

相关文章:

java - Parcelable 对象错误 : Unmarshalling unknown type code *** at offset ***

android - 无法使用 Android Studio 导入 ViewPagerIndicator 库

Java 泛型——这个语法有什么用?

java - 如何修复此错误? "java.lang.IndexOutOfBoundsException"

java - 在 isActionMove() 上执行单点触摸

android - 在不对每个 Web 服务调用使用 SSL 加密的情况下保持持久登录

java - 断言失败 : (d + h * k > 1. 19209290e-7F),函数 InitVelocityConstraints 文件 ... b2MouseJoint.cpp,第 125 行

box2d - Box2D 中 "ground body"的用途?

python - Gym's box 2d (openAI) 安装不成功(pip错误)

android - 在 Andengine 中移动 Sprite