# How do I solve this math calculation?

Get help using Construct 2

### » Thu Feb 04, 2016 9:49 am

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.
B
16
S
2
Posts: 60
Reputation: 989

### » Thu Feb 04, 2016 6:55 pm

That line just makes a faction for each chain. So if there are three links then the fractions would be:
Code: Select all
`A-------------B    ^    ^    ^   1/3  2/3  3/3`

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: Select all
`every tick--- destroy chainfor each startSpriteendsprite: id = startsprite.idrepeat 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)`
B
101
S
39
G
134
Posts: 5,590
Reputation: 85,518

### » Thu Feb 04, 2016 8:31 pm

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?
B
16
S
2
Posts: 60
Reputation: 989

### » Thu Feb 04, 2016 9:06 pm

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
B
101
S
39
G
134
Posts: 5,590
Reputation: 85,518

### » Thu Feb 04, 2016 9:17 pm

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!
B
16
S
2
Posts: 60
Reputation: 989