Matrix manipulations, anyone?

Get help using Construct 2

Post » Fri Feb 28, 2014 1:00 am

I am in the process of trying to build a DC circuit simulator similar to the excellent one available at phet.colorado.edu. In order to make it work, I will need to do some matrix manipulations, specifically, computing the inverse of a matrix of size n x n and matrix multiplication.

Has anyone attempted to do this in Construct 2?

Methinks an extension to the Array plugin would be really cool...
www.simbucket.com - HTML5 Science Simulations / https://www.airconsole.com/#!play=com.n ... obotrumble - Robot Rumble on AirConsole
B
52
S
17
G
26
Posts: 432
Reputation: 18,084

Post » Fri Feb 28, 2014 3:38 am

This sounds like much more than an array plugin extension. You can probably achieve this easily enough with an existing javascript matrix library though, or even build a plugin around an existing library.
B
11
S
2
G
3
Posts: 283
Reputation: 1,968

Post » Sat Mar 01, 2014 4:49 pm

So I've tried the following so far to recursively compute an array determinant:

1. I've created an object called "array" in the layout. On start of layout I set all of the values for each XY.

2. Each time I call the "computedeterminant" function, I create a new, smaller array of (n-1) x (n-1) for the next step in the recursion.

3. My plan was to pick a value from the smaller array, set it to a temporary variable, then plug that value into the bigger array.

It looks like my smaller arrays aren't getting filled properly because I can't pick the original by UID.
www.simbucket.com - HTML5 Science Simulations / https://www.airconsole.com/#!play=com.n ... obotrumble - Robot Rumble on AirConsole
B
52
S
17
G
26
Posts: 432
Reputation: 18,084

Post » Sat Mar 01, 2014 5:11 pm

I smell a job for the hound :-)
B
51
S
16
G
9
Posts: 1,098
Reputation: 11,252

Post » Sat Mar 01, 2014 8:04 pm

Picking problem is solved with the "System -> Pick All" condition. Thank you Yann for the excellent picking video!

On to other troubleshooting...
www.simbucket.com - HTML5 Science Simulations / https://www.airconsole.com/#!play=com.n ... obotrumble - Robot Rumble on AirConsole
B
52
S
17
G
26
Posts: 432
Reputation: 18,084

Post » Sat Mar 01, 2014 9:33 pm

circuitbuilder.capx


So here is my most recent attempt. It looks like the value of the local variable "determinant" is being reset inappropriately in a recursion loop. Maybe a javascript variable scope issue?
You do not have the required permissions to view the files attached to this post.
www.simbucket.com - HTML5 Science Simulations / https://www.airconsole.com/#!play=com.n ... obotrumble - Robot Rumble on AirConsole
B
52
S
17
G
26
Posts: 432
Reputation: 18,084

Post » Sun Mar 02, 2014 8:15 pm

The determinant is now computed correctly. The problem appears to be that loopindex("x") is in the global scope. In other words, if you recursively call a function with for() loops in it, it will always reference the first time you called a loop of that name. The solution was to uniquely identify the loop with a different name in each iteration.

In this case, instead of calling a for() loop with the tag "i", I called the for loop with the tag "i"&n, where n is the current size of the matrix I am working with. This is unique for each level of the recursion.

See the attached image for the solution.

Ashley, is recursion supposed to work this way? Or am I missing something?

matrixdeterminanteventsheet.PNG
:
You do not have the required permissions to view the files attached to this post.
www.simbucket.com - HTML5 Science Simulations / https://www.airconsole.com/#!play=com.n ... obotrumble - Robot Rumble on AirConsole
B
52
S
17
G
26
Posts: 432
Reputation: 18,084


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 7 guests