本文主要是介绍#Andengine##Box2D#-----前轮驱动赛车,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一. 实现功能:
应用Andengine引擎+Box2D物理引擎,实现“前轮驱动”赛车.
二. 相关代码:
1. 在游戏场景中创建物理世界:
FixedStepPhysicsWorld mPhysicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0f, 0f), false, 8, 3); //创建物理世界,将重力设置为0
registerUpdateHandler(mPhysicsWorld); //在当前场景中注册该物理世界
2. 创建汽车主体部分:
public static final FixtureDef CAR_DEF = PhysicsFactory.createFixtureDef(1f, 1f, 1f, false);Sprite CarShape=new Sprite(); //创建汽车主体精灵
GameScene.attach(CarShape); //将精灵附加到场景中Body carBody = PhysicsFactory.createBoxBody(Registry.sGameScene.mPhysicsWorld,CarShape,BodyDef.BodyType.DynamicBody,Registry.sGameScene.CAR_DEF); //根据汽车主体精灵创建Body
carBody.setLinearDamping(1); //设置线性方向上的阻尼
carBody.setAngularDamping(1); //设置转动阻尼
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(CarShape, carBody, true, true)); //将Sprite和Body进行连接
3. 创建四个轮胎:
public static final FixtureDef WHEEL_DEF = PhysicsFactory.createFixtureDef(1f, 1f, 1f, false);//右前轮(Sprite+Body):
rightTopWheel=new Sprite(CarShape.getX()+CarShape.getWidth()*0.5f,CarShape.getY()+18,ResourceManager.getInstance().WheelRegion,ResourceManager.getInstance().vbom);
Registry.sGameScene.attachChild(rightTopWheel); rightTopBody = PhysicsFactory.createBoxBody(Registry.sGameScene.mPhysicsWorld,rightTopWheel,BodyDef.BodyType.DynamicBody, Registry.sGameScene.WHEEL_DEF);
Registry.sGameScene.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rightTopWheel, rightTopBody, true, true));//左前轮(Sprite+Body):
leftTopWheel=new Sprite(CarShape.getX()-CarShape.getWidth()*0.5f,CarShape.getY()+18,ResourceManager.getInstance().WheelRegion,ResourceManager.getInstance().vbom);
Registry.sGameScene.attachChild(leftTopWheel);leftTopBody = PhysicsFactory.createBoxBody(Registry.sGameScene.mPhysicsWorld,leftTopWheel,BodyDef.BodyType.DynamicBody, Registry.sGameScene.WHEEL_DEF);Registry.sGameScene.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leftTopWheel, leftTopBody, true, true));//右后轮(Sprite+Body):
rightDownWheel=new Sprite(CarShape.getX()+CarShape.getWidth()*0.5f,CarShape.getY()-18,ResourceManager.getInstance().WheelRegion,ResourceManager.getInstance().vbom);
Registry.sGameScene.attachChild(rightDownWheel);rightDownBody = PhysicsFactory.createBoxBody(Registry.sGameScene.mPhysicsWorld,rightDownWheel,BodyDef.BodyType.DynamicBody, Registry.sGameScene.WHEEL_DEF);
Registry.sGameScene.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(rightDownWheel, rightDownBody, true, true));//左后轮(Sprite+Body)
leftDownWheel=new Sprite(CarShape.getX()-CarShape.getWidth()*0.5f,CarShape.getY()-18,ResourceManager.getInstance().WheelRegion,ResourceManager.getInstance().vbom);
Registry.sGameScene.attachChild(leftDownWheel);leftDownBody = PhysicsFactory.createBoxBody(Registry.sGameScene.mPhysicsWorld,leftDownWheel,BodyDef.BodyType.DynamicBody, Registry.sGameScene.WHEEL_DEF);
Registry.sGameScene.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leftDownWheel, leftDownBody, true, true));
4.将轮胎与汽车主体进行关联:
(1). RevoluteJoint (旋转关节) ----前轮
因为前轮负责方向的引导(即转向,方向盘控制前轮),所以需要使用旋转关节
private RevoluteJoint mJointRightTop;
private RevoluteJointDef rtjd; rtjd.initialize(carBody, rightTopBody, rightTopBody.getWorldCenter()); //将车体Body与右前轮Body进行连接,连接中心设为车胎中心
rtjd.enableMotor=true; //旋转关节开启扭矩
rtjd.maxMotorTorque=100; //最大扭矩设置为100
mJointRightTop= (RevoluteJoint)Registry.sGameScene.mPhysicsWorld.createJoint(rtjd); //在物理世界中注册该关节
(2). PrismaticJoint(平行关节)----后轮
后轮只需要跟随车辆主体进行运动
PrismaticJointDef jd=new PrismaticJointDef();
Joint mJointRightDown=null;jd.initialize(carBody, rightDownBody, rightDownBody.getWorldCenter(),new Vector2(1,0)); //将右后轮Body与车主体Body进行平行关节连接
jd.enableLimit=true; //设置为跟随车动
jd.lowerTranslation=0;
jd.upperTranslation=0;
mJointRightDown=Registry.sGameScene.mPhysicsWorld.createJoint(jd); //注册关节
5. 场景Update循环中控制车辆移动与转向:
*Box2D知识点: Body.getTransform() ---->获得Body的位置及速度,通过位置和速度能得到向量。也可以通过vals来简便获得
Body X轴的方向向量:
Vector2 d=new Vector2(Body.getTransform().vals[2],Body.getTransform().vals[3]);
Body Y轴的方向向量:
Vector2 d=new Vector2(Body.getTransform().vals[4],Body.getTransform().vals[5]);
public static double MAX_STEER_ANGLE = Math.PI/3; //最大扭矩
public static float STEER_SPEED = 1.5f; //转向速度
public static float HORSEPOWERS = 20f; //马力public float engineSpeed;
public double steeringAngle;场景循环:(Update)
//前进
sideXR = (float)myCar.getRightTopBody().getTransform().vals[4];
sideYR = (float)myCar.getRightTopBody().getTransform().vals[5];
Vector2 rdirection=new Vector2(sideXR,sideYR); //获得右前胎Y轴的方向向量
rdirection.mul(engineSpeed); //方向向量*动力=作用力sideXL = (float)myCar.getLeftTopBody().getTransform().vals[4];
sideYL = (float)myCar.getLeftTopBody().getTransform().vals[5];
Vector2 ldirection=new Vector2(sideXL,sideYL); //获得左前胎Y轴的方向向量
ldirection.mul(engineSpeed); //方向向量*动力=作用力myCar.getRightTopBody().applyForce(ldirection, myCar.getRightTopBody().getPosition()); //施加右前胎作用力
myCar.getLeftTopBody().applyForce(rdirection, myCar.getLeftTopBody().getPosition()); //施加左前胎作用力//转向
float mspeed;
mspeed=(float)(steeringAngle-myCar.getmJointRightTop().getJointAngle()); //获得转向扭矩力
myCar.getmJointRightTop().setMotorSpeed(mspeed * STEER_SPEED); //施加转向扭矩力mspeed=(float)(steeringAngle-myCar.getmJointLeftTop().getJointAngle());
myCar.getmJointLeftTop().setMotorSpeed(mspeed * STEER_SPEED);
6. 创建前进与转向按钮:
前进:
engineSpeed=-CarManager.getInstance().HORSEPOWERS;
转向:(右转)
steeringAngle=-CarManager.getInstance().MAX_STEER_ANGLE;
释放以后:
engineSpeed=0;
steeringAngle=0;
最后附上项目的demo供参考:
下载地址:http://pan.baidu.com/s/1jGkJbdc
这篇关于#Andengine##Box2D#-----前轮驱动赛车的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!