查看: 3009|回复: 0
打印 上一主题 下一主题

away3D 碰撞问题

[复制链接]

435

主题

2

听众

6371

积分

高级设计师

Rank: 6Rank: 6

纳金币
6372
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-9-12 15:31:53 |只看该作者 |倒序浏览
这个是限制相机的移动范围 。

下图为相机的移动的限制图。红色部分为可移动范围。







属性:
  [Embed(source="dd.jpg")]

  private var CollisionBitmap:Class;

  private var collisionBitmap:BitmapData;

  private var sampleBitmap:BitmapData;

  private var sampleRect:Rectangle = new Rectangle();

  private var samplePointoint = new Point();

  private var collisionRect:Rectangle;

  private var cameraX:Number;

  private var cameraY:Number;

  private var collisionShape:Shape = new Shape();

  private var collisionVectoroint = new Point();

  private var collisionMatrix:Matrix = new Matrix();

  private var collisionDot:Number;

  private var collisionDistance:Number = 30;

  读取图片
  private function initCollisionBitmap():void

  {

   collisionBitmap = Cast.bitmap(CollisionBitmap);

   

   sampleBitmap = new BitmapData(collisionDistance*2, collisionDistance*2, false, 0);

  

   sampleRect = new Rectangle(0, 0, collisionDistance*2, collisionDistance*2);

  }

检测方法
private function checkCollisionData():void

  {

   //get camera position on collision map

   cameraX = 350 - camera.x*1.87;

   cameraY = 510 + camera.z*1.87;

   

   //determine position of sample data

   sampleRect.x = int(cameraX - collisionDistance);

   sampleRect.y = int(cameraY - collisionDistance);

   

   //check for collision

   var i:int = collisionDistance + 1;

   do {

    i--;

    collisionRect = sampleBitmap.getColorBoundsRect(0xFFFFFF, 0x000000);

    collisionShape.graphics.clear();

    collisionShape.graphics.beginFill(0x660000);

    collisionShape.graphics.drawCircle(collisionDistance, collisionDistance, i);

    collisionShape.graphics.endFill();

    sampleBitmap.copyPixels(collisionBitmap, sampleRect, samplePoint);

    sampleBitmap.draw(collisionShape, null, null, BlendMode.MULTIPLY);

   } while (sampleBitmap.getColorBoundsRect(0xFFFFFF, 0x000000).width && i > 1)

   

   //resolve collision

   if (i < collisionDistance) {

    //calculate collision normal

    collisionVector.x = collisionRect.x + collisionRect.width/2 - collisionDistance;

    collisionVector.y = collisionRect.y + collisionRect.height/2 - collisionDistance;

    collisionVector.normalize(1);

   

    //update camera position on collision map

    cameraX -= collisionVector.x*(collisionDistance - i);

    cameraY -= collisionVector.y*(collisionDistance - i);

   

    //updata camrea position

    camera.x = (350 - cameraX)/1.87;

    camera.z = (cameraY - 510)/1.87;

   

    //determin collision vector in camera-space

    collisionMatrix.identity();

    collisionMatrix.rotate(panangle * toRADIANS);

    collisionVector.x = -collisionVector.x;

    collisionVector = collisionMatrix.deltaTransformPoint(collisionVector);

   

    //update camera speed

    collisionDot = cameraRightSpeed*collisionVector.x + cameraForwardSpeed*collisionVector.y;

    cameraRightSpeed -= collisionDot*collisionVector.x;

    cameraForwardSpeed -= collisionDot*collisionVector.y;

   

    //re-check collision data

    checkCollisionData();

   }

  }
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-15 20:28 , Processed in 0.149541 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部