U3D差集判断是否在左边或右边,点积判断是否在前后

差集-判断左右

比较差积的y,小于0是左,大于0是右

 

默认用法,不支持旋转:

var cross = Vector3.Cross(lhsObject.transform.position, rhsObject.transform.position);
if (cross.y > 0) //side
if(cross.y < 0)//another side

 

 

用forward,从而支持旋转:

void OnCollisionEnter(Collision other) {
     Vector3 direction = other.contacts[0].point - transform.position;
     var cross = Vector3.Cross(transform.forward, direction);
     if (cross.y < 0) print ("Left");
         else print("right");
 }
 
 function OnCollisionEnter(other:Collision){
     var direction:Vector3 = other.contacts[0].point - transform.position;
     var cross :Vector3 = Vector3.Cross(transform.forward, direction);
     if (cross.y < 0) print ("Left");
         else print("right");
 }

 

 

论坛上还看见一个demo,加深对差集的理解:(得到一个forward,up方向,取得right,left)

// original vector:
  var forward: Vector3 = Vector3(0.5, 0.7, 0.8);
  // up direction:
  var up: Vector3 = Vector3(0.0, 1.0, 0.0);
  // find right vector:
  var right = Vector3.Cross(forward.normalized, up.normalized);
  var left = -right;

 

点集-判断前后

点集用法差不多,A和B比较,结果大于0,B在前,结果小于0,B在后

直接贴出支持旋转的代码:(comparePoint是B)

var dir = comparePoint - tfm.position;
var dot = Vector3.Dot(tfm.forward, dir);
return dot;

 

你可能感兴趣的:(3D)