gpt4 book ai didi

java - 如何创建 movingPlatform?

转载 作者:行者123 更新时间:2023-11-30 00:34:02 29 4
gpt4 key购买 nike

我想用 box2d 创建一个可以左右移动的平台,但我不知道怎么做?

我创建了 platform-b​​ody 并给了他 KinematicType,并尝试用 setLinearVelocity 移动它,但没有帮助

这就是我创建平台“桶”的方式:

package com.niceboy.game.Objects;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

import java.util.Random;

import static com.niceboy.game.Constants.PLATFORM_MARGIN;
import static com.niceboy.game.Constants.PPM;

public class Bucket {
private Random rand = new Random();

private World world;
private Body body;

public Bucket(World world){
this.world = world;
}

public void createBucket(int y){
BodyDef bdef = new BodyDef();
bdef.position.set(rand.nextInt(Gdx.graphics.getWidth())/PPM,PLATFORM_MARGIN/PPM*y);
bdef.type = BodyDef.BodyType.KinematicBody;

body = world.createBody(bdef);

PolygonShape box = new PolygonShape();
box.setAsBox(50/PPM,20/PPM);
FixtureDef fdef = new FixtureDef();
fdef.shape = box;

body.createFixture(fdef).setUserData("bucket");
}

public void repos(int y){
body.getPosition().set(rand.nextInt(Gdx.graphics.getWidth())/PPM,PLATFORM_MARGIN/PPM*y);
}
}

但是我不知道怎么移动它

最佳答案

kinematicBody->setLinearVelocity(1,0);//每秒向右移动1个单位

我已经创建了 KinematicBody 主体并为其提供了 PolygonShape 并应用了线速度,然后主体开始移动,当您想要改变方向时应用相同的负幅度速度,以便它开始向相反的方向移动。

public class MainGame extends InputAdapter implements ApplicationListener {

private SpriteBatch batch;
private ExtendViewport extendViewport;
private OrthographicCamera cam;

private float w=20;
private float h=22;

private World world;
private Box2DDebugRenderer debugRenderer;

private Array<Body> array;
private Vector3 vector3;
private Body platform;
Vector2 vector2;
boolean isLeft;

@Override
public void create() {

vector2=new Vector2();
isLeft=true;
cam=new OrthographicCamera();
extendViewport=new ExtendViewport(w,h,cam);

batch =new SpriteBatch();
Gdx.input.setInputProcessor(this);

world=new World(new Vector2(0,-9.8f),true);
array=new Array<Body>();
debugRenderer=new Box2DDebugRenderer();
vector3=new Vector3();

BodyDef bodyDef=new BodyDef();
bodyDef.type= BodyDef.BodyType.KinematicBody;
bodyDef.position.set(0,0);
platform=world.createBody(bodyDef);

PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(3,1);

FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=.5f;
platform.createFixture(fixtureDef);
polygonShape.dispose();
platfrom.setLinearVelocity(1,0);
}

@Override
public void render() {

Gdx.gl.glClearColor(0,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

world.step(1/60f,6,2);
batch.setProjectionMatrix(cam.combined);
batch.begin();

world.getBodies(array);
for (Body body:array){
if(body.getUserData()!=null) {
Sprite sprite = (Sprite) body.getUserData();
sprite.setPosition(body.getPosition().x-sprite.getWidth()/2, body.getPosition().y-sprite.getHeight()/2);
sprite.setRotation(body.getAngle()*MathUtils.radDeg);
sprite.draw(batch);
}
}
batch.end();
debugRenderer.render(world,cam.combined);


Vector2 pos=platfrom.getTransform().getPosition();
if(pos.x>20-3) {
platfrom.setLinearVelocity(-1,0);
}
if(pos.x<3) {
platfrom.setLinearVelocity(1,0);
}

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void resize(int width, int height) {

extendViewport.update(width,height);
cam.position.x = w /2;
cam.position.y = h/2;
cam.update();
}

private void createPhysicsObject(float x,float y){

float sizeX=0.5f,sizeY=0.5f;
BodyDef bodyDef=new BodyDef();
bodyDef.position.set(x,y);
bodyDef.type= BodyDef.BodyType.DynamicBody;

Body body=world.createBody(bodyDef);

PolygonShape polygonShape=new PolygonShape();
polygonShape.setAsBox(sizeX,sizeY);
FixtureDef fixtureDef=new FixtureDef();
fixtureDef.shape=polygonShape;
fixtureDef.restitution=.2f;
fixtureDef.density=2;

body.createFixture(fixtureDef);
body.setFixedRotation(false);
polygonShape.dispose();

Sprite sprite=new Sprite(new Texture("badlogic.jpg"));
sprite.setSize(2*sizeX,2*sizeY);
sprite.setPosition(x-sprite.getWidth()/2,y-sprite.getHeight()/2);
sprite.setOrigin(sizeX,sizeY);

body.setUserData(sprite);
}

@Override
public void dispose() {
batch.dispose();
debugRenderer.dispose();
world.dispose();
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {

vector3.set(screenX,screenY,0);
Vector3 position=cam.unproject(vector3);
createPhysicsObject(vector3.x,vector3.y);

return false;
}
}

关于java - 如何创建 movingPlatform?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43710225/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com