Skip to content

Commit

Permalink
BT82 Avoiding Boolean Flags.cmproj
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Tristem committed Jul 19, 2016
1 parent 8e48c62 commit fa5baeb
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Binary file modified BattleTank/Content/Tank/Tank_BP.uasset
Binary file not shown.
Binary file modified BattleTank/Content/_Levels/BattleGround.umap
Binary file not shown.
23 changes: 15 additions & 8 deletions BattleTank/Source/BattleTank/Private/TankTrack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

UTankTrack::UTankTrack()
{
PrimaryComponentTick.bCanEverTick = true;
PrimaryComponentTick.bCanEverTick = false;
}

void UTankTrack::BeginPlay()
Expand All @@ -15,14 +15,16 @@ void UTankTrack::BeginPlay()

void UTankTrack::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit)
{
UE_LOG(LogTemp, Warning, TEXT("I'm hit, I'm hit!"))
DriveTrack();
ApplySidewaysForce();
CurrentThrottle = 0;
}

void UTankTrack::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
void UTankTrack::ApplySidewaysForce()
{
auto SlippageSpeed = FVector::DotProduct(GetRightVector(), GetComponentVelocity());

// Work-out the required acceleration this frame to correct
auto SlippageSpeed = FVector::DotProduct(GetRightVector(), GetComponentVelocity());
auto DeltaTime = GetWorld()->GetDeltaSeconds();
auto CorrectionAcceleration = -SlippageSpeed / DeltaTime * GetRightVector();

// Calculate and apply sideways (F = m a)
Expand All @@ -33,8 +35,13 @@ void UTankTrack::TickComponent(float DeltaTime, enum ELevelTick TickType, FActor

void UTankTrack::SetThrottle(float Throttle)
{
auto ForceApplied = GetForwardVector() * Throttle * TrackMaxDrivingForce;
CurrentThrottle = FMath::Clamp<float>(CurrentThrottle + Throttle, -1, 1);
}

void UTankTrack::DriveTrack()
{
auto ForceApplied = GetForwardVector() * CurrentThrottle * TrackMaxDrivingForce;
auto ForceLocation = GetComponentLocation();
auto TankRoot = Cast<UPrimitiveComponent>(GetOwner()->GetRootComponent());
TankRoot->AddForceAtLocation(ForceApplied, ForceLocation);
}
TankRoot->AddForceAtLocation(ForceApplied, ForceLocation);
}
6 changes: 5 additions & 1 deletion BattleTank/Source/BattleTank/Public/TankTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ class BATTLETANK_API UTankTrack : public UStaticMeshComponent

virtual void BeginPlay() override;

virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
void ApplySidewaysForce();

UFUNCTION()
void OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComponent, FVector NormalImpulse, const FHitResult& Hit);

void DriveTrack();

float CurrentThrottle = 0;
};

0 comments on commit fa5baeb

Please sign in to comment.