# Is it possible to determine an objects speed?

### » Mon Aug 31, 2009 11:55 pm

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!
B
4
S
1
G
3
Posts: 25
Reputation: 1,125

### » Tue Sep 01, 2009 12:15 am

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.
Moderator
B
115
S
41
G
20
Posts: 1,586
Reputation: 19,196

### » Tue Sep 01, 2009 8:45 am

yes I am using the physics behavior and the the spaceship is falling due to the gravity setting.
B
4
S
1
G
3
Posts: 25
Reputation: 1,125

### » Tue Sep 01, 2009 12:30 pm

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 [code:ayxlwcfm]sqrt(Sprite[Physics].VelocityX^2+Sprite[Physics].VelocityY^2)[/code:ayxlwcfm]

if the answer must be in pixels per second, then you would have to make a LastX and Last Y private variable
and
[code:ayxlwcfm]-Always
-----PixelsPerSecond=distance(sprite.x,sprite.y,sprite('LastX'),sprite('LastY'))
-----Set LastX to sprite.X;
-----Set LastY to sprite.Y;[/code:ayxlwcfm]
Spriter Dev
B
103
S
22
G
12
Posts: 3,285
Reputation: 17,151

### » Tue Sep 01, 2009 5:14 pm

[quote="lucid":3vqjap7g][code:3vqjap7g]-Always
-----PixelsPerSecond=distance(sprite.x,sprite.y,sprite('LastX'),sprite('LastY'))
-----Set LastX to sprite.X;
-----Set LastY to sprite.Y;[/code:3vqjap7g][/quote: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:

[code:3vqjap7g]- PixelsPerSecond = round(distance(sprite.x,sprite.y,sprite('LastX'),sprite('LastY')) / TimeDelta)
- Set LastX to sprite.X
- Set LastY to sprite.Y[/code:3vqjap7g]
Moderator
B
115
S
41
G
20
Posts: 1,586
Reputation: 19,196

### » Wed Sep 02, 2009 9:16 am

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!
B
4
S
1
G
3
Posts: 25
Reputation: 1,125

### » Tue Jul 26, 2011 12:58 am

very easy
+always
sprite: Set animation speed to round(sprite[platform].speed)

B
48
S
10
G
10
Posts: 200
Reputation: 8,322