Anyone know where I can find a simple raycast with trace example in C++ ?

Hello All,

I searched a lot of places but what I find seems to be pretty outdated and lacking explanation.
Unreal has great blueprint documentation but the C++ is surly lacking…

Went through some of the C++ tutorials but I still have not found one of the most important things I need to program anything more advanced…
Raycasting / Linecasting with channels / layers and casting the returned object to another object of type… so I can work with it…
Brings me to another issue, Not sure how we can do msg based classes in unreal 4 like the ability to talk to multiple classes without them knowing much about each other…(prob another thread)

I would like a C++ example of something like this… but I cant seem to find much… and this is more then what I need to just start.
https://docs.unrealengine.com/latest/INT/Gameplay/HowTo/UseRaycasts/Blueprints/index.html

Even a basic start would be nice…
Just draw a line with a basic color across the screen using a vector forward from the player controller.
I think that would help get started with more advanced ray casting / line tracing methods.

I want to do them all in C++, because this would be 10x faster then blueprints and ray casting can get expensive.

Thanks!

Here’s something I personally use in my game. Read the comments I added for more info
Goodluck!

.h



static bool Trace(UWorld* world, AActor* actorToIgnore, const FVector& start, const FVector& dir, float length, FHitResult& hit, ECollisionChannel CollisionChannel = ECC_Pawn, bool ReturnPhysMat = false);
static bool Trace(UWorld* world, AActor* actorToIgnore, const FVector& start, const FVector& end, FHitResult& hit, ECollisionChannel CollisionChannel = ECC_Pawn, bool ReturnPhysMat = false);


.cpp



//Trace using start point, direction, and length
bool UT::Trace(UWorld* world, AActor* actorToIgnore, const FVector& start, const FVector& dir, float length, FHitResult& hit, ECollisionChannel collisionChannel, bool returnPhysMat){
	return Trace(world, actorToIgnore, start, start + dir*length, hit, collisionChannel, returnPhysMat);
}
//Trace using start point, and end point
bool UT::Trace(UWorld* world, AActor* actorToIgnore, const FVector& start, const FVector& end, FHitResult& hit, ECollisionChannel collisionChannel, bool returnPhysMat){
	if (!world)
	{
		return false;
	}

	//Trace params, set the 'false' to 'true' if you want it to trace against the actual meshes instead of their collision boxes.
	FCollisionQueryParams TraceParams(FName(TEXT("VictoreCore Trace")), false, actorToIgnore);
	TraceParams.bReturnPhysicalMaterial = returnPhysMat;

	//Ignore Actors, usually the actor that is calling the trace
	TraceParams.AddIgnoredActor(actorToIgnore);

	//Re-initialize hit info, so you can call the function repeatedly and hit will always be fresh
	hit = FHitResult(ForceInit);

	//Trace!
	bool hitSomething = world->LineTraceSingle(
		hit,		//result
		start,	//start
		end, //end
		collisionChannel, //collision channel
		TraceParams
	);

	// Draw a square at the impact point.
	if (hitSomething) DrawDebugPoint(world, hit.ImpactPoint, 10, FColor(255, 255, 0), false, -1);
	
	// Draw the trace line. Red if something was hit, green if nothing was hit.
	DrawDebugLine(world, start, end, (hitSomething ? FColor(255, 0, 0) : FColor(0, 255, 0)), false, -1, 0, 1.5);

	return hitSomething;
}


Calling the code



FHitResult hit;
UT::Trace(GetWorld(), this, GetActorLocation(), -FVector::UpVector, 10, hit);
//if you set ReturnPhysMat to true, the hit will also contain the materials physmat


I made em static in a class called UT, so they can be called from anywhere after including the “UT.h”, I’d recommend putting em in a static class too!

Thanks man, I had some real life things but I plan to get back into this soon.


GetWorld()->LineTraceSingleByChannel()

Is the function you probably want. There are many different versions of it, some sweep shapes instead of lines (rays), some test for object channels instead of trace channels etc.

EDIT: Also Jezuz, bump much?