Is it possible to determine an objects speed?

This forum is currently in read-only mode.
From the Asset Store
Have you ever dreamed of playing in a Speedball game or tournament?
  • Here is my problem. Lets say I have a spaceship and it is falling to the ground. What I want to calculate is how many pixels it is dropping per second. Is this going to require some fancy formula or have I over looked some function in construct that will do this for me. Just to be clear i would want the speed (in pixels per sec) to be continualy updated so that the value would change if the speed of the ship changed. Obviously I then could use this data so that if the ship landed too fast the ship would break etc. Any help greatfully recieved!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Are you using any behaviors? Those usually have an expression to get speed. If you're using something custom, I know some formulas for finding the speed, but I'd need to know how you have your movement system setup first.

  • yes I am using the physics behavior and the the spaceship is falling due to the gravity setting.

  • <img src="">

    when you go to type numbers into the boxes for anything, like where you would set a private variable or check a value (like where the red arrow is pointing), you can double click an object's icon to get a list of expressions.

    If your object is using physics, there are Get Velocity X, and Y expressions. Although they aren't in pixels per second, it's physics's internal scale of speed.

    if you need to combine these two into one speed, it would be

    if the answer must be in pixels per second, then you would have to make a LastX and Last Y private variable
    -----Set LastX to sprite.X;
    -----Set LastY to sprite.Y;[/code:ayxlwcfm]
  • -Always
    -----Set LastX to sprite.X;
    -----Set LastY to sprite.Y;[/code:3vqjap7g]

    Just thought I'd mention that this expression will only retrieve the distance that the object moved in the last frame. You need to divide the distance by TimeDelta to get the actual pixels-per-second value. I'd also suggest putting round() around the expression so you don't have a jumpy floating point value:

    - PixelsPerSecond = round(distance(sprite.x,sprite.y,sprite('LastX'),sprite('LastY')) / TimeDelta)
    	[li]Set LastX to sprite.X[/li]
    	[li]Set LastY to sprite.Y[/code:3vqjap7g][/li]
  • Excellent, thank you linkman and lucid, after looking up how to measure speed I had guessed that I had to store a varible an then compare it with the x or y value at the next tick (or what ever it is called) but my attempts to put that into code was laughable. Anyway thanks again!

  • very easy


    sprite: Set animation speed to round(sprite[platform].speed)


Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)