场景精灵间的坐标转换

1.要点分析
1.在制作动画精灵的时候,为了方便计算,常常需要把场景中的坐标转换为精灵的内部坐标或者需要把精灵的内部坐标转换为场景坐标.如果精灵没有进行过旋转操作,他们之间只一个offset而已
2.本节需要实现的是在一个人脸精灵内,找到他的眼睛位置,然后随意变换精灵(大小比例,旋转,移动等)然后依旧定位出精灵的眼睛.

2.新内容
1.要在Activity上标注精灵的眼睛,需要使用到箭头,在简略的情况下,可以用三根直线实现,好在AndEngine已经为我们提供一Line类:
public class Line extends Shape

  1. <font color="#000000">/**
  2. * Uses a default {@link HighPerformanceLineVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Line#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  3. */
  4. public Line(final float pX1, final float pY1, final float pX2, final float pY2, final VertexBufferObjectManager pVertexBufferObjectManager) {
  5. this(pX1, pY1, pX2, pY2, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager, DrawType.STATIC);
  6. }
  7. /**
  8. * Uses a default {@link HighPerformanceLineVertexBufferObject} with the {@link VertexBufferObjectAttribute}s: {@link Line#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  9. */
  10. public Line(final float pX1, final float pY1, final float pX2, final float pY2, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) {
  11. this(pX1, pY1, pX2, pY2, Line.LINE_WIDTH_DEFAULT, pVertexBufferObjectManager, pDrawType);
  12. }
  13. /**
  14. * Uses a default {@link HighPerformanceLineVertexBufferObject} in {@link DrawType#STATIC} with the {@link VertexBufferObjectAttribute}s: {@link Line#VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT}.
  15. */
  16. public Line(final float pX1, final float pY1, final float pX2, final float pY2, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager) {
  17. this(pX1, pY1, pX2, pY2, pLineWidth, pVertexBufferObjectManager, DrawType.STATIC);
  18. }
  19. public Line(final float pX1, final float pY1, final float pX2, final float pY2, final float pLineWidth, final VertexBufferObjectManager pVertexBufferObjectManager, final DrawType pDrawType) {
  20. this(pX1, pY1, pX2, pY2, pLineWidth, new HighPerformanceLineVertexBufferObject(pVertexBufferObjectManager, Line.LINE_SIZE, pDrawType, true, Line.VERTEXBUFFEROBJECTATTRIBUTES_DEFAULT));
  21. }
  22. public Line(final float pX1, final float pY1, final float pX2, final float pY2, final float pLineWidth, final ILineVertexBufferObject pLineVertexBufferObject) {
  23. super(pX1, pY1, PositionColorShaderProgram.getInstance());</font>

复制代码

2.AnalogOnScreenControl类,这个我们在上一节的内容中已经详细介绍了,使用这个类只是方便我们观看坐标切换后的效果3.源代码分析        1.内部成员变量定义:背景+控制器+精灵脸

  1. private static final float CAMERA_WIDTH = 800;
  2. private static final float CAMERA_HEIGHT = 480;
  3. private Camera mCamera;
  4. private RepeatingSpriteBackground mBackground;
  5. private TiledTextureRegion mBaseRegion;
  6. private TiledTextureRegion mKnobRegion;
  7. private TiledTextureRegion mFaceRegion;
  8. @Override
  9. public EngineOptions onCreateEngineOptions() {
  10. // TODO Auto-generated method stub
  11. mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
  12. EngineOptions mEngineOptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH,CAMERA_HEIGHT),mCamera);
  13. return mEngineOptions;
  14. }
  15. @Override
  16. public void onCreateResources(
  17. OnCreateResourcesCallback pOnCreateResourcesCallback)
  18. throws Exception {
  19. // TODO Auto-generated method stub
  20. mBackground = new RepeatingSpriteBackground(CAMERA_WIDTH, CAMERA_HEIGHT, getTextureManager(), AssetBitmapTextureAtlasSource.create(getAssets(), "background_grass.png"), getVertexBufferObjectManager());
  21. BitmapTextureAtlas mTexture = new BitmapTextureAtlas(getTextureManager(), 256, 128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
  22. mBaseRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "onscreen_control_base.png", 0, 0, 1, 1);
  23. mKnobRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "onscreen_control_knob.png",128,0,1, 1);
  24. mFaceRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mTexture, this, "face_box.png", 192, 0, 1, 1);
  25. mTexture.load();
  26. pOnCreateResourcesCallback.onCreateResourcesFinished();
  27. }

