CharacterExample.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using AtomicEngine;
  3. public class CharacterExample : CSComponent
  4. {
  5. void Start()
  6. {
  7. SubscribeToEvent<KeyDownEvent>(e =>
  8. {
  9. if (e.Key == Constants.KEY_ESCAPE)
  10. GetSubsystem<Engine>().Exit();
  11. });
  12. Node objectNode = Scene.CreateChild("AtomicMutant");
  13. objectNode.Scale = new Vector3(1.5f, 1.5f, 1.5f);
  14. objectNode.Position = Node.Position;
  15. // spin node
  16. Node adjustNode = objectNode.CreateChild("AdjNode");
  17. adjustNode.Rotation = Quaternion.FromAxisAngle(new Vector3(0, 1, 0), 180);
  18. // Create the rendering component + animation controller
  19. AnimatedModel animatedModel = adjustNode.CreateComponent<AnimatedModel>();
  20. animatedModel.Model = AtomicNET.Cache.GetResource<Model>("Models/Mutant/Mutant.mdl");
  21. animatedModel.Material = AtomicNET.Cache.GetResource<Material>("Models/Mutant/Materials/mutant_M.material");
  22. animatedModel.CastShadows = true;
  23. // Create animation controller
  24. adjustNode.CreateComponent<AnimationController>();
  25. // Create rigidbody, and set non-zero mass so that the body becomes dynamic
  26. RigidBody body = objectNode.CreateComponent<RigidBody>();
  27. body.CollisionLayer = 1;
  28. body.Mass = 1.0f;
  29. // Set zero angular factor so that physics doesn't turn the character on its own.
  30. // Instead we will control the character yaw manually
  31. body.AngularFactor = Vector3.Zero;
  32. // Set the rigidbody to signal collision also when in rest, so that we get ground collisions properly
  33. body.CollisionEventMode = CollisionEventMode.COLLISION_ALWAYS;
  34. // Set a capsule shape for collision
  35. CollisionShape shape = objectNode.CreateComponent<CollisionShape>();
  36. shape.SetCapsule(1.5f, 1.8f, new Vector3(0.0f, 0.9f, 0.0f));
  37. character = objectNode.CreateComponent<Character>();
  38. AtomicNET.GetSubsystem<Renderer>().ShadowMapSize = 2048;
  39. // Get Camera from Scene
  40. Vector<Node> children = new Vector<Node>();
  41. Scene.GetChildrenWithComponent<Camera>(children, true);
  42. if (children.Size > 0)
  43. {
  44. cameraNode = children[0];
  45. }
  46. }
  47. Node cameraNode;
  48. Character character;
  49. float pitch = 0.0f;
  50. float yaw = 0.0f;
  51. void Update(float timeStep)
  52. {
  53. var input = GetSubsystem<Input>();
  54. input.SetMouseVisible(false);
  55. yaw += (float)input.MouseMoveX * PITCH_SENSITIVITY;
  56. pitch += (float)input.MouseMoveY * YAW_SENSITIVITY;
  57. // Limit pitch
  58. pitch = Clamp<float>(pitch, -80.0f, 80.0f);
  59. // Set rotation already here so that it's updated every rendering frame instead of every physics frame
  60. character.Node.Rotation = Quaternion.FromAxisAngle(Vector3.Up, yaw);
  61. if (input.GetKeyPress(Constants.KEY_F))
  62. {
  63. viewMode += 1;
  64. if (viewMode == 2)
  65. viewMode = 0;
  66. }
  67. }
  68. void PostUpdate(float timeStep)
  69. {
  70. if (cameraNode == null)
  71. return;
  72. Node characterNode = character.Node;
  73. // Get camera lookat dir from character yaw + pitch
  74. Quaternion rot = characterNode.Rotation;
  75. Quaternion dir = rot * Quaternion.FromAxisAngle(Vector3.Right, pitch);
  76. // Turn head to camera pitch, but limit to avoid unnatural animation
  77. Node headNode = characterNode.GetChild("Mutant:Head", true);
  78. float limitPitch = Clamp<float>(pitch, -45.0f, 45.0f);
  79. Quaternion headDir = rot * Quaternion.FromAxisAngle(new Vector3(1.0f, 0.0f, 0.0f), limitPitch);
  80. // This could be expanded to look at an arbitrary target, now just look at a point in front
  81. Vector3 headWorldTarget = headNode.WorldPosition + headDir * (new Vector3(0.0f, 0.0f, -1.0f));
  82. headNode.LookAt(headWorldTarget, new Vector3(0.0f, 1.0f, 0.0f));
  83. if (firstPerson)
  84. {
  85. cameraNode.Position = headNode.WorldPosition + rot * (new Vector3(0.0f, 0.15f, 0.2f));
  86. cameraNode.Rotation = dir;
  87. }
  88. else
  89. {
  90. // Third person camera: position behind the character
  91. Vector3 aimPoint;
  92. aimPoint = characterNode.Position + rot * (new Vector3(0.0f, 1.7f, 0.0f));
  93. // Collide camera ray with static physics objects (layer bitmask 2) to ensure we see the character properly
  94. Vector3 rayDir;
  95. if (viewMode == 0)
  96. rayDir = dir * Vector3.Back;
  97. else
  98. {
  99. dir = rot * Quaternion.FromAxisAngle(Vector3.Up, 180.0f);
  100. rayDir = dir * Vector3.Back;
  101. }
  102. float rayDistance = CAMERA_INITIAL_DIST;
  103. //PhysicsRaycastResult result;
  104. //scene_->GetComponent<PhysicsWorld>()->RaycastSingle(result, Ray(aimPoint, rayDir), rayDistance, 2);
  105. //if (result.body_)
  106. // rayDistance = Min(rayDistance, result.distance_);
  107. rayDistance = Clamp<float>(rayDistance, CAMERA_MIN_DIST, CAMERA_MAX_DIST);
  108. cameraNode.Position = aimPoint + rayDir * rayDistance;
  109. cameraNode.Rotation = dir;
  110. }
  111. }
  112. static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
  113. {
  114. if (val.CompareTo(min) < 0) return min;
  115. else if (val.CompareTo(max) > 0) return max;
  116. else return val;
  117. }
  118. const float CAMERA_MIN_DIST = 1.0f;
  119. const float CAMERA_INITIAL_DIST = 6.0f;
  120. const float CAMERA_MAX_DIST = 20.0f;
  121. const float YAW_SENSITIVITY = 0.1f;
  122. const float PITCH_SENSITIVITY = 0.1f;
  123. int viewMode;
  124. bool firstPerson = false;
  125. }