How to controll an rigidbody

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • How to controll an rigidbody

      In this tutorial, I’ll explain how to control a Rigidbody to move an entity around—this is especially useful for character controllers or other physics-based moving entities.

      First, it’s important to understand that as soon as you attach a Rigidbody component to an entity, you no longer control the entity’s position or rotation directly through its properties. Instead, the Rigidbody now governs the entity's transformations, updating its position and rotation based on physics calculations.

      For character movement, you can use the LinearVelocity function, which allows you to set movement along specific axes. For rotation, the AngularVelocity function works similarly, controlling rotational movement around the specified axes. Both functions take a Vec3 as input, allowing you to define the exact axis on which you want to move or rotate the entity.


      If you want to move the character forward in the direction it’s currently facing, you can calculate the forward vector using the CalculateForwardVector function. Below is an example of how to move a character around:

      C Source Code

      1. using Genesis.Core;
      2. using Genesis.Core.Behaviors.Physics3D;
      3. using Genesis.Core.GameElements;
      4. using Genesis.Graphics;
      5. using Genesis.Math;
      6. using Genesis.Physics;
      7. using System;
      8. using System.Collections.Generic;
      9. using System.Linq;
      10. using System.Text;
      11. using System.Threading.Tasks;
      12. namespace Test3D
      13. {
      14. public class ThirdPersonController : IGameBehavior
      15. {
      16. public enum Animation
      17. {
      18. ANIMATION_IDLE,
      19. ANIMATION_WALK,
      20. ANIMATION_RUNNING,
      21. ANIMATION_JUMPING,
      22. ANIMATION_STRAFE_RIGHT,
      23. ANIMATION_STRAFE_LEFT,
      24. }
      25. public RigidBodyBehavior3D RigidBody { get; set; }
      26. public String IdleAnimation { get; set; }
      27. public String WalkAnimation { get; set; }
      28. public String RunningAnimation { get; set; }
      29. public String JumpingAnimation { get; set; }
      30. public String StrafeLeftAnimation { get; set; }
      31. public String StrafeRightAnimation { get; set; }
      32. public bool Airborn { get; set; }
      33. private long lastJump = 0;
      34. public ThirdPersonController(RigidBodyBehavior3D rigidBody)
      35. {
      36. this.RigidBody = rigidBody;
      37. rigidBody.OnCollide += RigidBody_OnCollide;
      38. }
      39. private void RigidBody_OnCollide(Scene scene, Game game, GameElement collision)
      40. {
      41. Console.WriteLine($"Coliding with {collision.Name}");
      42. this.Airborn = false;
      43. }
      44. public override void OnDestroy(Game game, GameElement parent)
      45. {
      46. }
      47. public override void OnInit(Game game, GameElement parent)
      48. {
      49. game.BeforeUpdate += Game_BeforeUpdate;
      50. }
      51. private void Game_BeforeUpdate(Game game, IRenderDevice renderDevice)
      52. {
      53. if(this.Airborn)
      54. {
      55. this.Airborn = false;
      56. }
      57. }
      58. public override void OnRender(Game game, GameElement parent)
      59. {
      60. }
      61. public override void OnUpdate(Game game, GameElement parent)
      62. {
      63. // Defines the used variables
      64. var model = (Model)parent;
      65. var animation = Animation.ANIMATION_IDLE;
      66. Vec3 linearVelocity = RigidBody.GetLinearVelocity();
      67. float angularVelocity = 0f;
      68. float speed = ((float)game.DeltaTime) * 0.2f;
      69. // Moving forwards & backwards
      70. if (Input.IsKeyDown(Input.Keys.W))
      71. {
      72. animation = Animation.ANIMATION_WALK;
      73. if (Input.IsKeyDown(Input.Keys.Shift))
      74. {
      75. speed = ((float)game.DeltaTime) * 0.5f;
      76. animation = Animation.ANIMATION_RUNNING;
      77. }
      78. var vec = RigidBody.CalculateForwardVector(-speed);
      79. linearVelocity.X = vec.X;
      80. linearVelocity.Z = vec.Z;
      81. }
      82. else if (Input.IsKeyDown(Input.Keys.S))
      83. {
      84. var vec = RigidBody.CalculateForwardVector(speed);
      85. linearVelocity.X = vec.X;
      86. linearVelocity.Z = vec.Z;
      87. animation = Animation.ANIMATION_WALK;
      88. }
      89. // Straving
      90. if(Input.IsKeyDown(Input.Keys.A))
      91. {
      92. var vec = RigidBody.CalculateRightVector(speed);
      93. linearVelocity.X = vec.X;
      94. linearVelocity.Z = vec.Z;
      95. animation = Animation.ANIMATION_STRAFE_LEFT;
      96. }
      97. else if(Input.IsKeyDown(Input.Keys.D))
      98. {
      99. var vec = RigidBody.CalculateRightVector(-speed);
      100. linearVelocity.X = vec.X;
      101. linearVelocity.Z = vec.Z;
      102. animation = Animation.ANIMATION_STRAFE_RIGHT;
      103. }
      104. // Jumping
      105. if (Input.IsKeyDown(Input.Keys.Space) && this.CanJump())
      106. {
      107. linearVelocity.Y += 10f;
      108. animation = Animation.ANIMATION_JUMPING;
      109. lastJump = Utils.GetCurrentTimeMillis();
      110. }
      111. // Turning the model
      112. if (Input.IsKeyDown(Input.Keys.Q))
      113. {
      114. angularVelocity += 0.5f;
      115. }
      116. else if (Input.IsKeyDown(Input.Keys.E))
      117. {
      118. angularVelocity -= 0.5f;
      119. }
      120. // Setup the velocity for the behavior
      121. RigidBody.LinearVelocity(linearVelocity);
      122. RigidBody.AngularVelocity(0f, angularVelocity, 0f);
      123. // Animation
      124. switch (animation)
      125. {
      126. case Animation.ANIMATION_IDLE:
      127. this.PlayAnimation(this.IdleAnimation, model);
      128. break;
      129. case Animation.ANIMATION_WALK:
      130. this.PlayAnimation(this.WalkAnimation, model);
      131. break;
      132. case Animation.ANIMATION_RUNNING:
      133. this.PlayAnimation(this.RunningAnimation, model);
      134. break;
      135. case Animation.ANIMATION_JUMPING:
      136. this.PlayAnimation(this.JumpingAnimation, model);
      137. break;
      138. case Animation.ANIMATION_STRAFE_RIGHT:
      139. this.PlayAnimation(this.StrafeRightAnimation, model);
      140. break;
      141. case Animation.ANIMATION_STRAFE_LEFT:
      142. this.PlayAnimation(this.StrafeLeftAnimation, model);
      143. break;
      144. default:
      145. this.PlayAnimation(this.IdleAnimation, model);
      146. break;
      147. }
      148. // Setup the camera
      149. var camera = game.SelectedScene.Camera;
      150. var fwd = RigidBody.CalculateForwardVector(1.5f);
      151. camera.Location = RigidBody.GetLocation() + fwd;
      152. camera.Location.Y += 0.75f;
      153. Utils.LookAt(camera, RigidBody.GetLocation());
      154. }
      155. private void PlayAnimation(String animation, Model model)
      156. {
      157. if (model.Animator.CurrentAnimation.Name != animation)
      158. {
      159. model.PlayAnimation(animation);
      160. }
      161. }
      162. private bool CanJump()
      163. {
      164. var now = Utils.GetCurrentTimeMillis();
      165. if(now > lastJump + 500)
      166. {
      167. return true;
      168. }
      169. return false;
      170. }
      171. }
      172. }
      Display All