复制代码

在这里,控制器的背景,控制器指针和精灵脸蛋共用一个Texture.              
2.在onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)函数中,定义了3条直线,一个人脸精灵和一个控制器

  1. Scene mScene = new Scene();
  2. mScene.setBackground(mBackground);
  3. final Line mDownLine = new Line(0,0,0,0,3,getVertexBufferObjectManager());
  4. mDownLine.setColor(1, 0, 0);
  5. final Line mLeftLine = new Line(0,0,0,0,3,getVertexBufferObjectManager());
  6. mLeftLine.setColor(1, 0, 0);
  7. final Line mRightLine = new Line(0,0,0,0,3,getVertexBufferObjectManager());
  8. mRightLine.setColor(1, 0, 0);
  9. final MySprite mFace = new MySprite(100, 100,mFaceRegion, getVertexBufferObjectManager()){
  10. @Override
  11. protected void onManagedUpdate(float pSecondsElapsed) {
  12. // TODO Auto-generated method stub
  13. float mCoordinates[] = this.convertLocalToSceneCoordinates(11, 13);//坐标转换
  14. float x = mCoordinates[0];
  15. float y = mCoordinates[1];
  16. mDownLine.setPosition(x, y+50, x, y);
  17. mLeftLine.setPosition(x-10, y+10, x, y);
  18. mRightLine.setPosition(x+10, y+10, x, y);
  19. super.onManagedUpdate(pSecondsElapsed);
  20. }
  21. };

复制代码

在这里比较重要的是convertLocalToSceneCoordinates函数,先看看它是如何实现的,这个方法是在Entity类中实现的:

  1. /* (non-Javadoc)
  2. * @see org.andengine.entity.IEntity#convertLocalToSceneCoordinates(float[])
  3. */
  4. @Override
  5. public float[] convertLocalToSceneCoordinates(final float[] pCoordinates) {
  6. return this.convertLocalToSceneCoordinates(pCoordinates, Entity.VERTICES_LOCAL_TO_SCENE_TMP);
  7. }
  8. /* (non-Javadoc)
  9. * @see org.andengine.entity.IEntity#convertLocalToSceneCoordinates(float[], float[])
  10. */
  11. @Override
  12. public float[] convertLocalToSceneCoordinates(final float[] pCoordinates, final float[] pReuse) {
  13. final Transformation localToSceneTransformation = this.getLocalToSceneTransformation();
  14. pReuse[Constants.VERTEX_INDEX_X] = pCoordinates[Constants.VERTEX_INDEX_X];
  15. pReuse[Constants.VERTEX_INDEX_Y] = pCoordinates[Constants.VERTEX_INDEX_Y];
  16. localToSceneTransformation.transform(pReuse);
  17. return pReuse;
  18. }

复制代码

再跟踪getLocalToSceneTransformation() :

  1. @Override
  2. public Transformation getLocalToSceneTransformation() {
  3. if(this.mLocalToSceneTransformation == null) {
  4. this.mLocalToSceneTransformation = new Transformation();
  5. }
  6. // TODO Cache if parent(recursive) not dirty.
  7. final Transformation localToSceneTransformation = this.mLocalToSceneTransformation;
  8. localToSceneTransformation.setTo(this.getLocalToParentTransformation());
  9. final IEntity parent = this.mParent;
  10. if(parent != null) {
  11. localToSceneTransformation.postConcat(parent.getLocalToSceneTransformation());
  12. }
  13. return localToSceneTransformation;
  14. }

复制代码

