playMaker

Author Topic: How to Caclulate speed of object with and without rigid body?[SOLVED]  (Read 5784 times)

Rbanninga

  • Playmaker Newbie
  • *
  • Posts: 17
Good day everyone,

I've just started getting into Playmakers and it has been a great so far. I've run into a snag though which I am sure is super simple but somehow eludes me with how to transfer the logic into Playmaker. How can I calculate the speed of an object with and without rigid body?

I've tried Get Speed on an object with a rigid body. I then saved the value into a float and converted it into a string so I could output it to the GUI. At first it would display a number that was constantly increasing in value... regardless of me moving the object around in-game. I then went and disabled gravity on the object inside the rigid body component and then I stopped getting any value except 0.0 even when moving. So that is one question... what am I doing wrong...

And then the second approach I would like to attempt is one that does not need to trigger the physics system of Unity at all. I would like to compare the distance of an object in relation to its previous location every frame. I've read that its one good way to go about it.

Below is a piece of code I found that should do the trick I think. But I am wondering how best to transfer that into a playmaker FSM using the math functions.

float speed = (transform.position - this.mLastPosition).magnitude / elapsedTime;
this.mLastPosition = transform.position;

Now if I read this right it takes one position and subtracts it from a previous one? But how can you specify that each one is either the first or second frame? What is a good way to capture the magnitude and then divide it by time that has passed?

Apologies is this comes across as pure gibberish. Your help would be most appreciated. THANKS!
« Last Edit: October 08, 2013, 02:42:49 AM by jeanfabre »

Bjakuja

  • Junior Playmaker
  • **
  • Posts: 75
Re: How to Caclulate speed of object with and without rigid body?
« Reply #1 on: October 06, 2013, 04:33:04 PM »
Hi!
If you want to get distance between objects you can use Get Distance action check every frame and store into float variable if you want constant distance.
Other thing you can do for example if your character or object falling you can use collision event and store force (speed stored into float variable) when hitting ground or other object.

Or if you want something specific to do you can cast a ray towards desired object position (you can get it with Get Position action / optional every frame) and get it's distance. Note that you can specify how often to cast a ray, every frame, every second frame, third..... in Raycast.

I hope this helps ;)
Cheers!

Rbanninga

  • Playmaker Newbie
  • *
  • Posts: 17
Re: How to Caclulate speed of object with and without rigid body?
« Reply #2 on: October 07, 2013, 01:23:45 AM »
Hello Bjakuja,

Those are all interesting approaches for various other situations and I will have fun trying those out at one point for sure. But the main thing I want to do is measure the speed of an entity as it travels through the world. I've found one approach yesterday using delta time to sample the passage of time and use that to sample the coordinates as time passes. I then compare them with each other to figure out the distance traveled during a single delta frame.

One of my programmer friends informed me that I could also take the Magnitude value and multiply that with the delta time to get the distance... I need to try that one still.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to Caclulate speed of object with and without rigid body?
« Reply #3 on: October 07, 2013, 03:12:37 AM »
Hi,

 there is an action called "GetVelocity"

and you can also use this for non physics.

http://hutonggames.com/playmakerforum/index.php?topic=1182.msg4961#msg4961

bye,

 Jean

Rbanninga

  • Playmaker Newbie
  • *
  • Posts: 17
Re: How to Caclulate speed of object with and without rigid body?
« Reply #4 on: October 07, 2013, 12:12:46 PM »
Hello JeanFabre,

Thanks for the script! It works nicely. It also showed me how to go about it without using a script though. Oddly enough for anyone interesting if you want to know the speed of an object then make sure to Store Magnitude from the Get Axis Vector action. Then multiply it with the Speed value / Variable you set inside the Controller Simple Move action.



Now if you want this in Kilometers per hour you then take the resulting number and multiply it by 3.6.

So 3 meters per second travel speed equates to 10.8 Km per hour.

Or 3 x 60 seconds x 60 minutes divided by 1000 meters if you want to know how I got to the 3.6 value. :)

Thanks again for the help everyone!
« Last Edit: October 07, 2013, 12:31:03 PM by Rbanninga »