CharacterExample.cs 5.6 KB

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