Physics object velocity

For questions about using Classic.

Post » Tue Aug 30, 2011 8:09 pm

I have a feeling this is simple.

How do I get the velocity of a physics object based on the X & Y velocity?
Values should not be negative.

B
18
S
4
G
1
Posts: 413
Reputation: 2,512

Post » Tue Aug 30, 2011 8:46 pm

The abs() expression will remove the sign from a number (eg: the positive number only).

So the X and Y velocity of an object "Object" is:
abs(Object[Physics].VelocityX)

and

abs(Object[Physics].VelocityY)

However, if you'd like the speed in a single value, you can get that as

sqrt(Object[Physics].VelocityX ^ 2 + Object[Physics].VelocityY ^ 2)
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Tue Aug 30, 2011 8:57 pm

Thank you.
I acctually used
float(right(str(Sprite[Physics].Velocityy), len(str(Sprite[Physics].Velocityy))-1))
Or similar when value was lower than zero, to remove the minus sign. You helpred me a great deal.
B
18
S
4
G
1
Posts: 413
Reputation: 2,512

Post » Tue Aug 30, 2011 9:00 pm

[QUOTE=Urled] Thank you.
I acctually used
float(right(str(Sprite[Physics].Velocityy), len(str(Sprite[Physics].Velocityy))-1))
Or similar when value was lower than zero, to remove the minus sign. You helpred me a great deal.[/QUOTE]

No problem, if you ever need to do it and the abs() expression isn't there, you can also multiply it by -1 when it's below 0.
"Construct 4 lets YOU make advanced games! (maybe)" Construct Classic - Examples Kit
B
86
S
28
G
13
Posts: 2,092
Reputation: 15,009

Post » Wed Dec 28, 2011 10:35 am

Is there a way of getting the velocity inside a "for each object" select (So it gets the velocity of only 1 instance)?

It seems to always only get the velocity of one of the objects. I set a instance variable to velocityX+VelocityY. It's setting another instance variable correctly, so I know that's not the problem.
B
5
S
1
G
3
Posts: 9
Reputation: 1,108

Post » Wed Dec 28, 2011 12:31 pm

I also had that problem. Picking the UID is a solution, but the UID can change by deleting/adding objects. My method was the following:
I created a global variable(counter) and a private variable ID for the object. Now I add to the foreach action: add 1 to global('counter') and set object('ID') to global('counter').
So it creates the object, and increases the counter everytime when the sprite is created and assigns the number to the ID.

Short: every object has its own number in its private variable
You only have to put on the conditions: object('ID') = 2. If you want to make something with the 2nd object.

It was a little hard for me, to explain it in english. So if you don't understand anything, just reply.zyblade2011-12-28 12:32:37
B
24
S
9
G
2
Posts: 294
Reputation: 3,160


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest