Loading...
Battle Tank
Project Type Persoanl project
Software Used Unreal Engine 4, Visual Studio
Languages Used C++, Blueprint
Primary Role(s) Gameplay programming

What I Did



  • Tank Movement With Xbox one controller support
  • Suspension system for tank's Track using ue4 physics with C++
  • Build for xbox one (In Progress)
  • Simple A.I using C++ to drive enemy tanks.
  Tank suspension
The suspension was made to reduce the collisions with the terrain that made the tank movement very buggy and sometimes tank flies

As you can see that is how the Sprung Wheel Suspension work.
text_to_describe_your_image

text_to_describe_your_image

text_to_describe_your_image


void ASprungWheel::BeginPlay()
{
	Super::BeginPlay();

	Wheel->SetNotifyRigidBodyCollision(true);
	// Regestring on hit event
	Wheel->OnComponentHit.AddDynamic(this, &ASprungWheel::OnHit);

	SetupSuspension();
	
}

// Called every frame
void ASprungWheel::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	if (GetWorld()->TickGroup == TG_PostPhysics)
	{
		TotalForceMagnitudeThisFrame = 0.0f;
	}


}

void ASprungWheel::SetupSuspension()
{
	if (!GetAttachParentActor()) { return; }
	UPrimitiveComponent* RootComponent = Cast(GetAttachParentActor()->GetRootComponent());
	if (!RootComponent) { return; }
	MassWheelConstraint->SetConstrainedComponents(RootComponent, NAME_None, Axle, NAME_None);
	AxleWheelConstraint->SetConstrainedComponents(Axle, NAME_None, Wheel, NAME_None);
}


void ASprungWheel::AddDrivingForce(float ForceMagnitude)
{
	TotalForceMagnitudeThisFrame += ForceMagnitude;
}

void ASprungWheel::OnHit(UPrimitiveComponent * HitComponent, AActor * OtherActor, 
						 UPrimitiveComponent * OtherComp, FVector NormalImpulse, const FHitResult & Hit)
{
	ApplyForce();
}

void ASprungWheel::ApplyForce()
{
	Wheel->AddForce(Axle->GetForwardVector() * TotalForceMagnitudeThisFrame);
}

		

  controls and tanks movement behaviour
In this pic are the controls mapping text_to_describe_your_image

Binding Actions using blueprints text_to_describe_your_image

Driving Logic (Some pics) text_to_describe_your_image

text_to_describe_your_image

  Enemy Tank AI
This is a simple AI using C++

#include "BattleTank.h"
#include "TankAimingComponent.h"
#include "TankAIController.h"
#include "Tank.h" // So we can impliment OnDeath

// Depends on movement component via pathfinding system

void ATankAIController::BeginPlay()
{
	Super::BeginPlay();
}

void ATankAIController::SetPawn(APawn* InPawn)
{
	Super::SetPawn(InPawn);
	if (InPawn)
	{
		auto PossessedTank = Cast(InPawn);
		if (!PossessedTank) { return; }

		// Subscribe our local method to the tank's death event
		PossessedTank->OnDeath.AddUniqueDynamic(this, &ATankAIController::OnPossedTankDeath);
	}
}

void ATankAIController::OnPossedTankDeath()
{
	if (!ensure(GetPawn())) { return; } // TODO remove if ok
	GetPawn()->DetachFromControllerPendingDestroy();
}

// Called every frame
void ATankAIController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

	auto PlayerTank = GetWorld()->GetFirstPlayerController()->GetPawn();
	auto ControlledTank = GetPawn();

	if (!PlayerTank && !ControlledTank) { return; }
	
	// Move towards the player
	MoveToActor(PlayerTank, AcceptanceRadius); // TODO check radius is in cm

	// Aim towards the player
	auto AimingComponent = ControlledTank->FindComponentByClass();
	AimingComponent->AimAt(PlayerTank->GetActorLocation());

	if (AimingComponent->GetFiringState() == EFiringState::Locked)
	{
		AimingComponent->Fire(); // TODO limit firing rate
	}
}
		



Click Here To Go To The Project On Github


Copyright © 2018 by Mazen Morgan and David Wagih