• News
• About

• Industries

• Learning & Support

• Community

• Marketplace

Download # OnComponentHit: velocities and impulse calculation

I have done this tutorial: https://wiki.unrealengine.com/First_Person_Shooter_C%2B%2B_Tutorial#Creating_a_GameMode It works fine. Now I try to modify OnComponentHit handler. The idea is a physics impulse is depended of a mass of a projectile and of a velocity difference between the projectile and an object which hit by the projectile.

The base code (from the tutorial above) is:

``````

void AFPSProjectile::OnHit(AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
if ( OtherActor && (OtherActor != this) && OtherComp )
{
OtherComp->AddImpulseAtLocation(ProjectileMovement->Velocity * 100.0f, Hit.ImpactPoint);
}
}

``````

As we can see, an impulse is calculated as a product of a velocity of a projectile and its mass (a constant 100.0f).

I wrote next code:

``````

FVector  OAV = Other_Actor->GetVelocity();
FVector  Velocity = Movement_Component->Velocity - OAV;
Other_Component->AddImpulseAtLocation(
Velocity * Collision_Component->GetMass(),
Hit.ImpactPoint);

``````

In the code I get a velocity of a target actor (I use a big StaticMeshActor cube as a target). Then I calculate a difference between velocities of the projectile and the target, and then I calculate the impulse and call AddImpulseAtLocation.

Visually all works correct. But when I set a breakpoint to this handler I saw that the velocity of the target is not zero although the target is a cube which initially stays on ground on my level (it starts movement only after hitting).

What is wrong? Or, maybe, my code is fundamentally incorrect?

Did you resolve it? At what problem?