国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

關于J2ME——MIDP1.0中的碰撞檢測

2019-11-18 12:44:00
字體:
來源:轉載
供稿:網友

  在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;
  }
  
  這樣速度會快很多。對于有透明背景的圖片,我們可以圍繞非透明部分多設立幾個矩形區進行碰撞檢測。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 颍上县| 阿鲁科尔沁旗| 攀枝花市| 杭州市| 宜宾县| 龙海市| 屯留县| 渭南市| 江都市| 五华县| 桃源县| 元朗区| 鲜城| 丁青县| 海阳市| 贵州省| 大洼县| 翁源县| 银川市| 峨边| 舒兰市| 高碑店市| 赤壁市| 大连市| 剑阁县| 临猗县| 新野县| 泸定县| 岳普湖县| 大姚县| 米泉市| 普定县| 葫芦岛市| 辉县市| 赤壁市| 鄂托克旗| 宝山区| 上林县| 惠水县| 日土县| 安顺市|