distance() to sprite edge?

Get help using Construct 2

Post » 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,120
Reputation: 11,259

Post » 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
92
S
32
G
110
Posts: 5,295
Reputation: 71,501

Post » 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,120
Reputation: 11,259

Post » Mon Jan 23, 2017 10:17 pm

Use the dimensions of the object: sprite.x + (sprite.width / 2)
Image
B
30
S
9
G
3
Posts: 171
Reputation: 3,329

Post » 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
distance(tracers.HitX,tracers.HitY,badguy.X+badguy.width / 2,badguy.y+badguy.height / 2)
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,120
Reputation: 11,259

Post » 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
92
S
32
G
110
Posts: 5,295
Reputation: 71,501

Post » 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,120
Reputation: 11,259

Post » Tue Jan 24, 2017 1:06 pm

Pick overlapping point system condition could help.
A big fan of JavaScript.
B
74
S
20
G
71
Posts: 2,230
Reputation: 44,892

Post » 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! :?
Image
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,120
Reputation: 11,259


Return to How do I....?

Who is online

Users browsing this forum: Armench, Jackriduk, kiki4construct and 29 guests