• AndEngine学习:CollisionDetectionExample(碰撞检测)


    首先让我们看一下测试代码:

    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,由于代码非常简单,也就不多废话了。



  • 相关阅读:
    递归
    最简单的基于FFMPEG的音频编码器(PCM编码为AAC)
    最简单的基于FFMPEG的封装格式转换器(无编解码)
    最简单的基于FFMPEG的图像编码器(YUV编码为JPEG)
    视频主观质量评价工具:MSU Perceptual Video Quality tool
    ffmbc——为广播电视以及专业用途量身定制的FFmpeg
    方便使用FFMPEG的经验
    OpenCV提取显示一张图片(或者视频)的R,G,B颜色分量
    avcodec_decode_video2()解码视频后丢帧的问题解决
    HEVC,VP9,x264性能对比
  • 原文地址:https://www.cnblogs.com/xiaobo68688/p/2229460.html
Copyright © 2020-2023  润新知