How do two objects always keep the same distance from each other?

Asked

Viewed 280 times

2

Both objects move parallel through the map.

I tried to use the Lerp function, but I can’t make it work. How to use this function correctly?

1 answer

1

As the two objects move parallel and always the same distance, I believe that the simplest solution is to directly challenge the position of second object according to the position of the first, plus a distance of offset.

Here is a simplified example:

The object sp1 follows the mouse coordinates (at each event tick):

Set position to (Mouse.X, Mouse.Y)

In the same event, you add a action by changing the position of object sp2 for a fixed distance from the position of sp1 (in this example, 150 units on the axle X):

Set position to (sp1.X + 150, sp1.Y)

The Event sheet gets like this:

Imagem do event sheet com as funções implementadas


The function lerp calculates a linear interpolation between 2 points, and can be used, for example, to smooth the movement of an object:

lerp(a, b, x)

For the above function call, it returns the value of a + x*(b-a).

In the example, if you replace the command in the action of the object sp2 for:

Set position to (lerp(sp1.X, sp1.X + 300, 0.5), sp1.Y)

You will get exactly the same effect, however, with a higher processing cost.

  • Hello. Thank you for answering. Ah, another thing: if one of the objects increases in size over time, as I would do so that the distance between them still remains the same always?

  • @Deividsgomes: Hi! Yesterday, I answered a similar question: Some kind of collision would apply in this case?. I believe I can help :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.