How to Calculate Object Velocity in Cinema 4D

» »

Sometimes you need to get the velocity of an object in your simulations. This is an example of how to get the velocity in Cinema 4D using XPresso.

In my case, I have a project where a car is steering automatically to chase a target. When I was trying it out, I realized that once the car reaches a certain speed it starts "wobbling", because at that speed even the slightest turn of the wheels results in the car changing direction more than it wants to.

This problem actually made me stop working on the project for quite some time, because it was a real show stopper — as soon as my auto steering car got up to "action" speed the scene became useless.

Then yesterday when I was tucking my youngest daughter to sleep, it came to me. I needed to get the velocity of the object in the scene and calculate the velocities of axes Z and X. This way I could get an indicator for when the car was going too fast, and do something about it.

This is how I solved it!

Object Velocity using XPresso

Once I got to a computer and tested this out, I realized that every object in XPresso actually has an output for this specific cause — the Position Velocity.

1. Position Velocity in XPresso

The Position Velocity node outputs a vector of the X, Y and Z velocities in units per second, and by adding a Result node after the output I could see the numbers rising and falling as my car was going through my scene. Now what?

First off, the vector information needs to be turned into numbers. This is done with the Vector2Reals node, which takes a vector and outputs X, Y and Z as real numbers.

New problem! As my car was going across the X axis the number for the X velocity turned negative, which means my approach with adding those numbers together wouldn't work. Luckily, the node Absolute does exactly what I wanted — it doesn't care if the number is negative or positive, it just gives you the number itself.

I then added the absolute units of X and Z together, which gave me a number to analyze. This little fella showed me that my car's velocity goes from 0 to approaximately 900 when at full speed.

Tip of the day: Activate "Animation Refresh" in the XPresso settings to view real-time updated values in your XPresso layout. The option can be found in the XPresso window's menu; just tick "Calculate" > "Animation Refresh".

2. Range Mapper Node

In order to make some use of the 0-900 number I now had, I turned to the Range Mapper node. This is a fantastic node, that calculates exactly what you want every time. It's like magic, honestly. Now, we had a number of 0-900 and we want the wheels to turn less and less as we reach 900.

As the "Input Lower" I entered 400, since the car is allowed to turn as much as it wants before we get up to higher speed, and as the "Input Upper" I entered 800 since it has to be stabilized earlier to be stable enough. So waht about the outputs of the Range Mapper?

If the speed is at 400 or lower, the turning angle should be left as is — the car is allowed to turn freely. When the car is at 800, I want the steering angle to be half of what it is at 400. So, I set the "Output Lower" to 1" as we're leaving it intact, and the "Output Upper" to 0.5*, half of the original.

3. Multiply the Steering Angle

Now we're at the finished line! All we have to do now is to add a Multiply node, to which we connect the Steering Angle and the Range Mapper output. This gives us "Steering Angle x 1" when at 400 or lower, and "Steering Angle x 0.5" when at 800 or higher.

Anything in between is interpolated as you would expect — for instance, 600 will make the multiplier be 0.75.

Celebrate

That is actually it — we are done! Quite simple, once you get your head around it. I hope this helped you somehow, as it surely helped me a lot.

Now I can finally finish my beloved car project!