开发者

getting random points in a cone of vision

i am trying to make my game character(fishes) go to a new generated destination only on what their current cone of vision is able to "see", therefore i have drawn a triangle MC to make it their cone of vision and from there generate random points within the area of the cone, and the code i have used is shown below, and it kept returning the top coords of my stage why is that so? i have done a localToGlobal and it is still the same

public function getRandomDestination():void
{
    //Math.floor(Math.random()*(1+High-Low))+Low;
    var conePoint:Point = new Point(Math.floor(Math.random() * cone.width), Math.floor(Math.random() * cone.height));
    _destinationX = localToGlobal(conePoint).x;//Math.floor(Math.random()*(1+_maxX-100)+100);
    tra开发者_如何学编程ce(_destinationX);
    _destinationY = localToGlobal(conePoint).y; //Math.floor(Math.random() *(1+_maxY-100)+100);
    trace(_destinationY);
}

getting random points in a cone of vision


Wouldn't it be easier to first select a random angle (in a radius specified) and then use that angle to select a random distance from the fish?

Something like:

function selectPosition(maxAngle:Number, maxDistance:Number):Object
{
    var base:Number = rotation + (-(maxAngle/2) + Math.random()*maxAngle);
    var radians:Number = base / 180 * Math.PI;

    var dist:Number = Math.random()*maxDistance;

    return {
        x: Math.cos(radians) * dist,
        y: Math.sin(radians) * dist
    };
}

var result:Object = selectPosition(30, 100);
trace(result.x, result.y);

Which will return an object containing and x and y point that falls within the angle and distance specified.

The above can be visually represented like this:

getting random points in a cone of vision


The way you calculate the random point in a rectangle is invalid. First of all you are using a rectangle not a cone (cone is a three-dimensional geometric shape). This is how I would calculate a random point within a rectangle (I'm going to use an isosceles triangle)

WARNING: amazing MSPaint skillz.

getting random points in a cone of vision

First we get the position on the y axis using Math.randon() witch gives us a random value between 0 and 1 (call it rndY).

Next we need to calculate the position on the x axis. We calculate it like this:

rndX = 0.5 - rndY/2 + rndY * Math.random()

This ensures us that the position on the x axis will always be within the the triangle.

To fix your localtoglobal problem more source or a fla would be helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