ViaCognita
(ViaCognita)
November 16, 2016, 9:01am
1
I’m learning Unreal creating a Pong game and I have this code to ball movement:
// Use a UProjectileMovementComponent to govern this projectile's movement
BallMovement = CreateDefaultSubobject<UProjectileMovementComponent>(this, TEXT("ProjectileComp"));
BallMovement->UpdatedComponent = CollisionComp;
BallMovement->InitialSpeed = 3000.f;
BallMovement->MaxSpeed = 3000.f;
BallMovement->Friction = 0.f;
BallMovement->ProjectileGravityScale = 0.f;
BallMovement->bRotationFollowsVelocity = false;
BallMovement->bShouldBounce = true;
BallMovement->Velocity = FVector(0.f, 1.f, 0.f);
But the ball loses speed when it bounces. I’m testing it make it bounces between two walls.
How can I do to avoid lose speed?
Teiwaz
(Teiwaz)
November 17, 2016, 9:12am
2
Try messing with the restitution setting on the physical material used on your walls. That affects “bounciness” of the wall.
Alternatively, you could try overriding the ComputeBounceResult function to scale the MoveDelta to be the same magnitude as the projectile’s velocity before the bounce result.
ViaCognita
(ViaCognita)
December 1, 2016, 6:33pm
3
I did it with this:
void APongBallActor::NotifyHit(UPrimitiveComponent* MyComp, AActor* Other, UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult & Hit)
{
Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit);
FVector ReflectedVelocity = -2 * FVector::DotProduct(BallMovement->Velocity, HitNormal) * HitNormal + BallMovement->Velocity;
BallMovement->Velocity = ReflectedVelocity;
}