我使用sensorManager在我的应用程序中移动精灵,但精灵不会与我在PhysicsWorld中注册的任何对象发生碰撞。这是我的完整代码:
public class MainActivity extends SimpleLayoutGameActivity implements IOnSceneTouchListener, SensorEventListener {
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 450;
private PhysicsEditorShapeLibrary physicsEditorShapeLibrary;
private static final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(1f, 0.5f, 0.5f);
private BitmapTextureAtlas mBitmapTextureAtlas;
private Camera mCamera;
public Scene scene;
private TextureRegion texturepolygon1,mFaceTextureRegion,sfondoregion;
private Sprite face,sfondosprite,polygon;
private SensorManager sensorManager;
private PhysicsWorld mPhysicsWorld;
private float accellerometerSpeedX ;
private float accellerometerSpeedY ;
float sX;
float sY;
private HashMap<String, TextureRegion> textureRegionHashMap = new HashMap<String, TextureRegion>();
@Override
public EngineOptions onCreateEngineOptions() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new FillResolutionPolicy(), this.mCamera);
return engineOptions;
}
@Override
public void onCreateResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR);
this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "pallina.png", 0, 0);
textureRegionHashMap.put("pallina", this.mFaceTextureRegion);
this.mBitmapTextureAtlas.load();
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), CAMERA_WIDTH, CAMERA_HEIGHT, TextureOptions.BILINEAR);
this.texturepolygon1 = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "livello1.png", 0, 0);
textureRegionHashMap.put("livello1", this.texturepolygon1);
this.mBitmapTextureAtlas.load();
this.physicsEditorShapeLibrary = new PhysicsEditorShapeLibrary();
this.physicsEditorShapeLibrary.open(this, "shapes/shapes.xml");
this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), CAMERA_WIDTH, CAMERA_HEIGHT, TextureOptions.BILINEAR);
this.sfondoregion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "sfondo.png", 0, 0);
this.mBitmapTextureAtlas.load();
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final LoopEntityModifier entityModifier = new LoopEntityModifier(new ParallelEntityModifier(new RotationModifier(6, 0, 360), new SequenceEntityModifier(new ScaleModifier(3, 1, 1.5f), new ScaleModifier(3, 1.5f, 1))));
final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
this.scene = new Scene();
scene.setBackground(new Background(Color.WHITE));
scene.setOnSceneTouchListener(this);
sfondosprite = new Sprite(0, 0, sfondoregion, this.getVertexBufferObjectManager());
scene.attachChild(sfondosprite);
//
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 0), false);
//fisica//
sensorManager = (SensorManager) this.getSystemService(Context.SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_GAME);
//
final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
final Rectangle right = new Rectangle(CAMERA_WIDTH - 8, 0, 8, CAMERA_HEIGHT, vertexBufferObjectManager);
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);
this.scene.attachChild(ground);
this.scene.attachChild(roof);
this.scene.attachChild(left);
this.scene.attachChild(right);
this.scene.registerUpdateHandler(this.mPhysicsWorld);
//bordi
sX = CAMERA_WIDTH / 2;
sY = CAMERA_HEIGHT / 2;
/* Create A spinning rectangle and a line. */
final Rectangle centerRectangle = new Rectangle(centerX - 50, centerY - 16, 32, 32, this.getVertexBufferObjectManager());
centerRectangle.registerEntityModifier(entityModifier);
scene.attachChild(centerRectangle);
final Line line = new Line(centerX + 50 - 16, centerY, centerX + 50 + 16, centerY, this.getVertexBufferObjectManager());
line.registerEntityModifier(entityModifier.deepCopy());
scene.attachChild(line);
/* Create A spinning rectangle and a line. */
this.polygon = new Sprite(0, 0, this.texturepolygon1, this.getVertexBufferObjectManager());
scene.attachChild(polygon);
Body body2 = this.physicsEditorShapeLibrary.createBody("livello1", polygon, this.mPhysicsWorld);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(polygon,body2,false,false) );
this.face = new Sprite(sX,sY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
scene.attachChild(face);
Body body1 = this.physicsEditorShapeLibrary.createBody("pallina", face, this.mPhysicsWorld);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,body1,false,true) );
/* The actual collision-checking. */
scene.registerUpdateHandler(new IUpdateHandler() {
@Override
public void onUpdate(final float pSecondsElapsed) {
updateSpritePosition();
final EngineLock engineLock = MainActivity.this.mEngine.getEngineLock();
engineLock.lock();
if(centerRectangle.collidesWith(face)) {
centerRectangle.setColor(1, 0, 0);
} else {
centerRectangle.setColor(0, 1, 0);
}
if(line.collidesWith(face)){
line.setColor(1, 0, 0);
} else {
line.setColor(0, 1, 0);
}
}
@Override
public void reset() { }
});
return scene;
}
public void onSensorChanged(SensorEvent event) {
accellerometerSpeedX = (int) event.values[1];
accellerometerSpeedY = (int) event.values[0];
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
private void updateSpritePosition() {
// Set the Boundary limits
int tL = 0;
int lL = 0;
int rL = CAMERA_WIDTH - (int) face.getWidth();
int bL = CAMERA_HEIGHT - (int) face.getHeight();
// Calculate New X,Y Coordinates within Limits
if (sX >= lL)
sX += accellerometerSpeedX*2;
else
sX = lL;
if (sX <= rL)
sX += accellerometerSpeedX*2;
else
sX = rL;
if (sY >= tL)
sY += accellerometerSpeedY*2;
else
sY = tL;
if (sY <= bL)
sY += accellerometerSpeedY*2;
else
sY = bL;
// Double Check That New X,Y Coordinates are within Limits
if (sX < lL)
sX = lL;
else if (sX > rL)
sX = rL;
if (sY < tL)
sY = tL;
else if (sY > bL)
sY = bL;
polygon.setPosition(0, 0);
face.setPosition(sX, sY);
}
精灵“脸部”完美移动但覆盖任何登记的障碍物,如墙壁和多边形