# How do I return -1 or 1 by comparing positions

Get help using Construct 2

### » Wed Sep 30, 2015 1:39 pm

Hi everyone,

This sounds simple enough but I can't find a way to get a -1/1 by comparing positions.

For example, there's a player object, and when it collides with something I want to compare their X positions so if the object.x is > than the player.x, I get a 1 and if it's not I get a -1.

Any idea how to achieve that? It's probably something simple but I'm stumped!

Thanks!
B
129
S
44
G
41
Posts: 857
Reputation: 28,485

### » Wed Sep 30, 2015 1:45 pm

round( X / abs(X)) will give you -1/+1.
B
71
S
22
G
279
Posts: 3,838
Reputation: 153,873

### » Wed Sep 30, 2015 1:46 pm

blackhornet wrote:round( X / abs(X)) will give you -1/+1.

Nice, exactly what I needed! Thanks!
B
129
S
44
G
41
Posts: 857
Reputation: 28,485

### » Wed Sep 30, 2015 2:19 pm

@blackhornet
Err after some testing I still can't get what I want. Sorry, I'm really bad at math!

This function allows me to get a -1/1 position based on the 0,0 in the canvas. I've been tweaking it to get a -1/1 position based on two objects, but I don't see how...

Edit: Nevermind, got it. I swear I tried it before but it didn't work, tried it again now and it did...

I used round((player.X-door.x)/abs(player.x-door.X))
B
129
S
44
G
41
Posts: 857
Reputation: 28,485

### » Wed Sep 30, 2015 2:25 pm

X=object.x - player.x
round( X / abs(X))
B
71
S
22
G
279
Posts: 3,838
Reputation: 153,873

### » Wed Sep 30, 2015 2:25 pm

Conditional expression(under operators).

Code: Select all
`object.X > player.X ? 1 : -1`
Moderator
B
114
S
41
G
20
Posts: 1,586
Reputation: 19,161

### » Wed Sep 30, 2015 2:30 pm

Code: Select all
`object.X > player.X ? 1 : -1`

Oh wow that's handy. Thank you!
B
129
S
44
G
41
Posts: 857
Reputation: 28,485