AndEngine中手柄的使用——DigitalOnScreenControl && AnalogOnScreenControl

本文深入探讨游戏开发与AI音视频处理领域的关键技术和实践应用,包括游戏引擎、Unity、Cocos2d-X等,以及AR、语音识别、深度学习等AI音视频处理技术的实际操作。
部署运行你感兴趣的模型镜像


package org.andengine.examples;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.controller.MultiTouch;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.math.MathUtils;

import android.opengl.GLES20;
import android.widget.Toast;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 00:06:23 - 11.07.2010
 */
public class AnalogOnScreenControlsExample extends SimpleBaseGameActivity {
 // ===========================================================
 // Constants
 // ===========================================================

 private static final int CAMERA_WIDTH = 480;
 private static final int CAMERA_HEIGHT = 320;

 // ===========================================================
 // Fields
 // ===========================================================

 private Camera mCamera;

 private BitmapTextureAtlas mBitmapTextureAtlas;
 private ITextureRegion mFaceTextureRegion;

 private BitmapTextureAtlas mOnScreenControlTexture;
 private ITextureRegion mOnScreenControlBaseTextureRegion;
 private ITextureRegion mOnScreenControlKnobTextureRegion;

 private boolean mPlaceOnScreenControlsAtDifferentVerticalLocations = false;

 // ===========================================================
 // Constructors
 // ===========================================================

 // ===========================================================
 // Getter & Setter
 // ===========================================================

 // ===========================================================
 // Methods for/from SuperClass/Interfaces
 // ===========================================================

 @Override
 public EngineOptions onCreateEngineOptions() {
  this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

  final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
  engineOptions.getTouchOptions().setNeedsMultiTouch(true);
//设置多点触控,因为有两个手柄
  if(MultiTouch.isSupported(this)) {
   if(MultiTouch.isSupportedDistinct(this)) {
    Toast.makeText(this, "MultiTouch detected --> Both controls will work properly!", Toast.LENGTH_SHORT).show();
   } else {
    this.mPlaceOnScreenControlsAtDifferentVerticalLocations = true;
    Toast.makeText(this, "MultiTouch detected, but your device has problems distinguishing between fingers.\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
   }
  } else {
   Toast.makeText(this, "Sorry your device does NOT support MultiTouch!\n\n(Falling back to SingleTouch.)\n\nControls are placed at different vertical locations.", Toast.LENGTH_LONG).show();
  }

  return engineOptions;
 }

 @Override
 public void onCreateResources() {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

  this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
  this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
  this.mBitmapTextureAtlas.load();

  this.mOnScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
  this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
  this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
  this.mOnScreenControlTexture.load();
 }

 @Override
 public Scene onCreateScene() {
  this.mEngine.registerUpdateHandler(new FPSLogger());

  final Scene scene = new Scene();
  scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

  final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
  final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
  final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
  final PhysicsHandler physicsHandler = new PhysicsHandler(face);
  face.registerUpdateHandler(physicsHandler);

  scene.attachChild(face);

//这是第一个手柄,控制平移的

  /* Velocity control (left). */
  final float x1 = 0;
  final float y1 = CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight();
  final AnalogOnScreenControl velocityOnScreenControl = new AnalogOnScreenControl(x1, y1, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
   @Override
   public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
    physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
   }

   @Override
   public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
    /* Nothing. */
   }
  });
  
  velocityOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  velocityOnScreenControl.getControlBase().setAlpha(0.5f);

  scene.setChildScene(velocityOnScreenControl);

//这是第二个手柄,控制旋转的
  /* Rotation control (right). */
  final float y2 = (this.mPlaceOnScreenControlsAtDifferentVerticalLocations) ? 0 : y1;
  final float x2 = CAMERA_WIDTH - this.mOnScreenControlBaseTextureRegion.getWidth();
  final AnalogOnScreenControl rotationOnScreenControl = new AnalogOnScreenControl(x2, y2, this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IAnalogOnScreenControlListener() {
   @Override
   public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
    if(pValueX == x1 && pValueY == x1) {
     face.setRotation(x1);
    } else {
     face.setRotation(MathUtils.radToDeg((float)Math.atan2(pValueX, -pValueY)));
    }
   }

   @Override
   public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
    /* Nothing. */
   }
  });

//设置混合色的代码
  rotationOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  rotationOnScreenControl.getControlBase().setAlpha(0.5f);

  velocityOnScreenControl.setChildScene(rotationOnScreenControl);

  return scene;
 }

 // ===========================================================
 // Methods
 // ===========================================================

 // ===========================================================
 // Inner and Anonymous Classes
 // ===========================================================
}

