文谷首页 | 业界传真 | 网络技术 | 服务器 | 数据库 | 存储技术 | 系统安全 | 无线技术 | Cisco | .Net | Windows | Linux | Unix | Java
电子商务 | 网站工程 | 网页设计 | 平面设计 | 多媒体 | 编程语言 | Oracle | MSSQL | Photoshop | ASP | PHP | 实用技巧 | 进程查询 | 文谷论坛
Java频道
 资讯动态   考试认证   新手入门   核心技术   高级技术   J2EE   J2ME   XML   开源技术   其他技术
您现在的位置: IT文谷 >> 开发平台 >> Java >> J2ME >> 核心技术 >> 文章正文
关于J2ME——MIDP1.0中的碰撞检测关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测
关于J2ME——MIDP1.0中的碰撞检测
关于J2ME——MIDP1.0中的碰撞检测关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测
关于J2ME——MIDP1.0中的碰撞检测关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测

  在MIDP1.0中,我们不能像MIDP2.0中的Sprite类一样有很方便的碰撞函数可以使用,我们只能自己来写代码实现。常见的碰撞检测的方式是基于矩形的碰撞,因为我们的图片都是矩形的。检测矩形碰撞的一种方式是看一个矩形的4个角是否进入另一个矩形内。假如我们有一个Actor类,也就是我们的一个自定义的类似于精灵类吧,那么我们可以给他定义一个这样的方法:
  
  /**
  * 检测一个特定点是否进入一个Actor内
  * @param px 特定点的x坐标.
  * @param py 特定点的y坐标
  * @return 如果特定点进入Actor范围内,那么返回true,否则为false;
  */
  public boolean isCollidingWith(int px, int py)
  {
  if (px >= getX() && px <= (getX() + getActorWidth()) &&
  py >= getY() && py <= (getY() + getActorHeight()) )
  return true;
  return false;
  }
  
  /**
  * 检测一个Actor对象是否碰上了当前的Actor对象。
  * @param another 另一个Actor对象
  * @return 如果another和当前对象发生碰撞,则返回true,否则为false.
  */
  public boolean isCollidingWith(Actor another)
  {
  // check if any of our corners lie inside the other actor's
  // bounding rectangle
  if (isCollidingWith(another.getX(), another.getY()) ||
  isCollidingWith(another.getX() + another.getActorWidth(), another.getY()) ||
  isCollidingWith(another.getX(), another.getY() + another.getActorHeight()) ||
  isCollidingWith(another.getX() + another.getActorWidth(),
  another.getY()+ another.getActorHeight()))
  return true;
  else
  return false;
  }
  
  关于矩形碰撞检测,还有一个更简单的方式就是判断一个矩形的4条边是否在另一个矩形的4条边之外。因此我们可以写一个更加通用快速的简单的碰撞方法:
  
  /**
  * 较为通用的矩形碰撞检测方法
  * @param ax a矩形左上角x坐标
  * @param ay a矩形左上角y坐标
  * @param aw a矩形宽度
  * @param ah a矩形高度
  * @param bx b矩形左上角x坐标
  * @param by b矩形左上角y坐标
  * @param bw b矩形宽度
  * @param bh b矩形高度
  * @return
  */
  public static final boolean isIntersectingRect(int ax, int ay, int aw,
  int ah, int bx, int by, int bw, int bh) {
  if (by + bh < ay || // is the bottom of b above the top of a?
  by > ay + ah || // is the top of b below bottom of a?
  bx + bw < ax || // is the right of b to the left of a?
  bx > ax + aw) // is the left of b to the right of a?
  return false;
  
  return true;
  }
  
  这样速度会快很多。对于有透明背景的图片,我们可以围绕非透明部分多设立几个矩形区进行碰撞检测。

关于J2ME——MIDP1.0中的碰撞检测关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测
  • 上一篇文章:

  • 下一篇文章:
  • 进入论坛讨论

    相关文章
    我的Thinking in Java学习笔记
    使用JBoss和Eclipse创建J2EE应用
    SpringFramework(9)
    NoTitle385
    向Web Service进军--Axis+Tomcat模拟一个银行存取款
    JAVA中的指针,引用及对象的clone
    JAVA中用动态代理类实现记忆功能(二)
    JAVA中用动态代理类实现记忆功能(一)
    DB Test
    基金系统的初步研究(1)
    SpringFramework(8)
    深入浅出Java clone技术(2)
    热门文章最新推荐

    版权与免责声明:
    ① 本网转载其他媒体稿件是为传播更多的信息,此类稿件不代表本网观点,版权归原作者所有,本网不承担此类稿件侵权行为的连带责任。
    ② 本站原创文章,转载时请注明出自文谷及作者姓名
    ③在本网BBS上发表言论者,文责自负。
    ④如您因版权等问题需要与本网联络,请在30日内联系 。
    关于J2ME——MIDP1.0中的碰撞检测关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测
    关于J2ME——MIDP1.0中的碰撞检测关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测2006-7-19 23:14:02关于J2ME——MIDP1.0中的碰撞检测

    全站热点
    最新推荐
    关于文谷 | 联系文谷 | 免责声明 | 文谷论坛
    Tel: 0577-65690019      E-mail: ichenjian@gmail.com    MSN:ichenjian@hotmail.com    QQ:2911194
    Copyright © 2004-2008 wengu.com 文谷 All Rights Reserved
    浙ICP备05000327号