distance() to sprite edge?

0 favourites
  • 9 posts
From the Asset Store
On the edge
$20 USD
20% off
On the Edge is a puzzle game where you have 40 levels to have fun with.
  • 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?

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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?

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

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

    So, for instance:

    distance(tracers.HitX,tracers.HitY,badguy.X+badguy.width / 2,badguy.y+badguy.height / 2)
    Less than 16
    [/code:3sa24wrk]
    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?
  • 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:

    0---1
    |   |
    3---2[/code:p47hmjbr]
    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:
    [url=https://dl.dropboxusercontent.com/u/5426011/examples34/closest_dist.capx]https://dl.dropboxusercontent.com/u/542 ... _dist.capx[/url]
    
    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)
  • 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:

    0---1
    |   |
    3---2[/code:ohadkwkd]
    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:
    [url=https://dl.dropboxusercontent.com/u/5426011/examples34/closest_dist.capx]https://dl.dropboxusercontent.com/u/542 ... _dist.capx[/url]
    
    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?

  • Pick overlapping point system condition could help.

  • 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?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)