Is it possible to determine an objects speed?

For questions about using Classic.

Post » 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
G
3
Posts: 25
Reputation: 975

Post » 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
98
S
36
G
19
Posts: 1,584
Reputation: 17,717

Post » 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
G
3
Posts: 25
Reputation: 975

Post » 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
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » 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
98
S
36
G
19
Posts: 1,584
Reputation: 17,717

Post » 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
G
3
Posts: 25
Reputation: 975

Post » Tue Jul 26, 2011 12:58 am

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

ready!
B
45
S
9
G
10
Posts: 194
Reputation: 8,165


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 5 guests