Is it possible to have a sprite snap to the Collision Polygon of another sprite?

0 favourites
  • 3 posts
From the Asset Store
Grid - snap to grid
$1 USD
80% off
Snap to visible grid - perfect solution for any game genre
  • so, what im trying to make is an interactive floor plan designer for the game ARK. in the game you can either use square foundations or triangle foundations, and i want to make a small program that will allow me to quickly put these two simple objects together in a 2d "map" so i can quickly design floor plans for bases. although im not quite sure how to do this, ive managed to get a drag and drop behaviour that allows the square foundation tiles to snap to a 64 pixel grid. but for the triangles, i want them to snap to the sides of the square, not to the grid. how would i do this?

    Example:

    here is an example of an ARK floor plan,

    it is made up of lots of squares and triangles, i want my program to be able to quickly plan out an image similar to this.

    how my program currently works:

    Squares (working):

    Triangles (not working):

    All i need to do is make it so that the side of the triangle can snap to the side of the square, and then also allow for say another triangle to snap onto that square so on and so forth.

    example of how i would like it to work:

  • If there are only fixed snap locations without other shapes or sizes, I would try using invisible helper sprites to snap to. You'll need two sets, one for triangles and one for squares, since the distance to the center of each is different. Set the helper sprite angle to the angle between itself and the object that created it (rounded).

    So placing a square would create 8 helper sprites, one for each square and triangle at each snap location. 6 helpers would be created for triangles. When placing a new shape, check only the correct helper type to snap to, and if it's close enough to snap then set the angle of the new shape based on the angle of the helper sprite it snapped to.

    Destroy any helpers that are already overlapping a shape.

    Edit: make sure your "connecting" flat edge is facing towards the right (0 degrees) for the triangle sprite

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This isn't a full solution, but it does show how to position a square or triangle to another square or triangle. I did in in a random way for testing, so it's not interactive like you're after yet.

    All the sides of the shapes need to be the same length. So for the triangle it's height= sqrt(3)/2*width. Except I rotated it to point right to make some of the math easier.

    Also the sprite's origins need to be in the center of the shapes. The triangle's should be (width/3, height/2)

    The angle from the center of the shape to the sides would be

    sprite.angle+(0,90,180 or 270) for the square

    sprite.angle+(60,180 or 300) for the triangle

    and the distance from the center of the shape to the edge is width/2 for the square and sqrt(3)/6*height for the triangle.

    To position shape B onto an edge of shape A you'd

    1. position B onto A

    2. set B's angle to an angle to to edge of A

    3. move B forward by A's distance to center

    4. move B forward by B's distance to center

    The final step is to see if the new shape actually fits. We do that with an overlap check between the new object and all the existing ones. In the capx I'm purposely spacing things since objects in contact count as overlapping but that gap can be removed once the new shape is found to fit.

    dropbox.com/s/eybebzudn59vns6/box_tri.capx

    To make it interactive you'd want to do basically oosyrag's idea. Loop over all the shapes, and create new ones at every edge. They stay marked as new and are only added when clicked on, which marks that as new and deletes all the other new ones.

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