在项目设置的描述页面填写版权说明
.
#include "BattleTank.h"
#include "TankTrack.h"
UTankTrack::UTankTrack() {
PrimaryComponentTick.bCanEverTick = false;
}
void UTankTrack::BeginPlay() {
OnComponentHit.AddDynamic(this, &UTankTrack::OnHit);
}
void UTankTrack::OnHit(UPrimitiveComponent* HitComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) {
DriveTracks(); // TODO CLAMP THROTTLE VALUE SO PLAYER CAN'T OVERDRIVE THE TRACKS !!!
ApplySidewaysForce(); // TODO CLAMP SIDEWAYS FORCE VALUE SO PLAYER CAN'T OVERDRIVE THE TRACKS !!!
CurrentThrottle = 0; // reset throttle after hit so tank doesn't keep going in the same direction
}
void UTankTrack::ApplySidewaysForce() { // TODO CLAMP SIDEWAYS FORCE VALUE SO PLAYER CAN'T OVERDRIVE THE TRACKS !!! *******SEE ABOVE*******
auto SlippageSpeed = FVector::DotProduct(GetRightVector(), GetComponentVelocity()); // get the right vector of tank and then dot product it with velocity to get slip speed (how fast it is slipping)
auto DeltaTime = GetWorld()->GetDeltaSeconds(); // how much time has passed since last frame - use this to calculate acceleration (delta velocity / delta time)
auto CorrectionAcceleration = -SlippageSpeed / DeltaTime * GetRightVector(); // calculate sideways force to apply to get back on track (acceleration is equal to delta velocity / delta time) ******NOTE: using negative value because we want it going opposite direction of current slip speed***** ***NOTE: GetRightVector gets the right vector of tank which will be the opposite direction of current slip speed so multiply by negative 1*** *** NOTE: divide by DeltaTime because we want acceleration which is change in velocity over change in time*** ******NOTE: multiplying by getrightvector will give us a vector that points in opposite direction of current slippage and has magnitude equal to acceleration****** ***** NOTE: this will give us an acceleration value that will counteract our current slippage and bring us back on track***** ****** THIS IS CALLED ANTI-SLIPPAGE SYSTEM AND IS COMMONLY USED IN CAR PHYSICS GAMES/SIMULATIONS TO KEEP CAR ON TRACK********* *** NOTE: if car isn't slipping then correctionAcceleration will be zero because SlippageSpeed would also be zero****** *** NOTE: If car is slipping left then correctionAcceleration would point right with magnitude equal to how fast its slipping left*** *** NOTE: if car was slipping too fast then correctionAcceleration would be large magnitude pointing right which would counteract the cars slippage and bring it back on track*** **NOTE: We are not actually applying this force directly here but rather passing it off as a parameter into TankMovement component where we can apply it with AddForceAtLocation function! ** **SEE TankMovement Component for explanation on why we do this! ** */ */ */ */ /**/ /**/ /**/ /**/ /**/ /*/*/*/*/*/*//*/*/*/*/*/*/*/*/ /*///*///*/// /*//////*///////****///////********//////************////////*****************************////////////////////////////********************************************************************************************************************///////////////////////////////////////////////////////////************/////////////////////////**////*************///*///////////**********************//////////////****************************/////////////////////////////////////////////////////////////////////////////////**///*************/////////////////////////////////////////////////////////**///*************/////////////////////////////////////////////////////////**///*************/////////////////////////////////////////////////////////**///*************/////////////////////////////////////////////////////////**///*************/////////////////////////////////////////////////////////**///*************/////////////////////////////////////////////////////////**
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。