Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "CsCharMoveComponent.h"
- #include "Math/Vector.h"
- // Called when the game starts or when spawned
- UCsCharMovementComponent::UCsCharMovementComponent(const FObjectInitializer& ObjectInitializer)
- : Super(ObjectInitializer)
- {
- }
- void UCsCharMovementComponent::BeginPlay()
- {
- Super::BeginPlay();
- //UE_LOG(LogTemp, Warning, TEXT("H2"));
- UE_LOG(LogTemp, Log, TEXT("Walk Speed: %.1f"), WalkSpeed);
- UE_LOG(LogTemp, Log, TEXT("Walk Accel: %.1f"), WalkAccel);
- UE_LOG(LogTemp, Log, TEXT("Friction: %.1f"), Friction);
- UE_LOG(LogTemp, Log, TEXT("Air Speed: %.1f"), AirSpeed);
- UE_LOG(LogTemp, Log, TEXT("Air Accel: %.1f"), AirAccel);
- UE_LOG(LogTemp, Log, TEXT("Gravity: %.1f"), GetGravityZ());
- }
- bool UCsCharMovementComponent::ResolvePenetrationImpl(const FVector& Adjustment, const FHitResult& Hit, const FQuat& NewRotation)
- {
- //UE_LOG(LogTemp, Log, TEXT("ResolveImpl"));
- JustTeleported |= Super::ResolvePenetrationImpl(Adjustment, Hit, NewRotation);
- return JustTeleported;
- }
- void UCsCharMovementComponent::HandleImpact(const FHitResult& Hit, float TimeSlice, const FVector& MoveDelta)
- {
- //UE_LOG(LogTemp, Log, TEXT("HandleImpact"));
- }
- void UCsCharMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
- {
- InputVector = ConsumeInputVector().GetClampedToSize(1.0, 1.0);
- //UE_LOG(LogTemp, Warning, TEXT("Input: %.1f %.1f %.1f"), InputVector.X, InputVector.Y, InputVector.Z);
- PerformMovement(DeltaTime);
- //UE_LOG(LogTemp, Log, TEXT("Vel: %.1f %.1f %.1f"), _Velocity.X, _Velocity.Y, _Velocity.Z);
- }
- FVector UCsCharMovementComponent::Accelerate(float DeltaTime, FVector WishDir, float WishSpeed, float Accel)
- {
- // See if we are changing direction a bit
- float ProjectedSpeed = FVector::DotProduct(_Velocity, WishDir);
- // Reduce wishspeed by the amount of veer.
- float AddSpeed = WishSpeed - ProjectedSpeed;
- // If not going to add any speed, done.
- if (AddSpeed <= 0)
- return FVector(0,0,0);
- // Determine amount of accleration.
- float AccelSpeed = Accel * WishSpeed * DeltaTime;
- // Cap at addspeed
- if (AccelSpeed > AddSpeed)
- AccelSpeed = AddSpeed;
- // Adjust velocity.
- _Velocity += AccelSpeed * WishDir;
- return AccelSpeed * WishDir;
- }
- void UCsCharMovementComponent::PerformMovement(float DeltaTime)
- {
- _Velocity = VC;
- auto V0 = _Velocity;
- UE_LOG(LogTemp, Log, TEXT("== PerformMovement Start ================"))
- UE_LOG(LogTemp, Log, TEXT("_Velocity0: %s"), *_Velocity.ToString());
- UE_LOG(LogTemp, Log, TEXT("V0: %s"), *V0.ToString());
- TryJump();
- //ApplyCrouch();
- UE_LOG(LogTemp, Log, TEXT("_Velocity1: %s"), *_Velocity.ToString());
- if (IsGrounded())
- {
- ApplyFriction(DeltaTime);
- LastAccel = WalkMove(DeltaTime);
- UE_LOG(LogTemp, Log, TEXT("_Velocity2: %s"), *_Velocity.ToString());
- }
- else
- {
- LastAccel = AirMove(DeltaTime);
- UE_LOG(LogTemp, Log, TEXT("_Velocity2: %s"), *_Velocity.ToString());
- }
- VC = _Velocity;
- MoveComponent(DeltaTime);
- UE_LOG(LogTemp, Log, TEXT("_Velocity Final: %s"), *_Velocity.ToString());
- UE_LOG(LogTemp, Log, TEXT("V0 Final: %s"), *V0.ToString());
- if (IsGrounded()) {
- // TryStayOnGround();
- }
- UE_LOG(LogTemp, Log, TEXT("=============================="));
- }
- void UCsCharMovementComponent::MoveComponent(float DeltaTime)
- {
- JustTeleported = false;
- FVector Movement = _Velocity * DeltaTime;
- if (IsGrounded())
- {
- //Movement.Z = -FVector::DotProduct(GroundNormal, Movement) / GroundNormal.Z;
- }
- if (true)//(!Movement.IsNearlyZero())
- {
- FHitResult Hit;
- bool surf = false;
- auto p0 = UpdatedComponent->GetComponentLocation();
- auto move0 = Movement;
- auto v0 = _Velocity;
- SafeMoveUpdatedComponent(Movement, UpdatedComponent->GetComponentRotation(), true, Hit);
- //UE_LOG(LogTemp, Log, TEXT("_Velocity: %.1f %.1f %.1f"), _Velocity.X, _Velocity.Y, _Velocity.Z);
- // If we bumped into something, try to slide along it
- auto storehit = Hit.Normal;
- if (Hit.IsValidBlockingHit())
- {
- //UE_LOG(LogTemp, Log, TEXT("Hit Normal: %.1f %.1f %.1f"), Hit.Normal.X, Hit.Normal.Y, Hit.Normal.Z);
- if (Hit.Normal.Z >= MinWalkableZ)
- {
- IsOnGround = true;
- GroundNormal = Hit.Normal;
- WasCorrecting = true;
- }
- else {
- IsOnGround = false;
- surf = true;
- }
- FVector StartPosition = UpdatedComponent->GetComponentLocation(), EndPosition;
- float AdjustedTime = 1.f - Hit.Time;
- UE_LOG(LogTemp, Log, TEXT("Hit normal before slide: %s"), *(Hit.Normal).ToString());
- SlideAlongSurface(Movement, AdjustedTime, Hit.Normal, Hit, true);
- if (true)// (!JustTeleported)
- {
- EndPosition = UpdatedComponent->GetComponentLocation();
- _Velocity = (EndPosition - StartPosition) / (AdjustedTime * DeltaTime);
- VC = (EndPosition - StartPosition) / (AdjustedTime * DeltaTime);
- if (surf) {
- auto grav = GetGravityZ() * DeltaTime;
- UE_LOG(LogTemp, Log, TEXT("== Tick Info =========================================================="));
- UE_LOG(LogTemp, Log, TEXT("Hit Normal: %s"), *(storehit).ToString());
- UE_LOG(LogTemp, Log, TEXT("Gravity Accel: %.1f"), (GetGravityZ() * DeltaTime));
- UE_LOG(LogTemp, Log, TEXT("Strafe Accel: %s, %.1f"), *LastAccel.ToString(), LastAccel.Size());
- auto truegrav = (FVector(0, 0, -1) * GetGravityZ() * DeltaTime) - (FVector::DotProduct(FVector(0, 0, -1) * GetGravityZ() * DeltaTime, storehit));
- auto truestrafe = (LastAccel-FVector::DotProduct(LastAccel,storehit));
- UE_LOG(LogTemp, Log, TEXT("True Gravity: %.1f"), truegrav.Z);
- UE_LOG(LogTemp, Log, TEXT("True Strafe: %.1f"), truestrafe.Z);
- auto rawmove = p0 + move0;
- auto clipped = rawmove - StartPosition; //clip distance
- auto projectioncheck = FVector::DotProduct(rawmove - EndPosition, Hit.Normal);
- /*
- auto cresolution = clipped - FVector::DotProduct(clipped, Hit.Normal); //unclipped end position
- auto cfullmove = (StartPosition + cresolution) - p0;
- auto cfullmovevel = cfullmove / DeltaTime;
- auto cunclipvel = cresolution / (AdjustedTime * DeltaTime);
- auto unclipprojection = FVector::DotProduct(rawmove - (StartPosition + cresolution),Hit.Normal);
- */
- UE_LOG(LogTemp, Log, TEXT("== Tick Info =========================================================="));
- UE_LOG(LogTemp, Log, TEXT("Movement start position: %s"), *p0.ToString());
- UE_LOG(LogTemp, Log, TEXT("Planned movement: %s"), *move0.ToString());
- UE_LOG(LogTemp, Log, TEXT("Planned end position: %s"), *rawmove.ToString());
- UE_LOG(LogTemp, Log, TEXT("Initial velocity: %s"), *v0.ToString());
- UE_LOG(LogTemp, Log, TEXT("== Collision Info =========================================================="));
- UE_LOG(LogTemp, Log, TEXT("Collision start position: %s"), *StartPosition.ToString());
- UE_LOG(LogTemp, Log, TEXT("Collision end position: %s"), *EndPosition.ToString());
- UE_LOG(LogTemp, Log, TEXT("Resolved _Velocity: %s"), *_Velocity.ToString());
- UE_LOG(LogTemp, Log, TEXT("projection . hitnormal (should be ~0): %.1f"), projectioncheck);
- /*
- UE_LOG(LogTemp, Log, TEXT("== Calculated Info =========================================================="));
- UE_LOG(LogTemp, Log, TEXT("Calculated end position: %s"), *(StartPosition+cresolution).ToString());
- UE_LOG(LogTemp, Log, TEXT("Calculated full velocity: %s"), *cfullmovevel.ToString());
- UE_LOG(LogTemp, Log, TEXT("Calculated resolution velocity: %s"), *cunclipvel.ToString());
- UE_LOG(LogTemp, Log, TEXT("Calculated projection check: %.1f"), unclipprojection);
- UE_LOG(LogTemp, Log, TEXT("Gravity: %.1f"), GetGravityZ());
- UE_LOG(LogTemp, Log, TEXT("=============================================================================="));
- */
- /*
- bool customResolution = false;
- if (customResolution) {
- _Velocity = (v0-(Hit.Normal*FVector::DotProduct(v0,Hit.Normal)));
- if (FVector::DotProduct(_Velocity, Hit.Normal) < 0) {
- _Velocity = _Velocity - (Hit.Normal * FVector::DotProduct(_Velocity, Hit.Normal));
- }
- //UpdatedComponent->SetRelativeLocation(p0 + _Velocity*DeltaTime);
- }
- */
- UpdatedComponent->SetWorldLocation(EndPosition);
- }
- }
- }
- else {
- /*
- if (WasCorrecting) {
- WasCorrecting = false;
- }
- else {
- IsOnGround = false;
- }
- */
- IsOnGround = false;
- }
- }
- }
- void UCsCharMovementComponent::TryJump()
- {
- if (!IsGrounded())
- return;
- if (IsJumpHeld) {
- IsJumpHeld = false;
- _Velocity.Z += JumpSpeed;
- IsOnGround = false;
- UE_LOG(LogTemp, Log, TEXT("Jump %.1f"), JumpSpeed);
- }
- }
- void UCsCharMovementComponent::ApplyCrouch()
- {
- }
- void UCsCharMovementComponent::TryStayOnGround()
- {
- FVector StartLocation = UpdatedComponent->GetComponentLocation();
- FHitResult Hit;
- SafeMoveUpdatedComponent(FVector(0, 0, -10), UpdatedComponent->GetComponentRotation(), true, Hit);
- if (!Hit.bBlockingHit)
- {
- UpdatedComponent->SetWorldLocation(StartLocation);
- IsOnGround = false;
- }
- else if (Hit.Normal.Z >= MinWalkableZ)
- {
- GroundNormal = Hit.Normal;
- }
- else
- {
- IsOnGround = false;
- }
- //UE_LOG(LogTemp, Log, TEXT("Ground: %.1f %.1f %.1f"), GroundNormal.X, GroundNormal.Y, GroundNormal.Z);
- }
- FVector UCsCharMovementComponent::WalkMove(float DeltaTime)
- {
- auto frame = Accelerate(DeltaTime, InputVector, WalkSpeed, WalkAccel);
- _Velocity.Z += GetGravityZ() * DeltaTime;
- return frame;
- }
- FVector UCsCharMovementComponent::AirMove(float DeltaTime)
- {
- auto frame = Accelerate(DeltaTime, InputVector, AirSpeed, AirAccel);
- _Velocity.Z += GetGravityZ() * DeltaTime;
- return frame;
- }
- void UCsCharMovementComponent::ApplyFriction(float DeltaTime)
- {
- float Speed = _Velocity.Size();
- if (Speed == 0)
- return;
- float Drop = Friction * DeltaTime;
- float NewSpeed = Speed - Drop;
- if (NewSpeed < 0)
- NewSpeed = 0;
- _Velocity *= (NewSpeed / Speed);
- //UE_LOG(LogTemp, Log, TEXT("Friction"));
- }
- bool UCsCharMovementComponent::IsGrounded()
- {
- return IsOnGround;
- }
- void UCsCharMovementComponent::AddImpulseCs(FVector Impulse, bool b_VelocityChange)
- {
- UE_LOG(LogTemp, Log, TEXT("IMPULSE ADDED"));
- if (b_VelocityChange)
- _Velocity += Impulse;
- else
- _Velocity += Impulse / Mass;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement