How do I prevent overlap with self ( instance ) and other objects when moving?

Not favoritedFavorited Favorited 0 favourites
  • 4 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hi,

    kindly Check Attached Video

    Subscribe to Construct videos now

    I'm trying to move an object on a grid using touch input. The object should snap to the nearest grid cell, which works fine. However, I want to prevent the object from moving if the target cell is already occupied — either by itself or by another object (like an obstacle box).

  • Why not use the Tile movement behavior?

    Alternately, you can use Is overlapping at offset.

  • Actually, my movement is real-time — the object follows the touch and snaps to grid positions as the finger moves.

    Can Tile Movement work in this kind of setup, or is it only for step-by-step key input?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Instead just setting the position you’ll want to do it in steps.

    One idea is to take the angle from the object to the touch position, and then simulate a control if it’s close to that angle. For example:

    Angle angle(object.x,object.y,touch.x,touch.y) is within 45 degrees of 0
    — object: tileMovement: simulate control right

    You’d just do that for 0, 90, 180 and 270 degrees. And you’d depend on the solid behavior to keep from moving over other things.

    Another very visual way to do it is to use some sprites to use as detectors, and position them one grid space in all four directions around the object as well as on the object itself. Then you’d just pick the closest object around the player that wasn’t overlapping something to move to.

    global uid=-1
    On object touch
    — set uid to object.uid
    
    Pick object by uid uid
    — det: destroy
    — create det at object.x, object.y
    — create det at object.x+32, object.y
    — create det at object.x-32, object.y
    — create det at object.x, object.y+32
    — create det at object.x, object.y-32
    
    Det overlaps wall
    — det: destroy
    
    [inverted] pick object by uid uid
    Det overlaps object
    — det: destroy
    
    Pick object by uid uid
    Det: pick closest to touch.x, touch.y
    — object: set position to det.x, det.y

    Just some ideas.

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