Android 物理体通过每个
我正在和Andengine+Box2D一起做一个小项目。我有一个玩家角色和一个地面。地面是一个静态物体,而plyaer是动态物体。我想要一个人在地上行走的效果 问题是,当我移动播放器时,它正在下沉。我如何修复它?我必须用图片解释它 这是我的密码:Android 物理体通过每个,android,box2d,andengine,Android,Box2d,Andengine,我正在和Andengine+Box2D一起做一个小项目。我有一个玩家角色和一个地面。地面是一个静态物体,而plyaer是动态物体。我想要一个人在地上行走的效果 问题是,当我移动播放器时,它正在下沉。我如何修复它?我必须用图片解释它 这是我的密码: import org.andengine.engine.camera.Camera; import org.andengine.engine.options.EngineOptions; import org.andengine.engine.op
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
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.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
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.MassData;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
public class BlokKirmaActivity extends SimpleBaseGameActivity {
private EngineOptions engineOptions;
private Camera camera;
private Scene oyunSahne;
private static final int CAMERA_HEIGHT = 480;
private static final int CAMERA_WIDTH = 800;
private PhysicsWorld physicsWorld= new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH),false);
private FixtureDef fixDef = PhysicsFactory.createFixtureDef(1.0f, 0.0f, 1.0f);
private BitmapTextureAtlas texYer,texKamil,texSol,texSag,texZipla;
private TextureRegion texRegYer,texRegKamil,texRegSol,texRegSag,texRegZipla;
private Sprite spriteYer,spriteKamil,spriteSol,spriteSag,spriteZipla;
private Body bodyYer,bodyKamil;
private boolean solBasilimi=false,sagBasilimi=false,ziplaBasilimi=false;
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
engineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(),camera);
engineOptions.getTouchOptions().setNeedsMultiTouch(true);
engineOptions.getAudioOptions().setNeedsSound(true);
return this.engineOptions;
}
@Override
protected void onCreateResources() {
texYer = new BitmapTextureAtlas(this.getTextureManager(),1024,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texKamil = new BitmapTextureAtlas(this.getTextureManager(),64,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texSol = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texSag = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texZipla = new BitmapTextureAtlas(this.getTextureManager(),128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texYer.load();
texKamil.load();
texSol.load();
texSag.load();
texZipla.load();
texRegYer = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texYer, this, "gfx/oyunyer.png",0,0);
texRegKamil = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texKamil, this, "gfx/kamil.png",0,0);
texRegSol = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texSol, this, "gfx/sol.png",0,0);
texRegSag = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texSag, this, "gfx/sag.png",0,0);
texRegZipla = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.texZipla, this, "gfx/zipla.png",0,0);
}
@Override
protected Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
this.oyunSahne = new Scene();
this.oyunSahne.registerUpdateHandler(physicsWorld);
spriteYer = new Sprite(0,355,texRegYer,getVertexBufferObjectManager());
spriteKamil = new Sprite(220,50,texRegKamil,getVertexBufferObjectManager()){
protected void onManagedUpdate(float pSecondsElapsed){
if(solBasilimi==true){
bodyKamil.setLinearVelocity(-10, 0);
}
if(sagBasilimi==true){
bodyKamil.setLinearVelocity(10, 0);
}
}
};
spriteSol = new Sprite(100,50,texRegSol,getVertexBufferObjectManager()){
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
solBasilimi=true;
}
if(pSceneTouchEvent.isActionUp()){
solBasilimi=false;
}
return true;
}
};
spriteSag = new Sprite(300,50,texRegSag,getVertexBufferObjectManager()){
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
sagBasilimi=true;
}
if(pSceneTouchEvent.isActionUp()){
sagBasilimi=false;
}
return true;
}
};
spriteZipla = new Sprite(600,50,texRegZipla,getVertexBufferObjectManager()){
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()){
bodyKamil.setLinearVelocity(0,-20);
}
if(pSceneTouchEvent.isActionUp()){
bodyKamil.setLinearVelocity(0,0);
}
return true;
}
};
bodyYer = PhysicsFactory.createBoxBody(physicsWorld, spriteYer, BodyType.StaticBody, fixDef);
this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spriteYer, bodyYer,true,false));
bodyKamil = PhysicsFactory.createBoxBody(physicsWorld, spriteKamil, BodyType.DynamicBody, fixDef);
this.physicsWorld.registerPhysicsConnector(new PhysicsConnector(spriteKamil, bodyKamil,true,false));
MassData massData1 = bodyKamil.getMassData();
massData1.mass= 75;
bodyKamil.setMassData(massData1);
this.oyunSahne.attachChild(spriteYer);
this.oyunSahne.attachChild(spriteKamil);
this.oyunSahne.attachChild(spriteSol);
this.oyunSahne.attachChild(spriteSag);
this.oyunSahne.attachChild(spriteZipla);
this.oyunSahne.registerTouchArea(spriteSol);
this.oyunSahne.registerTouchArea(spriteSag);
this.oyunSahne.registerTouchArea(spriteZipla);
return this.oyunSahne;
}
}
很可能这是因为你的物理身体的大小或形状与你的精灵不同。 我不知道任何简单的方法来修改它。可能是你必须为精灵创建自己的形状。 但是这里有一个很好的工具箱2D debug renderer供您使用,您可以使用它来获得物理体的轮廓。 使用起来非常简单,只需将项目作为库包含,然后添加一行代码即可
mScene.attachChild(new Box2dDebugRenderer(mPhysicsWorld));
现在你所有的物理身体都会显示一个轮廓,
请记住,此问题将减少FPS,但对调试非常有帮助。您需要在问题中包含更多信息,以避免问题被关闭。包括一个外部网站的链接也不能代替发布一个完整的问题。所以,没有人知道或者我不能很好地解释我的问题?谢谢你的回答,但我找不到下载链接。顺便说一下,它说Box2DebugRenderer已过时。是否有更新的版本?很抱歉,错误的链接编辑了文章,并将新链接添加到pastebin copy并使用它:)