Keep a sprite within defined range of another sprite

Get help using Construct 2

Post » Tue Jul 05, 2016 3:23 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
83
S
34
G
40
Posts: 3,032
Reputation: 30,474

Post » Tue Jul 05, 2016 5:08 am

Here's one way:
viewtopic.php?f=147&t=69754&p=590475&hilit=limit+circle#p590475

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
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Tue Jul 05, 2016 5:29 am

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
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
83
S
34
G
40
Posts: 3,032
Reputation: 30,474


Return to How do I....?

Who is online

Users browsing this forum: pmerineau, Yarfapet and 12 guests