I have a rotating triangle colliding against the 4 walls of the device. It always "reflect" on the walls with the same angle, even though it collides with different angles at the wall. Is this the expected the behavior, physics wise? I'd expect it to bounce in different directions according to it's angle. Maybe I have something configured wrong?
For the walls and triangle:
Using java Syntax Highlighting
- final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(0f, 1f, 0f);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
Thanks a lot
Edit: I've experimented making the triangle with 1f for density. The body then starts to rotate with a different velocity, but it's still bouncing in the same direction, which I find weird. It also seems to conservate energy, because when it's spinning too fast it gets very slow. Is there a way to make the linear velocity constant and be able to change the spinning speed indenpently on collisions?
Here's the whole source code
Using java Syntax Highlighting
- package org.anddev.andengine.examples;
- import static org.anddev.andengine.extension.physics.box2d.util.constants.PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
- import org.anddev.andengine.engine.Engine;
- import org.anddev.andengine.engine.camera.Camera;
- import org.anddev.andengine.engine.options.EngineOptions;
- import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
- import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
- import org.anddev.andengine.entity.primitive.Rectangle;
- import org.anddev.andengine.entity.scene.Scene;
- import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
- import org.anddev.andengine.entity.scene.background.ColorBackground;
- import org.anddev.andengine.entity.shape.Shape;
- import org.anddev.andengine.entity.sprite.AnimatedSprite;
- import org.anddev.andengine.entity.util.FPSLogger;
- import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
- import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
- import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
- import org.anddev.andengine.input.touch.TouchEvent;
- import org.anddev.andengine.opengl.texture.Texture;
- import org.anddev.andengine.opengl.texture.TextureOptions;
- import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
- import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
- import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
- import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
- import org.anddev.andengine.util.Debug;
- import android.hardware.SensorManager;
- import android.view.MotionEvent;
- import android.widget.Toast;
- import com.badlogic.gdx.math.Vector2;
- import com.badlogic.gdx.physics.box2d.Body;
- import com.badlogic.gdx.physics.box2d.FixtureDef;
- import com.badlogic.gdx.physics.box2d.PolygonShape;
- import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
- /**
- * @author Nicolas Gramlich
- * @since 18:47:08 - 19.03.2010
- */
- public class PhysicsExample extends BaseExample implements IAccelerometerListener, IOnSceneTouchListener {
- // ===========================================================
- // Constants
- // ===========================================================
- private static final int CAMERA_WIDTH = 720;
- private static final int CAMERA_HEIGHT = 480;
- // ===========================================================
- // Fields
- // ===========================================================
- private Texture mTexture;
- private TiledTextureRegion mBoxFaceTextureRegion;
- private TiledTextureRegion mCircleFaceTextureRegion;
- private TiledTextureRegion mTriangleFaceTextureRegion;
- private TiledTextureRegion mHexagonFaceTextureRegion;
- private PhysicsWorld mPhysicsWorld;
- private int mFaceCount = 0;
- // ===========================================================
- // Constructors
- // ===========================================================
- // ===========================================================
- // Getter & Setter
- // ===========================================================
- // ===========================================================
- // Methods for/from SuperClass/Interfaces
- // ===========================================================
- @Override
- public Engine onLoadEngine() {
- Toast.makeText(this, "Touch the screen to add objects.", Toast.LENGTH_LONG).show();
- final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
- return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));
- }
- @Override
- public void onLoadResources() {
- this.mTexture = new Texture(64, 128, TextureOptions.BILINEAR);
- TextureRegionFactory.setAssetBasePath("gfx/");
- this.mBoxFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32
- this.mCircleFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32
- this.mTriangleFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "face_triangle_tiled.png", 0, 64, 2, 1); // 64x32
- this.mHexagonFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mTexture, this, "face_hexagon_tiled.png", 0, 96, 2, 1); // 64x32
- this.mEngine.getTextureManager().loadTexture(this.mTexture);
- this.enableAccelerometerSensor(this);
- }
- @Override
- public Scene onLoadScene() {
- this.mEngine.registerUpdateHandler(new FPSLogger());
- final Scene scene = new Scene(2);
- scene.setBackground(new ColorBackground(0, 0, 0));
- scene.setOnSceneTouchListener(this);
- this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 0), false);
- final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
- final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
- final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
- final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
- final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1f, 1f, 0f);
- PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef); //left
- 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.getBottomLayer().addEntity(ground);
- scene.getBottomLayer().addEntity(roof);
- scene.getBottomLayer().addEntity(left);
- scene.getBottomLayer().addEntity(right);
- scene.registerUpdateHandler(this.mPhysicsWorld);
- return scene;
- }
- public void onLoadComplete() {
- }
- @Override
- public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {
- if(this.mPhysicsWorld != null) {
- if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
- this.runOnUpdateThread(new Runnable() {
- @Override
- public void run() {
- PhysicsExample.this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
- }
- });
- return true;
- }
- }
- return false;
- }
- @Override
- public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
- //this.mPhysicsWorld.setGravity(new Vector2(pAccelerometerData.getY(), pAccelerometerData.getX()));
- }
- // ===========================================================
- // Methods
- // ===========================================================
- private void addFace(final float pX, final float pY) {
- final Scene scene = this.mEngine.getScene();
- this.mFaceCount++;
- Debug.d("Faces: " + this.mFaceCount);
- final AnimatedSprite face;
- final Body body;
- final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1f, 1f, 0f);
- if(this.mFaceCount % 4 == 0) {
- face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion);
- body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
- } else if (this.mFaceCount % 4 == 1) {
- face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion);
- body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
- } else if (this.mFaceCount % 4 == 2) {
- face = new AnimatedSprite(pX, pY, this.mTriangleFaceTextureRegion);
- body = PhysicsExample.createTriangleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
- } else {
- face = new AnimatedSprite(pX, pY, this.mHexagonFaceTextureRegion);
- body = PhysicsExample.createHexagonBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
- }
- //face.animate(200);
- //face.setVelocity(100, 0);
- //face.setAcceleration(8, 8);
- body.setLinearVelocity(new Vector2(50, 0));
- body.setAngularVelocity(1f);
- face.setUpdatePhysics(true);
- scene.getTopLayer().addEntity(face);
- this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true, false, false));
- }
- /**
- * Creates a {@link Body} based on a {@link PolygonShape} in the form of a triangle:
- * <pre>
- * /\
- * /__\
- * </pre>
- */
- public static Body createTriangleBody(final PhysicsWorld pPhysicsWorld, final Shape pShape, final BodyType pBodyType, final FixtureDef pFixtureDef) {
- /* Remember that the vertices are relative to the center-coordinates of the Shape. */
- final float halfWidth = pShape.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
- final float halfHeight = pShape.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
- final float top = -halfHeight;
- final float bottom = halfHeight;
- final float left = -halfHeight;
- final float centerX = 0;
- final float right = halfWidth;
- final Vector2[] vertices = {
- new Vector2(centerX, top),
- new Vector2(right, bottom),
- new Vector2(left, bottom)
- };
- return PhysicsFactory.createPolygonBody(pPhysicsWorld, pShape, vertices, pBodyType, pFixtureDef);
- }
- /**
- * Creates a {@link Body} based on a {@link PolygonShape} in the form of a hexagon:
- * <pre>
- * /\
- * / \
- * | |
- * | |
- * \ /
- * \/
- * </pre>
- */
- public static Body createHexagonBody(final PhysicsWorld pPhysicsWorld, final Shape pShape, final BodyType pBodyType, final FixtureDef pFixtureDef) {
- /* Remember that the vertices are relative to the center-coordinates of the Shape. */
- final float halfWidth = pShape.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
- final float halfHeight = pShape.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
- /* The top and bottom vertex of the hexagon are on the bottom and top of hexagon-sprite. */
- final float top = -halfHeight;
- final float bottom = halfHeight;
- final float centerX = 0;
- /* The left and right vertices of the heaxgon are not on the edge of the hexagon-sprite, so we need to inset them a little. */
- final float left = -halfWidth + 2.5f / PIXEL_TO_METER_RATIO_DEFAULT;
- final float right = halfWidth - 2.5f / PIXEL_TO_METER_RATIO_DEFAULT;
- final float higher = top + 8.25f / PIXEL_TO_METER_RATIO_DEFAULT;
- final float lower = bottom - 8.25f / PIXEL_TO_METER_RATIO_DEFAULT;
- final Vector2[] vertices = {
- new Vector2(centerX, top),
- new Vector2(right, higher),
- new Vector2(right, lower),
- new Vector2(centerX, bottom),
- new Vector2(left, lower),
- new Vector2(left, higher)
- };
- return PhysicsFactory.createPolygonBody(pPhysicsWorld, pShape, vertices, pBodyType, pFixtureDef);
- }
- // ===========================================================
- // Inner and Anonymous Classes
- // ===========================================================
- }
Parsed in 0.067 seconds, using GeSHi 1.0.8.4

