Box2D例子——Demo3自行车

package com.cvte.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
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.EdgeShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.DistanceJoint;
import com.badlogic.gdx.physics.box2d.joints.DistanceJointDef;

public class Demo3 extends ApplicationAdapter implements InputProcessor {

    private static final float PXTM = 80;

	protected OrthographicCamera mCamera;
	private Box2DDebugRenderer mRenderer;

	private World mWorld;

	private Body mBodyA;
	private Body mBodyB;
	private DistanceJoint mDistanceJoint;

	@Override
	public void create() {
        float cameraWidth = Gdx.graphics.getWidth() / PXTM;
        float cameraHeight = Gdx.graphics.getHeight() / PXTM;
		mCamera = new OrthographicCamera(cameraWidth, cameraHeight);
		mCamera.position.set(cameraWidth / 2, cameraHeight / 2, 0);
		mCamera.update();

		mRenderer = new Box2DDebugRenderer();

		mWorld = new World(new Vector2(0, -10), true);

//		//ground
//		{
//			BodyDef bd = new BodyDef();
//			bd.type = BodyType.StaticBody;
//			bd.position.set(toWorldSize(0), toWorldSize(20));
//
//			EdgeShape edgeShape = new EdgeShape();
//			edgeShape.set(new Vector2(toWorldSize(0), toWorldSize(0)), new Vector2(toWorldSize(Gdx.graphics.getWidth()), toWorldSize(0)));
//
//			FixtureDef fd = new FixtureDef();
//			fd.shape = edgeShape;
//
//			Body ground = mWorld.createBody(bd);
//			ground.createFixture(fd);
//		}

		// wall
		{
			BodyDef bd = new BodyDef();
			bd.type = BodyType.StaticBody;

			EdgeShape edgeShape = new EdgeShape();

			FixtureDef fd = new FixtureDef();
			fd.shape = edgeShape;

			Body box = mWorld.createBody(bd);
			edgeShape.set(new Vector2(toWorldSize(0), toWorldSize(0)), new Vector2(toWorldSize(Gdx.graphics.getWidth()), toWorldSize(0)));
			box.createFixture(fd);
			edgeShape.set(new Vector2(toWorldSize(Gdx.graphics.getWidth()), toWorldSize(0)), new Vector2(toWorldSize(Gdx.graphics.getWidth()),
					toWorldSize(Gdx.graphics.getHeight())));
			box.createFixture(fd);
			edgeShape.set(new Vector2(toWorldSize(Gdx.graphics.getWidth()), toWorldSize(Gdx.graphics.getHeight())), new Vector2(0, toWorldSize(Gdx.graphics.getHeight())));
			box.createFixture(fd);
			edgeShape.set(new Vector2(toWorldSize(0), toWorldSize(Gdx.graphics.getHeight())), new Vector2(0, toWorldSize(0)));
			box.createFixture(fd);
		}

		//ball
		{
			BodyDef bd = new BodyDef();
			bd.type = BodyType.DynamicBody;
			bd.position.set(toWorldSize(400), toWorldSize(800));

			CircleShape circleShape = new CircleShape();
			circleShape.setRadius(toWorldSize(20));

			FixtureDef fd = new FixtureDef();
			fd.shape = circleShape;
			fd.density = 1;
			fd.restitution = 1;

			Body ball = mWorld.createBody(bd);
			ball.createFixture(fd);
		}

		createBodies();
		createDistanceJoint();

		Gdx.input.setInputProcessor(this);
	}

	@Override
	public void dispose() {
		mRenderer.dispose();

		mWorld.dispose();

		mCamera = null;
	}

	@Override
	public void render() {
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		mWorld.step(Gdx.app.getGraphics().getDeltaTime(), 6, 2);

		mRenderer.render(mWorld, mCamera.combined);
	}

	private float toWorldSize(float pos) {
		return (pos / PXTM);
	}

	/**
	 * 创建关节连接的两个刚体
	 */
	private void createBodies() {
		Vector2 posA = new Vector2(toWorldSize(100), toWorldSize(100));
		Vector2 posB = new Vector2(toWorldSize(200), toWorldSize(100));

		{
			BodyDef bd = new BodyDef();
			bd.type = BodyType.DynamicBody;
			bd.position.set(posA.x, posA.y);

			CircleShape circleShape = new CircleShape();
			circleShape.setRadius(toWorldSize(30));

			FixtureDef fd = new FixtureDef();
			fd.shape = circleShape;
			fd.density = 1;
			fd.friction = 0.5f;
			fd.restitution = 0;

			mBodyA = mWorld.createBody(bd);
			mBodyA.createFixture(fd);
		}

		{
			BodyDef bd = new BodyDef();
			bd.type = BodyType.DynamicBody;
			bd.position.set(posB.x, posB.y);

			CircleShape circleShape = new CircleShape();
			circleShape.setRadius(toWorldSize(15));

			FixtureDef fd = new FixtureDef();
			fd.shape = circleShape;
			fd.density = 1;
			fd.friction = 0.5f;
			fd.restitution = 0;

			mBodyB = mWorld.createBody(bd);
			mBodyB.createFixture(fd);
		}
	}

