Add some randomness to the velocity when a ball bounces using UProjectiile Movement Component

I’m developing a clone of Arkanoid and I’m using UProjectile Movement Component to move the ball.

When the ball hit something it always bounces to the same direction. I’ve been checking UProjectileMovementComponent::ComputeBounceResult code to see if I can add some randomness to the velocity returned by the method, but I don’t think so.

For example, if the ball moves with (0, +y, +z), it will bounce to (0, +y, -z). I want to add some randomness to the z value. Something like (0, +y, -0.8z).

Is there is anything I can use it?

Maybe, I can use the method UProjectileMovementComponent::AddForce.

The fact you correctly call it Arkanoid instead of brick breaker made my day…

I wouldn’t use engine physics at all for this.

This is basically the simplest of programming algorithms in 2D space.
You need not know trig to generate a bounce. Nor anything else safe from basic geometry…

However - from having coded a couple of those. Introducing randomness to the bounce just aggravates the player.
There’s an expectation that the bounce is “correct” to the idea of newtonian space you get when playing.

As far as how.

Add velocity to the ball.
Check collision.
On hit event you take its direction vector and you invert it.

Now to introduce randomness to that, you break the vector, and alter the correct axis to be within a given range.

Because this 2d you only need to alter 1 axis to get the result you want - my guess is it’s the X axis.
The Y axis is probably constant throughout movement anyway.

You can pipe in a random between 0 and .025 to add to the vector, and test…
If it doesn’t annoy you enough you can bump up the value.

Kudos for doing this in cpp too.
(It’s way easier to make the math happen, but BP is more immediate for it).

Ps.
I’m forgetting something but it is super simple like invert. Just not directly invert.
You invert X when hitting vertical
And invert Z when hitting horizontal
How you detect that was just as simple though cannot recall.
In ue4 impact normal dot forward vector should get you there anyway.

Cough Breakout :stuck_out_tongue_winking_eye:

Thanks a lot for your answer but I want to know using the UProjectile Movement Component.

There’s no way to alter the pre-computed which will also fail on you over and over again - which is why it’s best not to use them.

you can try to do something similar logic wise. Add an Extra layer of collision which isn’t root.
Detect it’s impacts against stuff.
Add impulse in a custom direction.

On the projectile movement component, you have an OnBounce() event.
You can hook into this event, and add a random impulse to the projectile in the callback.

Where is that OnBounce() event? I cannot find it.

You assume that adding inpulse is “immediate”.
I would give it a much wider birth by adding a dedicated capsule who’s length increases with ball speed, and factually predicts the coming bounce.

That way there’s an actual chance that it’ll work regardless of frame count.

@ViaCognita
How can you ask “where is” if you are doing this in cpp?
You have to overwrite it.

The UProjectileMovementComponent has an event called
OnProjectileBounce

It’s true that an impulse can have a delay of one frame compared to the bounce.
However, the whole point of an “impulse” is that it imparts a direct change in velocity (and spin) based on affect point, direction, magnitude, and inertial tensor of the object. That’s the whole point of an “impulse” in physical simulation.

Depending on frame rate, speeds involved, size and prominence of projectile, and a host of other game specific factors, any delay in applying the impulse can be totally invisible, or could cause somewhat strange bounces. (But then, adding a random peturbation to the bounce will likely make it “strange” to they eye anyway!)

In my case, using “Set Velocity” node of projectile movement by adding a random vector to current velocity seems better than “Add Impulse”, since the later method requires turning “Simulate Physics” on and more coast. And Projectile Movement is not fully compatible with Simulate Physics. Consider use Set Velocity if only a tiny noise of bounce is wanted.

1 Like