这就是经过一系列的转换得到的
3.布置AnalogOnScreenControl:

  1. AnalogOnScreenControl mController = new AnalogOnScreenControl(30, CAMERA_HEIGHT - mBaseRegion.getHeight() - 30, mCamera, mBaseRegion, mKnobRegion, 0.1f, 100, getVertexBufferObjectManager(),
  2. new IAnalogOnScreenControlListener(){
  3. @Override
  4. public void onControlChange(
  5. BaseOnScreenControl pBaseOnScreenControl,
  6. float pValueX, float pValueY) {
  7. // TODO Auto-generated method stub
  8. mFace.setVelocityXY(pValueX*100,pValueY*100);
  9. }
  10. @Override
  11. public void onControlClick(
  12. AnalogOnScreenControl pAnalogOnScreenControl) {
  13. // TODO Auto-generated method stub
  14. }
  15. });

复制代码

4.将各种角色添加到场景中

  1. mFace.registerEntityModifier(new LoopEntityModifier(new SequenceEntityModifier(new ScaleModifier(3, 1.0f, 4.5f),
  2. new RotationModifier(5, 0, 360),new ScaleModifier(3, 4.5f, 1.0f),new RotationModifier(2, 360, 0))));
  3. mScene.attachChild(mFace);
  4. mScene.attachChild(mDownLine);
  5. mScene.attachChild(mLeftLine);
  6. mScene.attachChild(mRightLine);
  7. mScene.setChildScene(mController);
  8. pOnCreateSceneCallback.onCreateSceneFinished(mScene);

复制代码

在这里,精灵脸蛋通过修改器来实现缩放和旋转,移动是用过控制器来实现的
5.最后是自定义的精灵类,主要是一点点的修改而已

  1. public class  MySprite extends TiledSprite{
  2. private float mVelocityX = 0;
  3. private float mVelocityY = 0;
  4. public MySprite(float pX, float pY,
  5. ITiledTextureRegion pTiledTextureRegion,
  6. VertexBufferObjectManager pVertexBufferObjectManager) {
  7. super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
  8. // TODO Auto-generated constructor stub
  9. }
  10. @Override
  11. protected void onManagedUpdate(float pSecondsElapsed) {
  12. // TODO Auto-generated method stub
  13. this.mX += mVelocityX * pSecondsElapsed;
  14. this.mY += mVelocityY * pSecondsElapsed;
  15. this.setPosition(mX, mY);
  16. super.onManagedUpdate(pSecondsElapsed);
  17. }
  18. void setVelocityXY(float vX, float vY){
  19. mVelocityX = vX;
  20. mVelocityY = vY;
  21. }
  22. }

复制代码

3.测试经过层层写代码,是时候看看效果啦,每次到这里都是最开心的时刻哦


转载地址:http://blog.csdn.net/cen616899547/article/details/8140491

http://www.eoeandroid.com/forum-863-1.html

www.ogengine.com

场景精灵间的坐标转换

时间: 2024-08-26 21:34:27

场景精灵间的坐标转换的相关文章

架构设计:系统间通信(45)——阶段性问题记录

到此为止 <架构设计:系统间通信>专题就暂时告一段落了.这边文章笔者用于暂时记录这个专题中还需要补充的内容,并在后续的整理中足一补上: 退避算法和退避规则,以及其应用场景 系统间通信的性能指标.还需要进行说明. 典型的RPC框架,还需要增加Apache Avro的介绍 关于Netty的过滤器和执行器的执行顺序问题 RESET知识点的讲解和区别对比 对apache thrift IDL的描述好像有点问题,特别是在默认值上面. 关于RPC接口泛化的问题,还要结合dubbo的设计进行一下说明 补重要

(转)ARCGIS中坐标转换及地理坐标、投影坐标的定义

原文地址:http://blog.sina.com.cn/s/blog_663d9a1f01017cyz.html 1.动态投影(ArcMap) 所谓动态投影指,ArcMap中的Data 的空间参考或是说坐标系统是默认为第一加载到当前工作区的那个文件的坐标系统,后加入的数据,如果和当前工作区坐标系统不相同,则ArcMap会自动做投影变换,把后加入的数据投影变换到当前坐标系统下显示!但此时数据文件所存储的数据并没有改变,只是显示形态上的变化!因此叫动态投影!表现这一点最明显的例子就是,在Expor

大地测量控制点坐标转换技术规范

