点乘、叉乘、旋转实现
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class aaa1:MonoBehaviour
{
//点乘
float dot_multiply(Vector3 vec,Vector3 vec1)
{
float dis = Vector3.Dot(vec, vec1);
//float dis = (vec.x*vec1.x+vec.y*vec1.y+vec.z*vec1.z);
return dis;
}
//叉乘
Vector3 cross_multiply(Vector3 vec1, Vector3 vec2)
{
//Vector3 v = Vector3.Cross(vec1,vec2);
Vector3 v= new Vector3 (vec1.x * vec2.y - vec1.y * vec2.x , vec1.x * vec2.z - vec1.z * vec2.x , vec1.y * vec2.z - vec1.z * vec2.y);
return v;
}
//以自身中心点为基准 绕axis轴旋转一个角度angle。axis的坐标系是世界坐标系,angle的单位是弧度。
public Quaternion rotate(Vector3 v1, float angle)
{
//以轴和角度进行旋转,axis为轴即为一个向量,angle为角度
Quaternion q = Quaternion.AngleAxis(angle,v1);
//transform.rotation = Quaternion.identity * transform.rotation;
//transform.rotation = q;
return q;
}
public Quaternion rotate1()
{
//四元数对应的三个轴向的欧拉角,分别是绕x轴、y轴、z轴旋转的角度
Quaternion q1 = Quaternion.EulerAngles(20, 30, 40);
return q1;
}
//旋转量
public Quaternion rotate_amount()
{
float ff = 0.01;
Quaternion q1 = Quaternion.EulerAngles(20, 30, 40);
Quaternion q2 = Quaternion.EulerAngles(20, 30, 50);
//以maxDegreesDelta作为角度步长计算从from到to之间的旋转量
Quaternion Q = Quaternion.RotateTowards(q1, q2, ff );
return Q;
}
public Quaternion rotate_amount1()
{
Quaternion q1 = Quaternion.EulerAngles(20, 30, 40);
Quaternion q2 = Quaternion.EulerAngles(20, 30, 50);
//以maxDegreesDelta作为角度步长计算从from到to之间的旋转量
Quaternion Q = Quaternion.Slerp(q1, q2, 0.02);
return Q;
}
private void Start()
{
Vector3 v1 = new Vector3(1, 2, 3);
Vector3 v2 = new Vector3(2, 3, 4);
float f=dot_multiply(v1, v2);
Debug.Log("点乘:" + f);
Vector3 v3 = cross_multiply(v1, v2);
Debug.Log("叉乘:" + v3);
print(v3);
//旋转
Quaternion q1=rotate(Vector3.one, 30);
print("以极轴进行旋转q1:" + q1);
Quaternion q2 = rotate1();
print("以x y z进行旋转:"+q2);
Quaternion q3= rotate_amount();
Debug.Log("旋转量q3="+q3);
Quaternion q4 = rotate_amount();
Debug.Log("旋转量q4=" + q4);
}
}
投影----点乘
Vector3 touying(GameObject black_ball, Vector3 ying_axis)
{
//float dis = Vector3.Dot(black_ball.transform.position, ying_axis.normalized);
float dis = (black_ball.transform.position[0] * ying_axis.normalized[0]+
black_ball.transform.position[1] * ying_axis.normalized[1]+
black_ball.transform.position[2] * ying_axis.normalized[2]);
return new Vector3(0, 0, 0) + dis * ying_axis.normalized;
}
float touying2(GameObject black_ball, Vector3 ying_axis)
{
float dis = Vector3.Dot(black_ball.transform.position, ying_axis.normalized);
return dis;
}
叉乘
Vector3 chacheng(GameObject ball_A, GameObject ball_B, GameObject ball_C)
{
//Vector3 fa = Vector3.Cross(ball_B.transform.position - ball_A.transform.position, ball_C.transform.position - ball_A.transform.position);
Vector3 xiangl_AB = ball_B.transform.position - ball_A.transform.position;
Vector3 xiangl_AC = ball_C.transform.position - ball_A.transform.position;
Vector3 fa = new Vector3(xiangl_AB[1] * xiangl_AC[2] - xiangl_AB[2] * xiangl_AC[1],
xiangl_AB[2] * xiangl_AC[0] - xiangl_AB[0] * xiangl_AC[2],
xiangl_AB[0] * xiangl_AC[1] - xiangl_AB[1] * xiangl_AC[0]
);
return fa;
}