# distance() to sprite edge?

Get help using Construct 2

### » Mon Jan 23, 2017 9:15 pm

I'm having trouble with distance(), it seems using .x/.y relies on the distance from the center of the object? Is there a way to measure the distance from any point on the outside of it's edge?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Mon Jan 23, 2017 9:25 pm

It's from the origin of the object that is usually centered in the image editor. You could always add an imagepoint and use Sprite.imagepointx(1) instead of sprite.x.
B
100
S
38
G
134
Posts: 5,555
Reputation: 85,323

### » Mon Jan 23, 2017 9:33 pm

R0J0hound wrote:It's from the origin of the object that is usually centered in the image editor. You could always add an imagepoint and use Sprite.imagepointx(1) instead of sprite.x.

Yes, the problem is that would only be for one point, I was hoping to use an expression that would be effectively the same as "if overlapping", and I could then have the distance as short as possible from the edge, rather than the centre. Is there a was of checking an overlap entirely in the expression editor?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Mon Jan 23, 2017 10:17 pm

Use the dimensions of the object: sprite.x + (sprite.width / 2)
B
34
S
11
G
3
Posts: 191
Reputation: 3,678

### » Mon Jan 23, 2017 10:35 pm

gameglaux wrote:Use the dimensions of the object: sprite.x + (sprite.width / 2)

So, for instance:
Code: Select all
Less than 16

I can't get it to work evenly, I mean, that 16 could be, say, less than 2 to test for an overlap, couldn't it?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Tue Jan 24, 2017 12:31 am

The distance() expression only gives you the distance between two points. To get the closest distance between a point and a shape it will be more involved. Take the example of a square shape, you'll need to get the distance from all the corners and all the edges and pick the lowest value.

Basically you'd setup the imagepoints on your object like this:
Code: Select all
0---1
|   |
3---2

Where 0 is the origin.
Then to calculate the distances you'd use the distance() expression to get the distance to a point, and for the distance to a line you'd use a vector dot product to see if the the projected point is on the line, then a vector cross product is used to get the distance.

Example here:
https://dl.dropboxusercontent.com/u/542 ... _dist.capx

Now if that is too much math you can go for an approximate solution: Put a lot of instances all around the shape you want to get the closest distance to. It should look like dots going around the object's edge, the closer together the better. Then you can then do an event like this:

dotSprite: pick closest to (mouse.x, mouse.y)
--- set text to distance(dotSprite.x, dotSprite.y, mouse.x, mouse.y)
B
100
S
38
G
134
Posts: 5,555
Reputation: 85,323

### » Tue Jan 24, 2017 12:41 pm

R0J0hound wrote:The distance() expression only gives you the distance between two points. To get the closest distance between a point and a shape it will be more involved. Take the example of a square shape, you'll need to get the distance from all the corners and all the edges and pick the lowest value.

Basically you'd setup the imagepoints on your object like this:
Code: Select all
0---1
|   |
3---2

Where 0 is the origin.
Then to calculate the distances you'd use the distance() expression to get the distance to a point, and for the distance to a line you'd use a vector dot product to see if the the projected point is on the line, then a vector cross product is used to get the distance.

Example here:
https://dl.dropboxusercontent.com/u/542 ... _dist.capx

Now if that is too much math you can go for an approximate solution: Put a lot of instances all around the shape you want to get the closest distance to. It should look like dots going around the object's edge, the closer together the better. Then you can then do an event like this:

dotSprite: pick closest to (mouse.x, mouse.y)
--- set text to distance(dotSprite.x, dotSprite.y, mouse.x, mouse.y)

I see, so maybe distance isn't so much what I'm looking for? What I need really is an expression that tests if a points x/y is overlapping an instance. i know objects can be testing if they're overlapping, but could a point be tested with system expressions?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286

### » Tue Jan 24, 2017 1:06 pm

Pick overlapping point system condition could help.
Learning other engines, but C2 still rocks.
B
80
S
22
G
95
Posts: 2,417
Reputation: 57,671

### » Tue Jan 24, 2017 2:49 pm

Colludium wrote:Pick overlapping point system condition could help.

I feel so stupid, thanks for pointing that one out, I've even seen it and didn't think to try it!

That seems to work to check for an overlap within the shape at any point. Or is there a more efficient way I can test for this using one event?
B
41
S
12
G
14
Posts: 1,124
Reputation: 11,286