package com.mygdx.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.WorldManifold;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.TimeUtils;
import com.mygdx.game.utils.GdxTest;
public class Box2DCharacterControllerTest extends GdxTest implements ApplicationListener {
final static float MAX_VELOCITY = 14f;
boolean jump = false;
World world;
Body player;
Fixture playerPhysicsFixture;// 夹具,将形状附加到物体上?
Fixture playerSensorFixture;
OrthographicCamera cam;
Box2DDebugRenderer renderer;// 把Box2D的边缘线显示出来?
Array<Platform> platforms = new Array<Platform>();
Platform groundedPlatform = null;
float stillTime = 0;
long lastGroundTime = 0;
SpriteBatch batch;
BitmapFont font;
float accum = 0;
float TICK = 1 / 60f;//每秒60帧
@Override
public void create () {
//world = new World(new Vector2(0, -40), true);
world = new World(new Vector2(0, -10), true);
renderer = new Box2DDebugRenderer();
cam = new OrthographicCamera(28, 20);// 28米宽20米高
createWorld();
Gdx.input.setInputProcessor(this);
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false);
}
@Override
public void dispose () {
world.dispose();
renderer.dispose();
batch.dispose();
font.dispose();
}
private void createWorld () {
//float y1 = 1;// 创建平直路面
float y1 = (float)Math.random() * 0.1f + 1;// 创建不规则路面
float y2 = y1;
for (int i = 0; i < 50; i++) {// 创建50单位长度的地面
// 起点:-50米处,终点 -50+49*2+2 就是50米处 y1 y2 1米高的地方
Body ground = createEdge(BodyType.StaticBody, (-50 + i * 2), (y1), (-50 + i * 2 + 2), (y2), 0);// createEdge 就是创建一条线?
y1 = y2;
//y2 = 1;
y2=(float)Math.random() + 1;// 要么高要么低,在1米高处差上下最多1米
}
Body box = createBox(BodyType.StaticBody, 1, 1, 0);// 显示为绿色
//Body box = createBox(BodyType.StaticBody, 5, 5, 0);
box.setTransform(30, 3, 0);
box = createBox(BodyType.StaticBody, 1.2f, 1.2f, 0);
//box = createBox(BodyType.StaticBody, 5f, 5f, 0);
box.setTransform(5, 2.4f, 0);
player = createPlayer();
player.setTransform(-40.0f, 4.0f, 0);
player.setFixedRotation(true);// 这句斜杠掉player各种打转,然后不会跳了
for (int i = 0; i < 20; i++) {// DynamicBody都是红色
box = createBox(BodyType.DynamicBody, (float)Math.random(), (float)Math.random(), 3);
box.setTransform((float)Ma
[LIBGDX学习]LibGDX代码详解(三)Box2D character control
最新推荐文章于 2021-05-30 12:39:09 发布
本文介绍了使用LibGDX库进行Box2D游戏开发时,如何实现角色控制器的详细步骤。通过创建静态和动态物体、设置碰撞检测、处理玩家输入来控制角色移动和跳跃。同时,文章涵盖了世界、相机、形状和夹具的创建,以及摩擦力和速度限制的调整。
摘要由CSDN通过智能技术生成