How to know the angle of collision?

I have a sphere in the world. I want the sphere to move at a constant speed. I want the sphere to change direction when it collides.

The sphere has no mass, no friction, and no gravity.

I need to know the angle of collision to change the direction of the sphere.

The following is my code to calculate it.

tick

The following part is incomplete:

How can I get the collision angle between the two objects? Or some other easier way to achieve this purpose?

Thank You So Much!!

tl;dr:

How can I get the collision angle?

use Dot Product → ACOSd. It’d be something along the lines of:

ACOSd (hit SurfaceNormal DOT NormalisedVelocity) * 2

This should give you a 0-180 range in degrees.


Is there a reason why you’re doing all this rather than use actual physics?


I have a sphere in the world. I want the sphere to move at a constant speed. I want the sphere to change direction when it collides.

IF that’s what’s needed, the above seems overengineered. Since you want to take advantage of physics, rely on physics then. Full energy conservation can be achieved by configuring a physical material, overriding restitution and friction. Also, removing mass from a physically simulated body is asking for trouble, imho.

You’d need no script for the above and no special settings apart from removing drag (you’ve done this already) and creating a physical material.


If you absolutely must override physics (which can manifest as a bunch of odd issues further down the line), consider working with direction, not angle:

Alternatively, you could use mirrored velocity from the frame before the hit and skip Direction * Magnitude.

Converting to angles often leads to pigeonholing oneself into a corner, especially in 3D. On top of that, relying on generated physical hits will be awkward, as above. You might be better off using a floating movement component and emulate it instead with movement direction. Setting linear velocity per frame seems wasteful and defeats the purpose using physics in the first place.

Not sure what the real end goal is so I might be off my mark here.

1 Like

This has no script. Is this what you need?

I’m printing velocity every frame in the middle. If the velocity changed due to a massive framerate drop or inadequate float precision (or UE4 + PhysX shenanigans), you could always clamp the vector to ensure it stays as it should and enforce linear velocity then.

Hi @Everynone

I was thinking if i don’t use mass, friction and gravity i would get constant speed because there is not acceleration. But I didn’t know that this would cause problems in the physics simulation. I’m a noob!!

Ok, i won’t use the event OnTick to do this.

At the moment I am shooting projectiles at the sphere. That’s why I don’t want it to stop moving. But I also don’t want it to increase or decrease its speed. Because if it’s too slow it’s boring, and if it’s too fast then it’s very hard to hit. And another problem is when I shoot at it in the same direction of movement, sometimes it speeds up too much and goes through walls.

Ok, Now i seeing variations in the speed of the sphere. Sometimes it goes faster and sometimes it goes slower. I will try it.

Thank you so much for help!! very appreciate!!

1 Like

Also, have a look at this thread, there’s a method with pawn movement and projectile movement:

And, finally, this:

This will maintain velocity in an uncanny fashion by applying negative dampening which actually speeds things up.

Thank you so much!!