CharacterExample.cs 5.4 KB

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