Page 1 of 1

How to make "sticky actor"

PostPosted: Tue Aug 05, 2014 4:32 pm
by xenidius93
I want to make actor,which doesn't detach from the second actor, but how to do this?

Re: How to make "sticky actor"

PostPosted: Tue Aug 05, 2014 7:41 pm
by skydereign
Do you want the red sticky actor to stick only to the one gray actor? Or is the sticky actor supposed to stick to many different actors as well? If you just want it to stick to a single gray actor at a time, what you need to do is store a reference to the gray actor you want it to stick to. From there you can prevent the sticky one from ever going beyond a certain distance away from the gray one.
Code: Select all
// this assumes there is only one gray actor for simplicity
int radius = (width + height) / 4; // using an average of height and width for simplicity
int gray_radius = (gray.width + gray.height) / 4;
int max_dist = radius + gray_radius;

if(distance(x, y, gray.x, gray.y) > max_dist))
{
    float ang = direction(gray.x, gray.y, x, y);
    x = gray.x + cos(ang)*max_dist;
    y = gray.y - sin(ang)*max_dist;
}

This is just the idea presented in a simplified form. Since you are dealing with squares you should probably set x and y independently and not use radius. Also the above example would only work with one gray actor, so you would need to use some way of tracking which gray actor a given sticky one is connected to.

Re: How to make "sticky actor"

PostPosted: Wed Aug 06, 2014 3:00 am
by Zivouhr
That sounds good Skydereign.

Also, for an actor that follows the lead actor closely, like the body of a snake following the snake's face, the mouse demo where the stars follow the mouse movement might give some tips. Parenting it wouldn't give it any slack but whenever it reached inside the lead actor's Region Zone, it could stick by parenting it to the lead, and falling off if it hits something else, which my first thought about having it stick to the main character. Just some other ideas to think of another possible solution.

Re: How to make "sticky actor"

PostPosted: Wed Aug 06, 2014 8:45 am
by xenidius93
Hmm, I want to use it, to make a chain from actors "Ring" (red squares at second picture). So I think, that this is not that simple :/.