Advertisement
Guest User

movement implementation

a guest
Nov 6th, 2022
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.28 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2. #include "CsCharMoveComponent.h"
  3. #include "Math/Vector.h"
  4.  
  5. // Called when the game starts or when spawned
  6. UCsCharMovementComponent::UCsCharMovementComponent(const FObjectInitializer& ObjectInitializer)
  7. : Super(ObjectInitializer)
  8. {
  9.  
  10. }
  11. void UCsCharMovementComponent::BeginPlay()
  12. {
  13. Super::BeginPlay();
  14. //UE_LOG(LogTemp, Warning, TEXT("H2"));
  15.  
  16. UE_LOG(LogTemp, Log, TEXT("Walk Speed: %.1f"), WalkSpeed);
  17. UE_LOG(LogTemp, Log, TEXT("Walk Accel: %.1f"), WalkAccel);
  18. UE_LOG(LogTemp, Log, TEXT("Friction: %.1f"), Friction);
  19. UE_LOG(LogTemp, Log, TEXT("Air Speed: %.1f"), AirSpeed);
  20. UE_LOG(LogTemp, Log, TEXT("Air Accel: %.1f"), AirAccel);
  21. UE_LOG(LogTemp, Log, TEXT("Gravity: %.1f"), GetGravityZ());
  22.  
  23. }
  24.  
  25. bool UCsCharMovementComponent::ResolvePenetrationImpl(const FVector& Adjustment, const FHitResult& Hit, const FQuat& NewRotation)
  26. {
  27. //UE_LOG(LogTemp, Log, TEXT("ResolveImpl"));
  28. JustTeleported |= Super::ResolvePenetrationImpl(Adjustment, Hit, NewRotation);
  29. return JustTeleported;
  30. }
  31.  
  32. void UCsCharMovementComponent::HandleImpact(const FHitResult& Hit, float TimeSlice, const FVector& MoveDelta)
  33. {
  34. //UE_LOG(LogTemp, Log, TEXT("HandleImpact"));
  35. }
  36.  
  37. void UCsCharMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  38. {
  39.  
  40. InputVector = ConsumeInputVector().GetClampedToSize(1.0, 1.0);
  41.  
  42. //UE_LOG(LogTemp, Warning, TEXT("Input: %.1f %.1f %.1f"), InputVector.X, InputVector.Y, InputVector.Z);
  43.  
  44. PerformMovement(DeltaTime);
  45.  
  46. //UE_LOG(LogTemp, Log, TEXT("Vel: %.1f %.1f %.1f"), _Velocity.X, _Velocity.Y, _Velocity.Z);
  47. }
  48.  
  49. FVector UCsCharMovementComponent::Accelerate(float DeltaTime, FVector WishDir, float WishSpeed, float Accel)
  50. {
  51. // See if we are changing direction a bit
  52. float ProjectedSpeed = FVector::DotProduct(_Velocity, WishDir);
  53.  
  54. // Reduce wishspeed by the amount of veer.
  55. float AddSpeed = WishSpeed - ProjectedSpeed;
  56.  
  57. // If not going to add any speed, done.
  58. if (AddSpeed <= 0)
  59. return FVector(0,0,0);
  60.  
  61. // Determine amount of accleration.
  62. float AccelSpeed = Accel * WishSpeed * DeltaTime;
  63.  
  64. // Cap at addspeed
  65. if (AccelSpeed > AddSpeed)
  66. AccelSpeed = AddSpeed;
  67.  
  68. // Adjust velocity.
  69. _Velocity += AccelSpeed * WishDir;
  70.  
  71. return AccelSpeed * WishDir;
  72.  
  73. }
  74.  
  75. void UCsCharMovementComponent::PerformMovement(float DeltaTime)
  76. {
  77. _Velocity = VC;
  78. auto V0 = _Velocity;
  79. UE_LOG(LogTemp, Log, TEXT("== PerformMovement Start ================"))
  80. UE_LOG(LogTemp, Log, TEXT("_Velocity0: %s"), *_Velocity.ToString());
  81. UE_LOG(LogTemp, Log, TEXT("V0: %s"), *V0.ToString());
  82. TryJump();
  83. //ApplyCrouch();
  84.  
  85. UE_LOG(LogTemp, Log, TEXT("_Velocity1: %s"), *_Velocity.ToString());
  86. if (IsGrounded())
  87. {
  88. ApplyFriction(DeltaTime);
  89. LastAccel = WalkMove(DeltaTime);
  90. UE_LOG(LogTemp, Log, TEXT("_Velocity2: %s"), *_Velocity.ToString());
  91. }
  92. else
  93. {
  94. LastAccel = AirMove(DeltaTime);
  95. UE_LOG(LogTemp, Log, TEXT("_Velocity2: %s"), *_Velocity.ToString());
  96. }
  97.  
  98. VC = _Velocity;
  99. MoveComponent(DeltaTime);
  100. UE_LOG(LogTemp, Log, TEXT("_Velocity Final: %s"), *_Velocity.ToString());
  101. UE_LOG(LogTemp, Log, TEXT("V0 Final: %s"), *V0.ToString());
  102. if (IsGrounded()) {
  103. // TryStayOnGround();
  104. }
  105. UE_LOG(LogTemp, Log, TEXT("=============================="));
  106. }
  107.  
  108. void UCsCharMovementComponent::MoveComponent(float DeltaTime)
  109. {
  110. JustTeleported = false;
  111. FVector Movement = _Velocity * DeltaTime;
  112. if (IsGrounded())
  113. {
  114. //Movement.Z = -FVector::DotProduct(GroundNormal, Movement) / GroundNormal.Z;
  115. }
  116.  
  117. if (true)//(!Movement.IsNearlyZero())
  118. {
  119. FHitResult Hit;
  120. bool surf = false;
  121. auto p0 = UpdatedComponent->GetComponentLocation();
  122. auto move0 = Movement;
  123. auto v0 = _Velocity;
  124. SafeMoveUpdatedComponent(Movement, UpdatedComponent->GetComponentRotation(), true, Hit);
  125. //UE_LOG(LogTemp, Log, TEXT("_Velocity: %.1f %.1f %.1f"), _Velocity.X, _Velocity.Y, _Velocity.Z);
  126. // If we bumped into something, try to slide along it
  127. auto storehit = Hit.Normal;
  128. if (Hit.IsValidBlockingHit())
  129. {
  130. //UE_LOG(LogTemp, Log, TEXT("Hit Normal: %.1f %.1f %.1f"), Hit.Normal.X, Hit.Normal.Y, Hit.Normal.Z);
  131. if (Hit.Normal.Z >= MinWalkableZ)
  132. {
  133. IsOnGround = true;
  134. GroundNormal = Hit.Normal;
  135. WasCorrecting = true;
  136. }
  137. else {
  138. IsOnGround = false;
  139. surf = true;
  140. }
  141.  
  142. FVector StartPosition = UpdatedComponent->GetComponentLocation(), EndPosition;
  143. float AdjustedTime = 1.f - Hit.Time;
  144. UE_LOG(LogTemp, Log, TEXT("Hit normal before slide: %s"), *(Hit.Normal).ToString());
  145. SlideAlongSurface(Movement, AdjustedTime, Hit.Normal, Hit, true);
  146.  
  147. if (true)// (!JustTeleported)
  148. {
  149. EndPosition = UpdatedComponent->GetComponentLocation();
  150. _Velocity = (EndPosition - StartPosition) / (AdjustedTime * DeltaTime);
  151. VC = (EndPosition - StartPosition) / (AdjustedTime * DeltaTime);
  152. if (surf) {
  153. auto grav = GetGravityZ() * DeltaTime;
  154. UE_LOG(LogTemp, Log, TEXT("== Tick Info =========================================================="));
  155. UE_LOG(LogTemp, Log, TEXT("Hit Normal: %s"), *(storehit).ToString());
  156. UE_LOG(LogTemp, Log, TEXT("Gravity Accel: %.1f"), (GetGravityZ() * DeltaTime));
  157. UE_LOG(LogTemp, Log, TEXT("Strafe Accel: %s, %.1f"), *LastAccel.ToString(), LastAccel.Size());
  158. auto truegrav = (FVector(0, 0, -1) * GetGravityZ() * DeltaTime) - (FVector::DotProduct(FVector(0, 0, -1) * GetGravityZ() * DeltaTime, storehit));
  159. auto truestrafe = (LastAccel-FVector::DotProduct(LastAccel,storehit));
  160. UE_LOG(LogTemp, Log, TEXT("True Gravity: %.1f"), truegrav.Z);
  161. UE_LOG(LogTemp, Log, TEXT("True Strafe: %.1f"), truestrafe.Z);
  162.  
  163. auto rawmove = p0 + move0;
  164. auto clipped = rawmove - StartPosition; //clip distance
  165. auto projectioncheck = FVector::DotProduct(rawmove - EndPosition, Hit.Normal);
  166. /*
  167. auto cresolution = clipped - FVector::DotProduct(clipped, Hit.Normal); //unclipped end position
  168. auto cfullmove = (StartPosition + cresolution) - p0;
  169. auto cfullmovevel = cfullmove / DeltaTime;
  170. auto cunclipvel = cresolution / (AdjustedTime * DeltaTime);
  171. auto unclipprojection = FVector::DotProduct(rawmove - (StartPosition + cresolution),Hit.Normal);
  172. */
  173.  
  174. UE_LOG(LogTemp, Log, TEXT("== Tick Info =========================================================="));
  175. UE_LOG(LogTemp, Log, TEXT("Movement start position: %s"), *p0.ToString());
  176. UE_LOG(LogTemp, Log, TEXT("Planned movement: %s"), *move0.ToString());
  177. UE_LOG(LogTemp, Log, TEXT("Planned end position: %s"), *rawmove.ToString());
  178. UE_LOG(LogTemp, Log, TEXT("Initial velocity: %s"), *v0.ToString());
  179. UE_LOG(LogTemp, Log, TEXT("== Collision Info =========================================================="));
  180. UE_LOG(LogTemp, Log, TEXT("Collision start position: %s"), *StartPosition.ToString());
  181. UE_LOG(LogTemp, Log, TEXT("Collision end position: %s"), *EndPosition.ToString());
  182. UE_LOG(LogTemp, Log, TEXT("Resolved _Velocity: %s"), *_Velocity.ToString());
  183. UE_LOG(LogTemp, Log, TEXT("projection . hitnormal (should be ~0): %.1f"), projectioncheck);
  184. /*
  185. UE_LOG(LogTemp, Log, TEXT("== Calculated Info =========================================================="));
  186. UE_LOG(LogTemp, Log, TEXT("Calculated end position: %s"), *(StartPosition+cresolution).ToString());
  187. UE_LOG(LogTemp, Log, TEXT("Calculated full velocity: %s"), *cfullmovevel.ToString());
  188. UE_LOG(LogTemp, Log, TEXT("Calculated resolution velocity: %s"), *cunclipvel.ToString());
  189. UE_LOG(LogTemp, Log, TEXT("Calculated projection check: %.1f"), unclipprojection);
  190. UE_LOG(LogTemp, Log, TEXT("Gravity: %.1f"), GetGravityZ());
  191. UE_LOG(LogTemp, Log, TEXT("=============================================================================="));
  192. */
  193.  
  194. /*
  195. bool customResolution = false;
  196. if (customResolution) {
  197. _Velocity = (v0-(Hit.Normal*FVector::DotProduct(v0,Hit.Normal)));
  198. if (FVector::DotProduct(_Velocity, Hit.Normal) < 0) {
  199. _Velocity = _Velocity - (Hit.Normal * FVector::DotProduct(_Velocity, Hit.Normal));
  200. }
  201. //UpdatedComponent->SetRelativeLocation(p0 + _Velocity*DeltaTime);
  202. }
  203. */
  204. UpdatedComponent->SetWorldLocation(EndPosition);
  205. }
  206.  
  207. }
  208. }
  209. else {
  210. /*
  211. if (WasCorrecting) {
  212. WasCorrecting = false;
  213. }
  214. else {
  215. IsOnGround = false;
  216. }
  217. */
  218. IsOnGround = false;
  219. }
  220. }
  221. }
  222.  
  223. void UCsCharMovementComponent::TryJump()
  224. {
  225. if (!IsGrounded())
  226. return;
  227. if (IsJumpHeld) {
  228. IsJumpHeld = false;
  229. _Velocity.Z += JumpSpeed;
  230. IsOnGround = false;
  231. UE_LOG(LogTemp, Log, TEXT("Jump %.1f"), JumpSpeed);
  232. }
  233. }
  234.  
  235. void UCsCharMovementComponent::ApplyCrouch()
  236. {
  237.  
  238. }
  239.  
  240. void UCsCharMovementComponent::TryStayOnGround()
  241. {
  242. FVector StartLocation = UpdatedComponent->GetComponentLocation();
  243. FHitResult Hit;
  244. SafeMoveUpdatedComponent(FVector(0, 0, -10), UpdatedComponent->GetComponentRotation(), true, Hit);
  245. if (!Hit.bBlockingHit)
  246. {
  247. UpdatedComponent->SetWorldLocation(StartLocation);
  248. IsOnGround = false;
  249. }
  250. else if (Hit.Normal.Z >= MinWalkableZ)
  251. {
  252. GroundNormal = Hit.Normal;
  253. }
  254. else
  255. {
  256. IsOnGround = false;
  257. }
  258. //UE_LOG(LogTemp, Log, TEXT("Ground: %.1f %.1f %.1f"), GroundNormal.X, GroundNormal.Y, GroundNormal.Z);
  259. }
  260.  
  261.  
  262. FVector UCsCharMovementComponent::WalkMove(float DeltaTime)
  263. {
  264. auto frame = Accelerate(DeltaTime, InputVector, WalkSpeed, WalkAccel);
  265. _Velocity.Z += GetGravityZ() * DeltaTime;
  266. return frame;
  267. }
  268.  
  269. FVector UCsCharMovementComponent::AirMove(float DeltaTime)
  270. {
  271. auto frame = Accelerate(DeltaTime, InputVector, AirSpeed, AirAccel);
  272. _Velocity.Z += GetGravityZ() * DeltaTime;
  273. return frame;
  274. }
  275.  
  276. void UCsCharMovementComponent::ApplyFriction(float DeltaTime)
  277. {
  278. float Speed = _Velocity.Size();
  279. if (Speed == 0)
  280. return;
  281. float Drop = Friction * DeltaTime;
  282. float NewSpeed = Speed - Drop;
  283.  
  284. if (NewSpeed < 0)
  285. NewSpeed = 0;
  286.  
  287. _Velocity *= (NewSpeed / Speed);
  288.  
  289. //UE_LOG(LogTemp, Log, TEXT("Friction"));
  290. }
  291.  
  292. bool UCsCharMovementComponent::IsGrounded()
  293. {
  294. return IsOnGround;
  295. }
  296.  
  297. void UCsCharMovementComponent::AddImpulseCs(FVector Impulse, bool b_VelocityChange)
  298. {
  299. UE_LOG(LogTemp, Log, TEXT("IMPULSE ADDED"));
  300. if (b_VelocityChange)
  301. _Velocity += Impulse;
  302. else
  303. _Velocity += Impulse / Mass;
  304. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement