How do I solve this math calculation?

Get help using Construct 2

Post » 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: Image

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: Image

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

Image

Here is capx -file if somebody want to check the code in Construct2.
h*t*t*p://www.mediafire.com/download/dohmd4d3wjtbdqv/chain_creation_between_2_objects_tutorial.capx

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: 58
Reputation: 980

Post » 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 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)
B
91
S
31
G
102
Posts: 5,232
Reputation: 67,250

Post » Thu Feb 04, 2016 8:31 pm

@R0J0hound, thank you for your quick reply :)

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
Image

your method
Image

I was wondering, what is causing this? Maybe something to do with the --- loopindex/(startsprite.chainAmount + 1)) ---part?
B
16
S
2
Posts: 58
Reputation: 980

Post » 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
91
S
31
G
102
Posts: 5,232
Reputation: 67,250

Post » 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: 58
Reputation: 980


Return to How do I....?

Who is online

Users browsing this forum: Lancifer and 6 guests