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.
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).
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.
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!)