• News

• Industries

• Learning & Support

• Community

• Marketplace

# Push VS Punch with a motion controller - What the math?

So essentially I want to be able to differentiate between a push and a punch. My thought was to compare the controller’s starting point vector with the vector of the controller’s travel for the duration of the punch with a fancy thing I discovered recently called a ‘dot product’, until that dot product goes less than zero (end of punch).

I’d like the attack to succeed or fail based on the speed of the punch. Is that just the amplitude of the dot product? Or should I track the controller’s motion along it’s punch vector from tick to tick(time scaled). The attack succeeding (push vs punch) only if the distance the controller traveled is far enough?

Any help is appreciated - Thanks!

ps I’m using BP

Well really what you are wanting to know is the speed of the controller.
So, I’d forget about the dot product (where is the ‘starting point’ for the punch anyway?), and just look at the per-tick vector magnitude, time scale it to turn it into a speed, and then calculate the short-term average of those speeds every tick.
Then compare the resulting average speed with a threshold to decide between punching or not punching.

Something you might want to use the dot-product for though would be to detect the difference between a glancing blow and a straight on one.
When calculating the average speed above, also calculate the short-term average direction vector of the motion.
Upon impact, also calculate the vector from the impact point to the centre of the object.
Normalise both of them, then calculate their dot product, and the result is what fraction of the hit was aimed toward the centre of the object.
If you do all of this using only X and Y, then you’ll get the component aimed towards the Z-axis of the object, rather than the exact centre, which might well be more useful if the basic shape of your targets are capsules rather than spheres.

After a hit is detected, I’d disable hit detection for a short period or short distance, otherwise double hits could easily happen as the controller passes through the mesh then almost immediately hits it again.

Hope that is of some help. I can’t provide any Unreal specific suggestions, because I’ve only just started using it.

Thanks for the response, that makes a lot more sense. I might adapt your glancing blows dot product use but I’ll have to get the first part up and running.

It’s not punching as in boxing. More along the lines of a spaceship deflector shield punch, once the user finishes the punch, a force field will accellerate from the end of punch increasing in size and velocity out into space deflecting an asteroid and saving the ship The dot product will still work but I’m going to have to test to see how much to accellerate and increase in size in order to make it playable. Once it’s hitting, glancing blows might work but I’m using physX so hopefully it will be already calculated.

Ah, I see. So the end of the ‘punch’ isn’t a collision with another mesh. To detect the ‘end’ of the punch, you’ll need to detect sudden deceleration. Might take a bit of experimenting to get the thresholds right, but I like the idea.

Velocity is probably what you are looking to check on id say.

+1 for velocity. A push is more of a slow / steady type of movement, where as a punch is more of a fast / brisk type of movement. If you think of terms of splines, push = linear, punch = bezier curve. See trig for the actual maths involved.