在视野(scene)中有三个对象,分别为被控制的对象(face),左边的手柄(velocityOnScreenControl),右边的手柄,(rotationOnScreenControl),将这三个对象分别加入到视野中用到如下三句核心代码:scene.attachChild(face); scene.setChildScene(velocityOnScreenControl);

velocityOnScreenControl.setChildScene(rotationOnScreenControl);这时注意第三句用的是velocityOnScreenControl而并不是scene,原因是:如果用的是scene的话,就会覆盖前一个手柄。

以上用的是AnalogOnScreenControl(模拟手柄),当然了,我们也可以使用另一种”数字手柄“,即DigitalOnScreenControl,下面直接看代码啦:

package org.andengine.examples;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl.IOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.DigitalOnScreenControl;
import org.andengine.engine.handler.physics.PhysicsHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.opengl.GLES20;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 00:06:23 - 11.07.2010
 */
public class DigitalOnScreenControlExample extends SimpleBaseGameActivity {
 // ===========================================================
 // Constants
 // ===========================================================

 private static final int CAMERA_WIDTH = 480;
 private static final int CAMERA_HEIGHT = 320;
 private static final int DIALOG_ALLOWDIAGONAL_ID = 0;

 // ===========================================================
 // Fields
 // ===========================================================

 private Camera mCamera;

 private BitmapTextureAtlas mBitmapTextureAtlas;
 private ITextureRegion mFaceTextureRegion;

 private BitmapTextureAtlas mOnScreenControlTexture;
 private ITextureRegion mOnScreenControlBaseTextureRegion;
 private ITextureRegion mOnScreenControlKnobTextureRegion;

 private DigitalOnScreenControl mDigitalOnScreenControl;

 // ===========================================================
 // Constructors
 // ===========================================================

 // ===========================================================
 // Getter & Setter
 // ===========================================================

 // ===========================================================
 // Methods for/from SuperClass/Interfaces
 // ===========================================================

 @Override
 public EngineOptions onCreateEngineOptions() {
  this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

  return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
 }

 @Override
 public void onCreateResources() {
  BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

  this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
  this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);
  this.mBitmapTextureAtlas.load();

  this.mOnScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
  this.mOnScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
  this.mOnScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mOnScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
  this.mOnScreenControlTexture.load();
 }

 @Override
 public Scene onCreateScene() {
  this.mEngine.registerUpdateHandler(new FPSLogger());

  final Scene scene = new Scene();
  scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

  final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
  final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
  final Sprite face = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());
  final PhysicsHandler physicsHandler = new PhysicsHandler(face);
  face.registerUpdateHandler(physicsHandler);

  scene.attachChild(face);

  this.mDigitalOnScreenControl = new DigitalOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, this.getVertexBufferObjectManager(), new IOnScreenControlListener() {
   @Override
   public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
    physicsHandler.setVelocity(pValueX * 100, pValueY * 100);
   }
  });
  this.mDigitalOnScreenControl.getControlBase().setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
  this.mDigitalOnScreenControl.getControlBase().setAlpha(0.5f);
  this.mDigitalOnScreenControl.getControlBase().setScaleCenter(0, 128);
  this.mDigitalOnScreenControl.getControlBase().setScale(1.25f);
  this.mDigitalOnScreenControl.getControlKnob().setScale(1.25f);
  this.mDigitalOnScreenControl.refreshControlKnobPosition();

  scene.setChildScene(this.mDigitalOnScreenControl);

  return scene;
 }

 @Override
 public void onGameCreated() {
  this.showDialog(DIALOG_ALLOWDIAGONAL_ID);
 }

 @Override
 protected Dialog onCreateDialog(final int pID) {
  switch(pID) {
   case DIALOG_ALLOWDIAGONAL_ID:
    return new AlertDialog.Builder(this)
    .setTitle("Setup...")
    .setMessage("Do you wish to allow diagonal directions on the OnScreenControl?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(final DialogInterface pDialog, final int pWhich) {
      DigitalOnScreenControlExample.this.mDigitalOnScreenControl.setAllowDiagonal(true);
     }
    })
    .setNegativeButton("No", new DialogInterface.OnClickListener() {
     @Override
     public void onClick(final DialogInterface pDialog, final int pWhich) {
      DigitalOnScreenControlExample.this.mDigitalOnScreenControl.setAllowDiagonal(false);
     }
    })
    .create();
  }
  return super.onCreateDialog(pID);
 }

 // ===========================================================
 // Methods
 // ===========================================================

 // ===========================================================
 // Inner and Anonymous Classes
 // ===========================================================
}

 

该功能是使用了一个手柄来控制物体的移动。DigitalOnScreenControl ,AnalogOnScreenControl的功能类似。


DigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControlDigitalOnScreenControl

 

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

