# Physics Velocity

Get help using Construct 2

### » Wed Jul 03, 2013 12:45 am

Hi, I'm having a lot of trouble with this problem:

I have a spaceship and a variable called 'velocity threshold' - I want it so that if the ships velocity is past the threshold and the ship collides with a wall, the ship should blow up.

The ship can move in 360 degrees, like asteroids.

Any help is appreciated, thanks
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

### » Wed Jul 03, 2013 1:18 am

You can calculate the total velocity of an object with the physics behavior with:
sqrt(sprite.physics.VelocityX^2+sprite.physics.VelocityY^2)

Then compare that with your 'velocity threshold' with a 'on collison' trigger.
B
100
S
38
G
134
Posts: 5,555
Reputation: 85,323

### » Wed Jul 03, 2013 1:26 am

Another alternative (which I usually use) is distance(0,0,sprite.physics.velocityX,sprite.physics.velocityY)
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,739

### » Wed Jul 03, 2013 1:28 am

You can get the X and Y velocity using   ship.physics.velocityX/Y

so to find the overall velocity in any direction, is like trying to find the longest side of a triangle with pythagoras' theorem

Imagine 'a' is velocityX and 'b' is velocityY.. the overall velocity would be 'c'

Here's the full page

so to get the overall velocity in any direction, it's:
sqrt(ship.physics.velocityX^2,ship.physics.velocityY^2)

you might have to put each velocity value inside an abs( ) to avoid negative numbers but i'm not sure.

there is an easier way though..
The distance() expression uses pythagoras in the same way, so you can actually just use that..

distance(0,0,ship.physics.velocityX,ship.physics.velocityY)

if you do it this way, I just figured it'd be good to know why it works first

edit: double ninja'd, i aint even madkeepee2013-07-03 01:29:01
B
28
S
8
G
1
Posts: 469
Reputation: 4,683

### » Wed Jul 03, 2013 9:15 am

"edit: double ninja'd, i aint even mad"

LOL
B
53
S
16
G
9
Posts: 1,098
Reputation: 11,282

### » Wed Jul 03, 2013 9:35 am

Wow thanks, multiple solutions here - got it all working now and I've learnt some new stuff!
B
44
S
13
G
8
Posts: 622
Reputation: 7,417

### » Wed Jul 03, 2013 9:43 am

It was the distance(0,0,ship.physics.velocityX,ship.physics.velocityY) which worked.

Using sqrt(sprite.physics.VelocityX^2+sprite.physics.VelocityY^2)
almost worked except for 'head on' collisions which must have been negative values.
B
44
S
13
G
8
Posts: 622
Reputation: 7,417