# How do I solve this math calculation?

0 Favourites
• 5 posts
• Hey everybody,

I have a bit different problem. I made an experiment for my upcoming game. I wanted to create chain between 2 objects. Also, I wanted that the amount of chains is adjustable AND chains are divided in equal distance between two objects. I hope that makes sense.

Here is a screenshot what I'm talking about:

Problem is, I don't understand how the math works in the event. Odd, ehh? I can create something but I don't understand it.

Here is a screenshot from my code:

I'm referencing to line 3 and precisely this: chain: set lerpNB to ---> (1 / StartSprite.chainAmount) * loopindex

Here is capx -file if somebody want to check the code in Construct2.

I think everybody knows that first you need to understand the problem, before you can solve the problem. I can understand both but I can't understand why results works. In particular in this case.

So, if somebody could explain to me why my code (line 3) is working, I would really appreciate it.

• That line just makes a faction for each chain. So if there are three links then the fractions would be:

``````A-------------B
^    ^    ^
1/3  2/3  3/3[/code:33u2zxe7]
Then that fraction is used later with the lerp to position the links.

I'd just do this instead to do the chains.  In a way it's more straightforward.
[code:33u2zxe7]every tick
--- destroy chain

for each startSprite
endsprite: id = startsprite.id
repeat startsprite.chainAmount times
--- create chain at (0,0)
--- chain: set x to lerp(startsprite.x, endsprite.x, loopindex/(startsprite.chainAmount+1))
--- chain: set y to lerp(startsprite.y, endsprite.y, loopindex/(startsprite.chainAmount+1))
--- chain: set angle to angle(startsprite.x, startsprite.y, endsprite.x, endsprite.y)[/code:33u2zxe7]``````

Yes, your method is much more simpler and easier to follow. I just learned something new, again ^^

I found a bit difference how the chain pieces are positioned between startsprite and endsprite comparing to my method and your method. Here are the screenshots:

my method

I was wondering, what is causing this? Maybe something to do with the --- loopindex/(startsprite.chainAmount + 1)) ---part?

• Yeah, I forgot to test it.

I meant to use:

(loopindex+1)/(startsprite.chainAmount + 1)

The idea was to only have links between and not on the ends.

You can make it just like yours if you do:

loopindex/startsprite.chainAmount

• ## Try Construct 3

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

Construct 3 users don't see these ads
• Nice! That was also one of the points to have in the chain creation but I forgot to wrote about it. So your method is actually what I was looking for ^^ Thank you so much for your help!