Boids in Jack?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Boids in Jack?

yokes_in_flight
For module 5 in part 2 of the Coursera course, you have to implement a Jack application.

I tried implementing Boids (https://en.wikipedia.org/wiki/Boids) in Jack, but very quickly ran into what I suspect is memory constraints (pretty difficult to debug).

I had the following classes: vector, boid, boid controller, interface controller and main.

Each boid has to calculate three vectors for each boid close to it (but to determine which boids are close, it has to calculate a distance between it and every other boid using vector subtraction): separation, alignment and cohesion. Each boid does in every update, and every vector is an instance.

I’m 99% sure I didn’t have memory leaks, and I’m attributing it to the total amount of memory available (16 bits) not being enough. Hence: it’s not really straightforward to implement boids in Jack (running on the Hack computer) in a way that’s memory efficient enough.

Am I on the right track, or am I making a mistake in my thinking?
Reply | Threaded
Open this post in threaded view
|

Re: Boids in Jack?

dolomiti7
Implementing Boids in Jack on the Hack platform is for sure a challenging task. I guess it is generally possible, though you have to accept certain limitations and therefore be careful on your design decisions:

1. RAM (e.g. Heap) size: 14k 16-bit words of Heap (before OS space for charset etc, but that's not relevant on VM emulator level) is not a lot, but I don't think this is a general game stopper. It will just limit the amount of boids that you can simulate.
If you allocate the Heap via the Hack OS API (Memory.alloc()), you should get a proper error message on the screen if you run out of memory, and it should be straightforward to debug. Therefore I doubt that this is your main issue, unless you are using your own logic to write to memory.
Other than that the amount of required memory depends very much on your way of implementation.
2. Speed: depending on which emulator you are running your program (and again the size of your boid population), it could be quite slow. However, there are faster emulators available, so this is more of an arbitrary limitation of the nand2tetris VM emulator.
3. Math: a typical implementation of Boids relies on floating point vectors. Jack doesn't provide floating point support out of the box. There are certain implementations of floating point implementations around that you could use. However, software based floating point calculations further impact the speed. It also increases the program size, though this is not relevant on the VM emulator level (but it would have an impact if you try to translate your program into Hack ASM later on and have to stay below 32k program size).
Alternatively, you could think about a purely integer based implementation, impacting the movements of the boids. Or perhaps work with scaled integers/fixed point arithmetic to mitigate such limitations.

So again, I don't think it is impossible, but extremely challenging.
Reply | Threaded
Open this post in threaded view
|

Re: Boids in Jack?

yokes_in_flight
Thanks! I got a flavour of it working! https://github.com/emilesilvis/nand2tetris_joids

I had to drop my Vector2 class () and instead of doing the fancy vector arithmetic to accumulate forces, I had to go with a more "direct" approach. It works, but after about 13 boids the emulator slows down.