实现物体绕不同轴旋转,并可以外部调用的函数

第一个文件,声明枚举类型,分别为均匀变化和加速变化


1

2

3

4

5

6

7

8

using UnityEngine;

using System.Collections;

public enum CTRotationType

{

    Uniform,

    AccelerateUniformly

}

第二个文件:主函数,实现围绕轴变化的两个函数,分别为均匀变化和加速变化

 


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

using UnityEngine;

using System.Collections;

public class CTRotation : MonoBehaviour {

 // Use this for initialization

 void Start () {

 

 }

 

 // Update is called once per frame

 void Update () {

        if (isRotating)

        {

            executeRotate();

        }

    }

    bool isRotating = false;

    Quaternion definedRotation = new Quaternion(0, 0, 0,0);

 

    Vector3 rotateVector = new Vector3(1,0,0);

 

    float rotateVelocity = 0;

 

 float accelerateDuration = 0;

 float leftDuration = 0;

 float rotateDuration = 0;

 

    int rotateAxis = 0;

 float angleRange = 0;

 float deltaRotate = 0;//0;

 

 

 // acceleration when it is in the accelerating process.

    float rotateAcceleration = 0;

    CTRotationType rotateType;

 

 //int RotateType = 0;

    private void initRotateArgument( float _initAngleRange, int _initRotateAxis, float _initRotateDuration)

    {

  rotateAxis = _initRotateAxis;

        rotateDuration = _initRotateDuration;

        leftDuration = _initRotateDuration;

  angleRange = _initAngleRange;

  rotateType = CTRotationType.Uniform;

    }

    public void RotateTo(float _angleRange, int _axis, float _duration)

    {

  print("in the rotateto");

        isRotating = false;

        rotateType = CTRotationType.Uniform;

  //RotateType = 0;

  

  initRotateArgument(_angleRange, _axis, _duration);

  

  switch(rotateAxis)

  {

   case 0: //rotate around X axis

   {

    rotateVector = Vector3.right;

    break;

   }

   case 1://rotate around Y axis

   {

    rotateVector = Vector3.up;

    break;

   }

   case 2://rotate around Z axis

   {

    rotateVector = Vector3.forward;

    break;

   }

   default:

    break;

  }

  

  deltaRotate = angleRange/rotateDuration;

  

        isRotating = true;

    }

    public void RotateTo(float _angleRange, int _axis, float _duration, float _accelerateDuration)

    {

        isRotating = false;

        rotateType = CTRotationType.AccelerateUniformly;

  //RotateType = 1;

  rotateAcceleration = 1/((rotateDuration - accelerateDuration)*accelerateDuration);

        initRotateArgument(_angleRange, _axis, _duration);

  

  switch(rotateAxis)

  {

   case 0: //rotate around X axis

   {

    rotateVector = Vector3.right;

    break;

   }

   case 1://rotate around Y axis

   {

    rotateVector = Vector3.up;

    break;

   }

   case 2://rotate around Z axis

   {

    rotateVector = Vector3.forward;

    break;

   }

   default:

    break;

  }

        accelerateDuration = _accelerateDuration;

  

     //   deltaRotate = angleRange/(_duration - _accelerateDuration*2);

        isRotating = true;

    }

    void executeRotate()

    {

        switch (rotateType)

        {

            //case 0://CTMoveType.Uniform:

   case CTRotationType.Uniform:

                uniformRotate();

                break;

            //case 1://CTMoveType.AccelerateUniformly:

   case CTRotationType.AccelerateUniformly:

                accelerateRotate();

                break;

        }

       

        leftDuration -= Time.deltaTime;

       /* if (leftDuration <= 0)

        {

            transform.position = targetPosition;

            isMoving = false;

        }*/

    }

    private void accelerateRotate()

    {

  print(leftDuration);

        if (leftDuration > (rotateDuration - accelerateDuration))

        {

   rotateVelocity = (float)((angleRange*(rotateDuration - leftDuration))*rotateAcceleration);

          //  transform.Rotate(rotateVelocity * Time.deltaTime*rotateVector, Space.World);

   transform.Rotate(rotateVelocity * rotateVector*Time.deltaTime, Space.World);

        }

        else if (leftDuration > accelerateDuration)

        {

   rotateVelocity = (float)((angleRange*accelerateDuration)*rotateAcceleration);

            transform.Rotate(rotateVelocity*rotateVector*Time.deltaTime, Space.World);

        }

        else if (leftDuration > 0)

        {

   rotateVelocity= (float)((angleRange*leftDuration)*rotateAcceleration);

   transform.Rotate(rotateVelocity*rotateVector*Time.deltaTime, Space.World);

        }

  else

   isRotating = false;

    }

    private void uniformRotate()

    {

  print(leftDuration);

  //if(leftDuration)

  if(leftDuration > 0)

  {

   transform.Rotate(rotateVector*deltaRotate*Time.deltaTime, Space.World);

   //transform.Rotate(rotateVector * Time.deltaTime*deltaRotate, Space.World);

  }

  else

   isRotating = false;

    }

}

第三个文件,测试脚本


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

using UnityEngine;

using System.Collections;

public class TestRotationScript : MonoBehaviour {

 // Use this for initialization

 void Start () {

 

 }

 

 // Update is called once per frame

 void Update () {

      

 }

 void OnGUI ()