1. 范围 本标准规定了大地测量控制点坐标转换到2000国家大地坐标系的技术要求,包括重合点选取.标转换模型.转换方法.精度评价等. 本标准适用于地方独立坐标系.1954北京坐标系.1980西安坐标系.WGS-84坐标系,以及ITRF框架下的大地测量控制点向2000国家大地坐标系的坐标转换. 2. 术语.定义和缩 略语 2.1. 术语和定义 2.1.1. 坐标转换coordinate transformation 采用适用的转换模型和转换参数,将大地测量控制点坐标从某一坐标系转换到另一坐标系.

Cocos2d-X中的坐标

在Cocos2d-x中坐标可以分成四种: 1.GL坐标体系:GL坐标体系左下角为坐标原点,X轴向右,Y轴向上 2.UI坐标体系:UI坐标体系左上角为坐标原点,X轴向右,Y轴向上. 3.世界坐标体系:是窗口的坐标体系,它是GL坐标体系,它是左下角为坐标原点,X轴向右,Y轴向上. 4. 结点坐标体系:是Node的坐标体系,它是GL坐标体系,和世界坐标体系不同的是,它的原点是结点的左下角, 当一个结点调用SetPosition时,使用的参数是它的父结点(渲染树)的坐标体系 CCLayer默认大小和窗口

flash代码

Flash常用的动作命令一.Flash中的常用命令:1.在当前帧停止播放 on(release){ stop();} 2.从当前帧开始播放 on(release){ play();} 3.跳到第 10 帧,并且从第 10 帧开始播放 on(release){ gotoAndPlay(10);} 4.跳到第 20 帧,并且停止在该帧 on(release){ gotoAndStop(20);} 5.跳到下一个场景,并且继续播放 on(release){ nextScene(); play();}

游戏中的三角学——Sprite Kit 和 Swift 教程(1)

原文链接 : Trigonometry for Games – Sprite Kit and Swift Tutorial: Part 1/2 原文作者 : Nick Lockwood 译文出自 : 开发技术前线 www.devtf.cn 译者 : kmyhy 更新 2015/04/20:升级至 Xcode 6.3 和 Swift 1.2 更新说明:这是我们广受欢迎的教程之一的第三个版本--第一个版本是 Cocos2D 的,由 Matthijs Hollemans 缩写,第二个版本由 Tony

Unity 2D入门基础教程

注:这是根据网上教程完成的. 翻译:http://blog.1vr.cn/?p=1422 原文:http://www.raywenderlich.com/61532/unity-2d-tutorial-getting-started 如果用以前版本的Unity做2D游戏,虽然能做,但是要费很多周折. 比如你可以将一张纹理赋予一个”面片”网格,然后用脚本控制它的动画调整它的位移.如果你要使用物理引擎,那么还要将这个Obeject处理3D的,所以 你还要确保你的Object要有足够的深度以确保他们在

亲热接触Redis-第一天

引言 nosql,大规模分布式缓存遍天下.Internet的时代在中国由其走得前沿,这一切归功于我国特色的电商. 因此nosql.大数据技术在中国应用的比国外还要前沿. 从这一章開始我们将開始进入到真正的SOA.PAAS.SAAS.互联网的领域,因此每一篇我都会添加一小段业务的基础知识,让大家在学习技术的同一时候也能够了解一些业务,这边的业务不是指的business logic不是让大家去做业务人员,而是为大家带来IDEA,"没有做不到仅仅有想不到",阿里支付宝为什么发了...不是技术

Unity-LightMap

烘焙:把灯光信息写到贴图里 比如影子 AO 这样就可以不用实时灯光 是节省硬件资源的一种较好的方法 Realtime GI(Global Illumination),实时全局光照 首先来说说GI是干啥的,非常粗略的来描述下,如果说我们以前的光照系统就是由光源 - 物体 - 视点组成的话,那么全局光照系统就是由光源 - n多环境反射光 - 物体 - 视点.就是说GI额外包括了环境反射光的计算,它可以使得渲染出来的场景物体间的光影交互更为真实. 如果是离线烘焙的话,n多的环境反射光就是通过辐射度算法