Keep a sprite within defined range of another sprite

0 favourites
  • 3 posts
From the Asset Store
Minimal Sprite Font with Stroke for Pixel Art games.
  • Been thinking about how to approach this problem recently - and I have a couple of ideas but I'm hoping for something a little more elegant.

    I have a main "body" object and a smaller object which are part of the same container. I want to be able to freely move the small object around, but keep it constrained to the large "body" object within a defined circular radius. I have thought about using another sprite set to opacity 0 (and set as solid) as sort of a "wall" around the large object so the smaller object can't pass through - but that seems sloppy - and will cause issues with other objects colliding with the "wall".

    The second idea I had was to use physics and just hinge the objects together with an armature (think of like, a rope tied between to objects). I didn't really want to go this way either, because adding physics to the project is only replacing one problem I'm having with another. I'm trying to do this to optimise some stuff - so going with a (probably) more CPU intensive method isn't really going to help.

    Anyone got a better idea how to constrain the movement, maybe based on a distance calculation or something? The smaller object would need to have freedom of movement within the radius of the larger "body" but not be able to "escape" past a maximum distance in any given direction.

    I'm sure this has been asked before, but for the life of me I can't find any real references to the problem.

    Thanks in advance for any suggestions.

    ~Sol

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Here's one way:

    Basically you find the distance() and angle() between both objects, then if the distance is greater than the radius you can the set the second object's position to the first and use the "move at angle" action with the angle and radius as parameters. Or some variation of that.

    Here's one possible example in case the above capx doesn't do it as I recall.

    Global number dist=0

    Every tick

    --- set dist to distance(sprite1.x,sprite1.y,sprite2.x,sprite2.y)

    Dist>radius

    --- sprite2: move dist-radius pixels at 180+angle(sprite1.x,sprite1.y,sprite2.x,sprite2.y) degrees

  • Thanks a lot R0J0hound I will give this a try.

    I have managed a bit of a "hack" method using some distance comparisons - but I don't think it's ideal. I will try out what you've listed here. Cheers!

    ~Sol

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