	/**
	 * 创建距离关节
	 */
	private void createDistanceJoint() {
		DistanceJointDef djd = new DistanceJointDef();
		djd.initialize(mBodyA, mBodyB, mBodyA.getPosition(), mBodyB.getPosition());
		djd.collideConnected = true;

		mDistanceJoint = (DistanceJoint)mWorld.createJoint(djd);
	}

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.LEFT) {
			mBodyA.setAngularVelocity(2f);
		}
		else if (keycode == Keys.RIGHT) {
			mBodyA.setAngularVelocity(-2f);
		}

		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		return false;
	}

}

时间: 2024-10-12 09:38:58

Box2D例子——Demo3自行车的相关文章

Box2D例子——Demo0掉落的小球

package com.cvte.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.

Box2D例子——Demo1掉落的小球

<pre name="code" class="java">package com.cvte.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.

Box2D例子——Demo2停不下来的球球

package com.cvte.game; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputProcessor; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gd

iOS学习路之Objective-C(一)

1:类和对象 1:oc简介 1:oc是完全兼容c的 2:oc在c的基础上增加一层最小的面向对象的语法 3:写oc工程的时候可以混入c的代码,甚至是c++的代码 4:通过oc语言可以开发ios或os x平台下应用程序 2:编程思想 1:面向过程 2:面向对象 3:面向过程编程 面向过程编程代表:c 过程:实现某个应用程序所经历的步骤,每一个步骤对应一个功能,该功能可以通过一个自定义的函数来实现 面向过程编程:首先分析实现该应用程序功能需要经历几个步骤,每个步骤通过一个自定义的函数来实现,最后把所有

饥荒MOD lua编程0基础入门

前言 原贴写于饥荒游戏贴吧,为了使文章针对性更强,将原文切割并精简.此贴主要为编程0基础的modder讲解一些编程的基础知识.至于说有关饥荒框架的介绍,则会放在另一篇文章里讲解. 编程0基础的人,要想学习制作MOD,难度是比较大的,因为缺乏一些基本的编程概念,只懂得复制别人的代码或者在它们的基础上稍加改变,遇到稍微复杂一点的代码,就束手无策了.对于MOD崩溃或错误,也几乎没办法自行处理.但我也不推荐先去学一门编程语言之后再来学习MOD代码,这是没有必要的.事实上饥荒MOD里用到的基本编程知识都比

JavaSE学习(二):进制转换-数据类型转换-Java运算符

一.进制转换 1.1 其他进制转十进制(以十六进制为例): 十六进制范围:0-9, A-F对应数字10-15 2A7E(16)  =  14*16(0) +7*16(1) + 10*16(2)  + 2*16(3) =  10878(10) 1.2 十进制转二进制:将十进制数除以2,取余数,将最后一位余数排在第一位,倒数第二位的数排在第二位,依次排列,构成的01数字串即为表示该十进制数的二进制数. 总结:十进制%二进制 余数 0-1(Java中符号 "%"表示取余),逆序排列. (图片

Java 后台验证的工具类

Java 后台验证的工具类 public class ValidationUtil {         //手机号     public static String mobile = "^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";       //不允许为空     public static String blank = ".*[^ ].*";  

《割绳子》《蜡笔物理学》《Contre Jour》《顽皮鳄鱼爱洗澡》等游戏用Box2D引擎实现物理部分的方法(转)

从最热门游戏排行榜和flash游戏网站上,你能看到什么?许多2D游戏都有非常出色的物理学和美术设计.现在我们要学习那些游戏使用了什么物理学以及如何用Box2D制作它们. 除了知道是“什么”,更重要的是知道“如何做”,首先,我想问读者一个问题:如果你想复制物理游戏的机制或行为,你需要什么技术和方法? 一年以前,我问了自己同样的问题,<6 Dimensions>就是问题的答案.这款游戏是一个创意的盒子,每一面都包含一组使用Box2D物理学再加上视觉美学技术制作的不同的游戏机制.在此,我将与大家分享

Libgdx Box2D实战---放开那小球(四:不规则body创建--physics-body-editor的使用)

继续我们的问题,如果遇到不规则的图形怎么办?难不成要一个个组装吗? 这里就要提一个叫做"关节"的名词了,它可以将一个个部分进行组装成自己的形状,特别是各部件独自运转时,比如一辆自行车,它的轮子和车身都是各自运转的. 但是网上相关的资料真是太少了!一大堆复制粘贴的,全是废话,不知道大家是怎么感觉的?但是我的这个游戏没用到,所以没有深究,有兴趣的可以去谷歌一下. 下面我介绍的是一个非常非常方便的工具,它的名字叫做physics-body-editor,那么它是干什么的呢?恩,它可以根据你的