2011-04-20 85 views
0

那么我怎样才能限制我的物体在球体中的运动呢?我尝试了边界框和交集方法,但它似乎不工作?球体中的物体(边界球体),希望它限制球体内的运动

编辑1:

,这不是一个问题...我问:我有2个包围球,一大一小,小会大的内部运动,但我不希望它走到大范围外面我该怎么做?

+0

[Object in sphere(bounding sphere),想要限制球体内部的运动]的可能重复(http://stackoverflow.com/questions/5736236/object-in-spherebounding-sphere-want-it-to-限制运动中之球) – 2011-04-20 22:50:47

回答

0

小球体中心距大球体中心的距离不得超过(BigRadius - SmallRadius)。如果要计算接触的位置,可以通过接触所需的半径差异与从起​​点到终点的半径总差异的比率来缩放运动矢量(除非该运动足够大以至于交叉)

class Sphere { 
    public Vector3 Pos; 
    public double Radius; 
} 

void Test() 
{ 
    Sphere big = new Sphere() { Pos = new Vector3(0, 0, 0), Radius = 10.0 }; 
    Sphere small = new Sphere() { Pos = new Vector3(8, 0, 0), Radius = 1.0 }; 
    Vector3 move = new Vector3(0, 10, 0); 

    // new position without check, if within sphere ok 
    Vector3 newPos = small.Pos + move; 
    if (Vector3.Distance(newPos, big.Pos) < big.Radius - small.Radius) 
     return; 

    // diff in radius to contact with outer shell 
    double space = (big.Radius - small.Radius) - Vector3.Distance(small.Pos, big.Pos); 

    // movement past moment of hitting outer shell 
    double far = Vector3.Distance(newPos, big.Pos) - (big.Radius - small.Radius); 

    // scale movement by ratio to get movement to hit shell 
    move = move * (space/(space + far)); 
    newPos = small.Pos + move; 
} 

由大包围球的其中一个轴定义的平面,我认为这会工作,除非移动穿过由大球的坐标之一限定的平面和移动之外。也许你可以添加特殊的检查标志的运动X,Y或Z不同于big.Pos - small.Pos(小球体中心到大球体中心的相对位置)X,Y或Z ...

1

确保中心距不超过半径差值有一个更快的方法。它不需要每次都取一个平方根。

预先计算max_distance_squared每当半径的一个设置或更改,并保存它的地方可以重复使用:

local max_distance = big.radius - small.radius 
max_distance_squared = max_distance*max_distance  

现在你可以省略求平方根,得到的距离:

local dx = big.center_x - small.center_x 
local dy = big.center_y - small.center_y 
if (dx*dx + dy*dy <= max_distance_squared) 
    # great 
else 
    # explode 

节省的时间可以提高帧频或模拟速度。