AndEngine学习:CollisionDetectionExample(碰撞检测)
2011-10-30 19:48
423 查看
首先让我们看一下测试代码:
OK,让我们逐一解释。
这些应该没什么问题。
我们主要看
这个函数。
在这个函数里边我们定义了一个矩形和一条直线,并且让他们两个旋转
然后就是碰撞检测的部分了,当碰撞的时候,让矩形和直线都改变颜色。
OK,由于代码非常简单,也就不多废话了。
public class CollisionDetectionTestActivity extends BaseGameActivity { private static final int CAMERA_WIDTH = 480; private static final int CAMERA_HEIGHT = 320; private Camera mCamera; @Override public Engine onLoadEngine() { this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); return engine; } @Override public void onLoadResources() { } @Override public Scene onLoadScene() { this.mEngine.registerUpdateHandler(new FPSLogger()); final Scene scene = new Scene(); scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f)); int centerX = CAMERA_WIDTH / 2; int centerY = CAMERA_HEIGHT / 2; final LoopEntityModifier entityModifier = new LoopEntityModifier( new ParallelEntityModifier( new RotationModifier(12, 0, 360) )); final Rectangle rectangle = new Rectangle(centerX - 50, centerY - 16, 64, 64); rectangle.registerEntityModifier(entityModifier); final Line line = new Line(centerX, centerY, 32, 32); line.registerEntityModifier(entityModifier); scene.attachChild(rectangle); scene.attachChild(line); scene.registerUpdateHandler(new IUpdateHandler(){ @Override public void onUpdate(float pSecondsElapsed) { if(rectangle.collidesWith(line)) { rectangle.setColor(1, 0, 0); } else { rectangle.setColor(0, 1, 0); } if(line.collidesWith(rectangle)) { line.setColor(1, 0, 0); } else { line.setColor(0, 1, 0); } } @Override public void reset() { }}); return scene; } @Override public void onLoadComplete() { } }
OK,让我们逐一解释。
private static final int CAMERA_WIDTH = 480; private static final int CAMERA_HEIGHT = 320; private Camera mCamera; @Override public Engine onLoadEngine() { this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera)); return engine; } @Override public void onLoadResources() { } @Override public void onLoadComplete() { }
这些应该没什么问题。
我们主要看
public Scene onLoadScene();
这个函数。
在这个函数里边我们定义了一个矩形和一条直线,并且让他们两个旋转
final LoopEntityModifier entityModifier = new LoopEntityModifier( new ParallelEntityModifier( new RotationModifier(12, 0, 360) )); final Rectangle rectangle = new Rectangle(centerX - 50, centerY - 16, 64, 64); rectangle.registerEntityModifier(entityModifier); final Line line = new Line(centerX, centerY, 32, 32); line.registerEntityModifier(entityModifier); scene.attachChild(rectangle); scene.attachChild(line);
然后就是碰撞检测的部分了,当碰撞的时候,让矩形和直线都改变颜色。
scene.registerUpdateHandler(new IUpdateHandler(){ @Override public void onUpdate(float pSecondsElapsed) { if(rectangle.collidesWith(line)) { rectangle.setColor(1, 0, 0); } else { rectangle.setColor(0, 1, 0); } if(line.collidesWith(rectangle)) { line.setColor(1, 0, 0); } else { line.setColor(0, 1, 0); } }
OK,由于代码非常简单,也就不多废话了。
相关文章推荐
- AndEngine学习:PhysicsCollisionFiltering(有过滤器的碰撞检测)
- [AndEngine学习教程] 第9节 CollisionDetection 实体碰撞检测
- [AndEngine学习教程] 第9节 CollisionDetection 实体碰撞检测
- 基于层的碰撞检测 Layer-Based Collision Detection
- (转)[AndEngine学习教程] 第9节 CollisionDetection 实体碰撞检测
- 【学习笔记】AS3 碰撞检测之像素检测
- android游戏引擎andengine学习系列六:Physics物理碰撞效果理解
- Joint Deep Learning For Pedestrian Detection(论文笔记-深度学习:行人检测)
- AndEngine之DEMO学习(十二)EaseFunctionExample
- tensorflow学习(1)——训练自己的数据集并进行物体检测(object detection)
- 对抗学习用于目标检测--A-Fast-RCNN: Hard Positive Generation via Adversary for Object Detection
- Flash游戏学习笔记:矩形检测碰撞
- Rigidbody.detectCollisions 碰撞检测
- cocos2d-x 3.0游戏实例学习笔记 《跑酷》第七步--物理碰撞检测(2)--主角吃金币
- AndEngine之DEMO学习(十)PathModifierExample
- 碰撞测试 (Real Time Collision Detection Chapter 7)
- 【深度学习:目标检测】 Face Detection with the Faster R-CNN(数据集标注对比研究报告 )
- 学习 "使用cocos2d-x3.0和物理引擎实现碰撞检测" 中出现的一个问题
- 深度学习目标检测(object detection)系列(八)YOLO2
- Unity3D学习之(碰撞检测小项目)