【EI复现】基于深度强化学习的微能源网能量管理与优化策略研究(Python代码实现)内容概要:本文围绕“基于深度强化学习的微能源网能量管理与优化策略”展开研究,重点利用深度Q网络(DQN)等深度强化学习算法对微能源网中的能量调度进行建模与优化,旨在应对可再生能源出力波动、负荷变化及运行成本等问题。文中结合Python代码实现,构建了包含光伏、储能、负荷等元素的微能源网模型,通过强化学习智能体动态决策能量分配策略,实现经济性、稳定性和能效的多重优化目标,并可能与其他优化算法进行对比分析以验证有效性。研究属于电力系统与人工智能交叉领域,具有较强的工程应用背景和学术参考价值。; 适合人群:具备一定Python编程基础和机器学习基础知识,从事电力系统、能源互联网、智能优化等相关方向的研究生、科研人员及工程技术人员。; 使用场景及目标:①学习如何将深度强化学习应用于微能源网的能量管理;②掌握DQN等算法在实际能源系统调度中的建模与实现方法;③为相关课题研究或项目开发提供代码参考和技术思路。; 阅读建议:建议读者结合提供的Python代码进行实践操作,理解环境建模、状态空间、动作空间及奖励函数的设计逻辑,同时可扩展学习其他强化学习算法在能源系统中的应用。
皮肤烧伤识别作为医学与智能技术交叉的前沿课题,近年来在深度学习方法推动下取得了显著进展。该技术体系借助卷积神经网络等先进模型,实现了对烧伤区域特征的高效提取与分类判别,为临床诊疗决策提供了重要参考依据。本研究项目系统整合了算法设计、数据处理及模型部署等关键环节,形成了一套完整的可操作性方案。 在技术实现层面,首先需要构建具有代表性的烧伤图像数据库,涵盖不同损伤程度及愈合阶段的临床样本。通过对原始图像进行标准化校正、对比度增强等预处理操作,有效提升后续特征学习的稳定性。网络架构设计需充分考虑皮肤病变的区域特性,通过多层卷积与池化操作的组合,逐步抽象出具有判别力的烧伤特征表示。 模型优化过程中采用自适应学习率调整策略,结合交叉熵损失函数与梯度下降算法,确保参数收敛的稳定性。为防止过拟合现象,引入数据扩增技术与正则化约束,增强模型的泛化能力。性能验证阶段采用精确率、召回率等多维度指标,在独立测试集上全面评估模型对不同烧伤类型的识别效能。 经过充分验证的识别系统可集成至医疗诊断平台,通过规范化接口实现与现有医疗设备的无缝对接。实际部署前需进行多中心临床验证,确保系统在不同操作环境下的稳定表现。该技术方案的实施将显著缩短烧伤评估时间,为临床医师提供客观量化的辅助诊断依据,进而优化治疗方案制定流程。 本项目的突出特点在于将理论研究与工程实践有机结合,既包含前沿的深度学习算法探索,又提供了完整的产业化实施路径。通过模块化的设计思路,使得医疗专业人员能够快速掌握核心技术方法,推动智能诊断技术在烧伤外科领域的实际应用。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
【提高晶格缩减(LR)辅助预编码中VP的性能】向量扰动(VP)预编码在下行链路中多用户通信系统中的应用(Matlab代码实现)内容概要:本文主要介绍向量扰动(VP)预编码在下行链路多用户通信系统中的应用,并重点研究如何通过晶格缩减(LR)辅助预编码技术提升VP的性能。文中提供了基于Matlab的代码实现,展示了VP预编码的核心算法流程及其在多用户MIMO系统中的仿真应用。通过对传统VP预编码引入晶格缩减技术,有效降低了计算复杂度并提升了系统性能,尤其是在误码率和吞吐量方面的表现。此外,文档还列举了大量通信、信号处理、优化算法等相关领域的Matlab仿真资源,突出了该研究在现代无线通信系统设计中的实际价值和技术延展性。; 适合人群:具备通信工程、电子信息、信号与系统等相关专业背景的研究生、科研人员及从事无线通信系统仿真的工程师;熟悉Matlab编程并希望深入了解预编码技术原理与实现的技术人员。; 使用场景及目标:①用于多用户MIMO下行链路系统中预编码算法的研究与性能对比;②为VP预编码与晶格缩减技术的结合提供可运行的Matlab实现方案;③支持学术研究、课程设计或科研项目中的算法验证与仿真分析。; 阅读建议:建议读者结合Matlab代码逐段理解VP预编码与LR辅助技术的实现细节,重点关注信道矩阵处理、格点搜索优化及扰动向量生成等关键步骤。同时可参考文档中列出的其他通信与优化案例,拓展对相关技术体系的整体认知。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值