playMaker

Author Topic: Get accurate speed of a game object  (Read 185 times)

MattBradley

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Get accurate speed of a game object
« on: January 09, 2020, 02:07:38 PM »
Hi,

As the title suggests, I'm trying to get the speed of a game object using Playmaker. I have tried various methods with different degrees of success, but I haven't been able to get a really stable speed value.

I understand the basic principle to determine speed is to get distance traveled and divide it by the time it took.

So I figure that a single state where I get the 'start position' of my object subtract it from the 'end position' to get the distance, then divide it by time.deltaTime to get the speed:


This works in that it gives a value that appears to match the speed of the object, however the value is not steady. Even though the object is moving the same distance each frame the deltaTime is variable and so the value constantly fluctuates a few units up and down.

How can I get a more accurate speed value?

Thanks!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4651
    • View Profile
    • jinxtergames
Re: Get accurate speed of a game object
« Reply #1 on: January 10, 2020, 08:50:47 AM »
Hi.
Maybe 'Get Speed' or 'Get velocity' can help here?

There is also a action called Get 'Angular Veolcity' on the Ecosystem


MattBradley

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Get accurate speed of a game object
« Reply #2 on: January 12, 2020, 04:30:42 AM »
Hey djaydino, thanks for the reply!

One important thing to mention is that this object doesn't have a rigidbody because I'm doing some weird gravity and alignment stuff with it, so physics actions don't work in this instance.

I tried the 'Get Game Object Speed' action on the Ecosystem with the same results. I guess I could filter or smooth the result, but it's weird how the value fluctuates so much,

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 1037
    • View Profile
    • Fat Pug Studio
Re: Get accurate speed of a game object
« Reply #3 on: January 12, 2020, 05:09:03 AM »
You don't need to use physics actions, subtract the last frame position from current position and there's your speed. If the value is negative, multiply it by -1 so you get te positive value if you need it in that format.

MattBradley

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Get accurate speed of a game object
« Reply #4 on: January 12, 2020, 05:31:25 AM »
Hey krmko, thanks for the help! :)

I just tried that but I get a value of 0 for the difference between positions over a single frame. Maybe my object isn't moving fast enough, or the values from 'Get Position' aren't so precise?

krmko

  • Beta Group
  • Hero Member
  • *
  • Posts: 1037
    • View Profile
    • Fat Pug Studio
Re: Get accurate speed of a game object
« Reply #5 on: January 12, 2020, 03:49:25 PM »
Probably both last and current frame positions are being taken in the same frame, try adding next frame event in between.

MattBradley

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Get accurate speed of a game object
« Reply #6 on: January 13, 2020, 01:57:02 AM »
Of course, I tried this too and had the same result.

As a temporary solution I am using the translation I apply to the object to derive the distance per frame (which is very steady, it only fluctuates .00001 every few frames) and am using that distance as my 'speed'. I found that if I multiply it by 60 I get the actual speed the object is travelling at, but I'm not sure why that works. Only thing I can think is I'm converting from/to seconds/mins?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14832
  • Official Playmaker Support
    • View Profile
Re: Get accurate speed of a game object
« Reply #7 on: January 14, 2020, 12:43:13 AM »
Hi,

 Something is fishy here, this is a straight forward action and you should get the speed of your moving object. what space are you getting the speed on? can you switch between self and world and see if it makes a difference?

Bye,

 Jean

MattBradley

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
Re: Get accurate speed of a game object
« Reply #8 on: January 16, 2020, 12:17:59 PM »
Hey Jean! :)

Yea something isn't quite right. I tried local and world and both give the same result.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14832
  • Official Playmaker Support
    • View Profile
Re: Get accurate speed of a game object
« Reply #9 on: January 17, 2020, 01:19:54 AM »
Hi,

 are you moving that object on late or fixed update?

 can you isolate the issue on a scene? like if a move a cube  to a given and then check its speed using that action, does it matches?

Bye,

 Jean