- 最后登录
- 2013-9-29
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 6371
- 纳金币
- 6372
- 精华
- 0
|
这个是限制相机的移动范围 。
下图为相机的移动的限制图。红色部分为可移动范围。
属性:
[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();
}
} |
|