[英]libgdx and Box2d collision optimisation
I am new to Libgdx and I am a little confused on the best way to do collision/movement. 我是Libgdx的新手,对碰撞/运动的最佳方法有些困惑。 And it is bothering me a lot and I cant seem to move on from there unless I know I am not doing it the wrong way. 这让我很困扰,除非我知道我没有做错方法,否则我似乎无法继续前进。
This is my current classes that I am using. 这是我目前正在使用的课程。
public class Play extends GameState{
public Play(GameStateManager {
super(gsm);
world = new World(new Vector2(0, -9.81f), true);
b2dr = new Box2DDebugRenderer();
cl = new MyContactListener();
world.setContactListener(cl);
createPlayer();
b2dCam = new OrthographicCamera();
b2dCam.setToOrtho(false, Game.V_WIDTH / PPM, Game.V_HEIGHT / PPM);
}
public void update(float dt) {
handleInput();
player.update(Gdx.graphics.getDeltaTime());
world.step(dt, 6, 2);
}
public void render() {
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
tmr.setView(cam);
tmr.render();
sb.setProjectionMatrix(cam.combined);
player.render(sb);
b2dr.render(world, b2dCam.combined);
}
public void handleInput() {
if(MyInput.isPressed(MyInput.BUTTON1)){
if(cl.isPlayerOnGround()){
player.getBody().applyForceToCenter(0, 150, true);
}
}
if(MyInput.isDown(MyInput.BUTTON2)){
if(cl.isPlayerOnGround())
player.setVelocity(new Vector2(1, player.getVelocity().y));
else if(Math.abs(player.getVelocity().y) < .3)
player.getBody().applyLinearImpulse(.15f, 0, player.getPosition().x, player.getPosition().y, true);
}else{
if(MyInput.isDown(MyInput.BUTTON3)){
if(cl.isPlayerOnGround()) {
player.setVelocity(new Vector2(-1, player.getVelocity().y));
}else if(Math.abs(player.getVelocity().y) < .3){
player.getBody().applyLinearImpulse(-.15f, 0, player.getPosition().x, player.getPosition().y, true);
}
}else{
if(cl.isPlayerOnGround())
player.setVelocity(new Vector2(0, player.getVelocity().y));
}
}
}
}
My Player class extends this 我的播放器类对此进行了扩展
public class AnimationHandler {
protected Body body;
protected Animation animation;
protected float width;
protected float height;
private float time;
public AnimationHandler(Body body){
this.body = body;
animation = new Animation(1/12f);
}
public void setAnimation(TextureRegion[] region, float delay){
animation = new Animation(delay, region);
width = region[0].getRegionWidth();
height = region[0].getRegionHeight();
}
public void render(SpriteBatch sb){
time += Gdx.graphics.getDeltaTime();
sb.begin();
sb.draw(
animation.getKeyFrame(time, true),
body.getPosition().x * B2DVars.PPM - width / 2,
body.getPosition().y * B2DVars.PPM - width / 2
);
sb.end();
}
public Body getBody() {
return body;
}
So the main thing I wanted to take a look at is in The first class (Play) method handleInput(). 因此,我主要要看的是第一类(播放)方法handleInput()。 I am currently using player.setVelocity() as movement. 我目前正在使用player.setVelocity()作为运动。 What this does for me is it automatically does collision handling for walking left and right. 这对我来说是自动为左右行走进行碰撞处理。 And it works pretty well I admit. 我承认,它运作良好。 But I want someones opinion on this, I keep having a feeling there are flaws in the way I am doing it. 但是我希望有人对此发表意见,我一直感觉我的做法存在缺陷。 One flaw I was thinking about is if I have a platformer type game and want to jump up through a platform but land on it when I come back down I am not sure that is possible.. Because it is doing the collision itself instead of manually doing it. 我正在考虑的一个缺陷是,如果我有一款平台游戏,并且想跳上平台,但是当我掉下来时又落在平台上,我不确定这是可能的。因为它本身在进行碰撞而不是手动进行碰撞正在做。 Any thoughts? 有什么想法吗? Any input would be really appreciated, I am new to game programming and I want to be doing things the best way possible. 任何输入都会受到感激,我是游戏编程的新手,我想以最好的方式做事。
I'd say Box2D is very well suited for making a platformer (I know from my own experience). 我想说Box2D非常适合制作平台游戏(根据我的经验知道)。 A great starting point is described here: http://www.badlogicgames.com/wordpress/?p=2017 这里描述了一个很好的起点: http : //www.badlogicgames.com/wordpress/?p=2017
Also, try to prevent creating unnecessary new objects to reduce gc pressure: player.setVelocity(new Vector2(1, player.getVelocity().y)); 另外,请尝试防止创建不必要的新对象以降低gc压力:player.setVelocity(new Vector2(1,player.getVelocity()。y));
This creates a new Vector2 every time and should be avoided because it generates garbage. 每次都会创建一个新的Vector2,并且应避免使用它,因为它会产生垃圾。 In this case body.setLinearVelocity(vx, vy); 在这种情况下,body.setLinearVelocity(vx,vy); would be preferable 会更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.