    {

  CTRotation ttscript;

  CTChangeAlpha colorScript;

  GameObject testObject = GameObject.Find("TestCube");

        //Component testObjectScript = testObject.GetComponent("CRotation");

        ttscript = (CTRotation)testObject.GetComponent("CTRotation");

  colorScript = (CTChangeAlpha)testObject.GetComponent("CTChangeAlpha");

  

   if (GUI.Button (new Rect (20,40,80,20), "UniRotate")) {

       ttscript.RotateTo(3600f, 2, 2f);

  }

  

   if(GUI.Button(new Rect(20,60,80,20),"AccRotate")){

  ttscript.RotateTo(3600f, 2, 2f, 0.5f);

   }

  

    if(GUI.Button(new Rect(20,80,80,20),"Color")){

     colorScript.ColorTo(2,5.0f);

    }

    }

}

其中:第一个和第二脚本赋给目标物体;第三个脚本赋给任何一个物体作为测试物体使用

代码目的是方便外部调用和函数重用;注意isRotating参数的使用

时间: 2025-01-08 14:59:46

实现物体绕不同轴旋转,并可以外部调用的函数的相关文章

cocos2dx--- Node 绕Y轴旋转

step += 5; float ra = (float) CC_DEGREES_TO_RADIANS(step); float i = sinf(ra) * pNode->getCamera()->getZEye(); float j = cosf(ra) * pNode->getCamera()->getZEye(); pNode->getCamera()->setEyeXYZ(i, 0, j); cocos2dx--- Node 绕Y轴旋转,布布扣,bubuko.

绕任意轴旋转的矩阵推导总结

前言 常用的几何变换中旋转是较为复杂的一种,最近看<Physically Based Rendering, Second Edition: From Theory To Implementation>一书涉及绕任意轴旋转的实现,也给出了大体思路,但具体的推导过程及最后的旋转矩阵并未直接地给出,故根据参考Animated CGEM: Rotation About an Arbitrary Axis总结(欢迎指正). (一)基础 1.点乘与叉乘 点乘(dot)亦称作内积或数量积,如图,a·b =

定点绕定轴旋转

通过两个已知点,绕定轴旋转360次,每次旋转一度,获得360个点,一次相连,可以获得一个近似圆.网上找的公式基本都是左手坐标系下的选择公式,故而进行了多次坐标转换 1.3.2版本 public static List<Vector3d> Translate(List<Vector3> list) { List<Vector3d> zuoshou = new List<Vector3d>();//存左手坐标系下的各点坐标 List<Vector3d>

(转)android3D动画,绕y轴旋转

原文地址:http://blog.csdn.net/x_i_a_o_h_a_i/article/details/40449847 其实网上的3D旋转的例子很多,在这里我只是想把其代码做一个解释. 先上图: 代码: TurnAroundActivity /** * 图片浏览器的主Activity. * * @author guolin */ public class TurnAroundActivity extends Activity { /** * 根布局 */ private Relativ

构建绕任意轴旋转的矩阵

转载请注明出处:http://www.cnblogs.com/jietian331/p/5634039.html

Unity3D 控制物体旋转详解 —— 自身绕轴旋转、缓慢旋转、鼠标控制旋转、欧拉数和四元数的关系

问题描述:昨天需要实现一个功能是根据指令左转90度或者右转90度,当时脑汁可能是有浆糊吧,居然要用直接赋值rotation,这样一来就要牵扯到eulerAngles和四元数的Euler函数了,结果忙活了好久没解决,bug层出,今天经过详细了解,解决了相关问题,一并把其他关于角度和旋转的知识点整理出来. 一.问题的解决:如何让物体绕自身轴旋转 直接上关键代码: player_cube.Rotate(-90, 0, 0); player_cube是获取的需要旋转的物体的Transform,其中-90

矩阵变换:沿任意轴旋转及其推导

http://blog.csdn.net/zsq306650083/article/details/8773996 1. 2D中绕原点旋转 设基向量p,q和r分别是朝向+x,+y和+z方向的单位向量. 旋转角度为θ,基向量p,q绕原点旋转,得到新的基向量p`和q` 即旋转矩阵R(θ)为 2. 3d中绕坐标轴旋转 01. 绕x轴旋转,基向量q和r旋转θ,得到新的基向量q`和r` 即旋转矩阵Rx(θ)为: 02. 绕y轴旋转,基向量p和r旋转θ,得到新的基向量p`和r` 即旋转矩阵Ry(θ)为: 0

直线绕z轴旋转所成曲面的方程

直线:(x-x0)/L=(y-y0)/M=(z-z0)/N绕z轴旋转所成曲面的方程为: x2+y2=α+β(z-z0)+γ(z-z0)2 其中 α=x02+y02 β=(2/N)(Lx0+My0) γ=(L2+M2)/N2 绕其他两个轴的方程类似于此. 原文地址:https://www.cnblogs.com/jmz11111/p/8157604.html

绕单轴的旋转矩阵

对于绕单轴的旋转矩阵,很多小伙伴分不清楚到底是B转W还是W转B的,下面就先用上面对旋转矩阵的理解来推导一下绕单轴的旋转矩阵,然后介绍下怎么记忆.注意对旋转正负号的定义,惯用的定义是,从原点沿着坐标轴看,顺时针为正.下面看图. 初始状态的W和B系绕Z轴旋转绕Y.X轴旋转 这样就通过计算B系各坐标轴在W系的投影得到了绕各轴旋转的  ,反之计算W系在B系的投影就可得到绕各轴旋转的  ,不过因为它们互为转置,所以得到了一个,就知道另一个了. 注意这里的W系和B系可以换成任意两个坐标系,只要把握哪个坐标系