ChunkLoader Engine


SP4CEBAR 2021-09-30 16:42 (Edited)

The goal of this engine is to put many mechanics together to make epic games!

It can refresh the NX background in chunks so that you can make bigger or infinite maps, it also has motion physics, collision detection, projectiles, and explosions.

You're free to use this engine in any of your programs.

At the core of this is a flexible infinite terrain loader with multiple procedural generators, with structures, and compressed file storage for player builds and mines.

The gun systems are inspired by RandomHeroes and BroForce
The space elements will be inspired by "Into Space 2" and "Space Flight Simulator"
The mining, item, and crafting mechanics will be inspired by Minecraft
The car driving will be inspired by HillClimb racing


each chunk has a size of 8x8 cells
4x4 chunks fit inside the 32x32 cell background
3x3 chunks will be visible at any time
diagonal loading loads 7 chunks
vertical or horizontal loading loads 4 chunks

The map loader will be able to load NX backgrounds, predefined structures, compressed mining map data, procedural terrain, procedural platforms, procedural ores, maybe procedural caves

It has position, velocity, acceleration, and smooth map scrolling, and it can check collision with pixels inside a cell

Projectiles are arrays that hold the position (x,y) and the velocity (dx,dy) of all projectiles, these are updated each frame.

how to use

To add code:

To control the engine:

Terrain successors


ChunkLoader.nx | Open in app
2022-04-03 19:39
ChunkLoader.nx | Open in app
2022-04-03 18:54
ChunkLoader.nx | Open in app
2022-04-03 18:21
ChunkLoader.nx | Open in app
2022-04-03 13:50
ChunkLoader.nx | Open in app
2022-04-03 13:14
ChunkLoader.nx | Open in app
2022-04-02 11:18
ChunkLoader.nx | Open in app
2022-04-02 10:54
ChunkLoader.nx | Open in app
2022-04-02 10:30
ChunkLoader.nx | Open in app
2022-04-02 08:56
ChunkLoader.nx | Open in app
2022-03-25 19:20
ChunkLoader.nx | Open in app
2022-03-25 17:24
ChunkLoader.nx | Open in app
2022-03-25 08:48
ChunkLoader.nx | Open in app
2022-03-24 23:00
ChunkLoader.nx | Open in app
2022-03-23 08:47
ChunkLoader.nx | Open in app
2022-03-22 15:26
ChunkLoader.nx | Open in app
2022-03-21 23:04
ChunkLoader.nx | Open in app
2022-03-17 11:43
ChunkLoader.nx | Open in app
2022-03-16 21:52
ChunkLoader.nx | Open in app
2022-03-16 21:46
ChunkLoader.nx | Open in app
2022-03-08 13:33
ChunkLoader.nx | Open in app
2022-03-07 22:41
ChunkLoader.nx | Open in app
2022-03-06 22:58
ChunkLoader.nx | Open in app
2022-03-06 22:39
ChunkLoader.nx | Open in app
2022-03-06 22:32
ChunkLoader.nx | Open in app
2022-03-06 11:53
ChunkLoader.nx | Open in app
2021-12-14 12:06
ChunkLoader.nx | Open in app
2021-10-24 12:16
ChunkLoader.nx | Open in app
2021-10-23 20:16
ChunkLoader.nx | Open in app
2021-10-23 19:43
ChunkLoader.nx | Open in app
2021-10-23 15:57
ChunkLoader.nx | Open in app
2021-10-11 08:43
ChunkLoader.nx | Open in app
2021-10-10 13:00
ChunkLoader.nx | Open in app
2021-10-08 21:20
ChunkLoader.nx | Open in app
2021-10-06 16:07
ChunkLoader.nx | Open in app
2021-10-06 15:30
ChunkLoader.nx | Open in app
2021-10-06 15:14
ChunkLoader.nx | Open in app
2021-10-06 13:38
ChunkLoader.nx | Open in app
2021-10-05 21:20
ChunkLoader.nx | Open in app
2021-10-05 10:58
ChunkLoader.nx | Open in app
2021-09-30 16:42

Nicky 2021-10-01 13:57

I find an error

SP4CEBAR 2021-10-01 15:49

Negative coordinates?
Those aren't supported yet
I can quickly fix that using ABS(X MOD 32))

SP4CEBAR 2021-10-01 16:04

wait, that still mirrors it

SP4CEBAR 2021-10-01 16:06

Does someone know if there's an elegant solution to making X MOD 4 output 0,1,2,3,0,1,2,3... for every integer of X

SP4CEBAR 2021-10-01 16:44

The negative outputs of mod are flipped, but they also share the same zero which makes it quite annoying to get it to be 0,1,2,3,0,1,2,3... continuously for every integer value, this is the best I've come up with so far
FOR I=-7 TO 7
PRINT I,-A*3+(I-A) MOD 4

SP4CEBAR 2021-10-01 16:46 (Edited)

Or I could try to turn the input into an unsigned variable
FOR I=-7 TO 7
PRINT I,(I+2^14) MOD 4
Does anyone know what value of 2^n would be optimal for the floats NX uses?
I guess I shouldn't add very big values to it, because that'll make it discard relatively insignificant numbers right?

SP4CEBAR 2021-10-05 11:00 (Edited)

Thanks to the awesome support on the forum, it can now load negative cells!

SP4CEBAR 2021-10-05 21:24 (Edited)

In this version, collision works! It's inspired by the collision from Timo's map scroller

SP4CEBAR 2021-10-05 21:24 (Edited)

Next I'll work on terrain wedges again, maybe I could make some collision system for custom-shaped terrain-cells, to do that I probably should generate a table, which I'll probably store in RAM

SP4CEBAR 2021-10-06 13:39 (Edited)

It now has in-cell collision for wedges, but I can also make it work for other shapes

SP4CEBAR 2021-10-06 13:39 (Edited)

Also, It looks like you can puncture the collision box, I hope the terrain won't be too spiky then

SP4CEBAR 2021-10-06 15:16 (Edited)

It can now determine the shape of a cell and use that to check for collision with pixels inside a cell

SP4CEBAR 2021-10-06 15:22 (Edited)

Also, ignore the clouds, I forgot about them

SP4CEBAR 2021-10-06 15:31

Clouds have been fixed, and the terrain has been tidied up

SP4CEBAR 2021-10-06 15:33 (Edited)

The 4-point collision box is being punctured by the uneven terrain, should I add more points to the bottom of the collision box?

SP4CEBAR 2021-10-06 16:08

I've added comments to make the code easier to read

SP4CEBAR 2021-10-08 21:22 (Edited)

I'm experimenting with processing procedural terrain, so it can pick the right cells to get smooth terrain
It finds the delta y for the current terrain cell, the next one and the previous one, and uses that to detect a few predefined cases, which defines the character

My procedural terrain is currently just a bunch of sine waves, I actually still need to make a pseudo-random generator (so that I can insert a seed every time) and a procedural generator with something like Perlin noise

SP4CEBAR 2021-10-22 14:35

I just got the randomwalk-based procedural generator from adventure engine working
Surprisingly enough, it runs more reliable here than in adventure engine

SP4CEBAR 2021-10-22 15:41

I'm going to add the physics from the orbit simulator to this engine

SP4CEBAR 2021-10-22 18:00 (Edited)

The more programs I'm going to merge with this the more complicated it's going to get,
I really need to keep this organized!

SP4CEBAR 2021-10-22 18:02

At one point I'll throw my gun mechanics into this as well

SP4CEBAR 2021-10-23 16:02 (Edited)

I added the physics from my orbit simulator (I disabled the space-gravity... temporarily)
I upgraded my collision system to use was8bit's sprite-based in-cell collision detection and my collision system can now detect collision for objects that consist of multiple sprites (this costs a lot of performance)

SP4CEBAR 2021-10-23 16:07

With the new sprite-based collision, the collision box won't get punctured anymore

Timo 2021-10-23 16:23

It's fun with the rocket :)

SP4CEBAR 2021-10-23 19:45 (Edited)

Thanks, I really love that things like this are possible with NX :D

SP4CEBAR 2021-10-23 19:47 (Edited)

I just added angular velocities, and it can tip over now...
Now I just have to get the car from adventure engine working

SP4CEBAR 2021-10-23 19:49 (Edited)

It's a little bouncy because rotating makes it clip, and the current solution to clipping is giving it negative Y velocity. The new tipping-over mechanic causes a lot more rotations, which means that it starts clipping more, and so it bounces.

SP4CEBAR 2021-10-23 20:17 (Edited)

I just added a car (the characters were just thrown together)
You can switch your player character/vehicle with button B

was8bit 2021-10-24 05:39

Way cool :D

SP4CEBAR 2021-10-24 12:17

I just fixed a bug where the rocket couldn't tip over anymore

SP4CEBAR 2021-12-13 11:00

I just realized that the collision system got bugged when I added the rocket

SP4CEBAR 2021-12-13 11:02

Time to figure out what went wrong

SP4CEBAR 2021-12-13 11:09

Ah, it was when I improved the collision system...
how did I not see that it was bugged

was8bit 2021-12-13 11:12 (Edited)

Trivia... do u know where the term "BUG" meaning "a computer glitch or error of some kind" came from ;)

SP4CEBAR 2021-12-14 12:09 (Edited)

I think that's one of the most common computer facts
It got stuck in one of those enormous vacuum tube-powered processors, which broke it

SP4CEBAR 2021-12-14 12:12

Either way, I figured it out, I the width variable got set to a new variable inside the function that does the tipping over of the rocket, I still don't know whether or not I accidentally used the variable "W" for that

was8bit 2021-12-14 12:16 (Edited)

Yea, it was a moth... the tubes glowed, which attracted the moth, it got zapped when it tried to land on the tube, and shorted out (or damaged) the tube ;)

So when they asked what was wrong with the computer, the reply was "it was a bug" ;)

SP4CEBAR 2021-12-14 12:24

Technically, they made the most sophisticated fly zapper

SP4CEBAR 2021-12-14 12:30

It turns out that my original collision system is still more reliable than my modified collision system
soo.. I've still got some work to do

SP4CEBAR 2021-12-14 12:52

The temporary sprite that is used to detect collisions seems to be offset by one pixel up and one pixel left

was8bit 2021-12-14 14:49

I zapped and probably fried ;)

I think its fun to experiment with different ways and approaches... :) but often i get more game ideas than i have time to do...

SP4CEBAR 2022-01-27 10:27 (Edited)

I want to make the file type that stores mining and building soon, I think I've come up with quite an efficient data lay-out: the map consists of big chunks of 1024x1024 cells, each chunk starts with a header which has
- it's type (which is: big chunk),
- it's file size,
- and it's coordinates (divided by 1024 so they are a lot smaller),
inside that chunk there can be 256x256 little chunks each with a size of 4x4 cells, these little chunks start with a header which has
- it's type (which tells that this is a small chunk with bit depth 1,2,3,4 (amount of block types), the file size can be derived from the type and bit depth),
- it's coordinates (they are relative to the big chunk so it's only two bytes for x and y together),
and then the data, which is two bytes if the bit depth is one (16 bits, so 16 (4x4) cells that could be mined), it's four bytes if the bit depth is two (four placable block types), six bytes if the bit depth is three (eight placable block types)

SP4CEBAR 2022-01-27 10:36

It could perhaps be more efficient to have small chunks of 8x8 cells instead of 4x4 cells
The 4x4 small chunks have 3 header bytes and 2*BIT_DEPTH data bytes
8x8 small chunks would have 3 header bytes and 8*BIT_DEPTH data bytes, also the big chunks would then get a size of 2048x2048 cells

SP4CEBAR 2022-01-27 10:50 (Edited)

And then I could also add my gun mechanics system, and an item system, and crafting mechanics and then this engine definitely would have a lot of potential:
With guns, and vehicles, mining, and building, infinite terrain, and physics
My plans are to make HillClimb racing, Mineshaft (2D Minecraft), and then I want to make a space-themed game where an AI powered spacecraft is sent to outer space ages ago. In that time the AI has developed a society of robots, and that's basically the story line, you can read the full story line commented under "MISSION VI" inside the sourcecode of my rocket launch simulator

SP4CEBAR 2022-01-27 10:59

Another thing that makes this engine powerful is at it's core, it can switch between multiple map generators, and it can use them simultaneously (in a future version)
It would have a bunch of procedural generators for terrain, caves, ores, and platforms (human-made structures) and the map file for the mines and buildings of the player can also be loaded from here

SP4CEBAR 2022-01-31 15:02 (Edited)

Update on the compact map data file type thingy

(with the size in bytes)

In general, there are big chunks, with smaller chunks inside of them, and the smaller chunks could have a few follower chunks.

big chunk (covers 1024x1024 cells) (header is 6 bytes):

1 : bits: 1 chunk type (which is "big chunk", stored as "1")
. . . . . . : 7 chunk count 1 (one chunk equals 5 bytes)
1 : chunk count 2 (one chunk equals 5 bytes) (I could remove it to make this header also five bytes in size and have a repeated header after 128 chunks)
2 : x-coordinate (multiplied by 1024 (big chunk))
2 : y-coordinate (multiplied by 1024 (big chunk))

chunk (4x4 cells) (5 bytes):

1 : type bits: 2 chunk type (which is "chunk", stored as "01")
. . . . . . . . . : 2 4 size presets (the amount of followers)
. . . . . . . . . : 4 16 block presets (characters + palettes)
1 : x-coordinate (relative to big chunk)
1 : y-coordinate (relative to big chunk)
2 : data (4x4 bitmap, 2 block types: mined or not mined)

follower (4x4 cells) (5 bytes):

1 : type bits: 2 chunk type (which is "follower", stored as "00")
. . . . . . . . . : 2 chunks to skip to reach source chunk, skip 4 could lead to another follower
. . . . . . . . . : 4 16 block presets (characters + palettes)
4 : data (4x4 bitmap, 4 block types)

Using the big chunk header, the search can find the right big chunk. I designed the chunk and the follower to have the same size so that I can jump through it and skip a few chunks (reduced searching time), if search lands on a follower, it'll read how many chunks to skip backward to get to the source chunk, if there are a lot of chained followers, then it'll take multiple skips (of four chunks) to get to the chunk (I could make the skipping steps 2^n, so {00,01,10,11} will become {1,2,4,8} steps to skip, which will speed up the searching time if there would be many followers)

To add new chunks all it has to do is shift the data 5 bytes using the COPY command, and then it can add the chunk.

If I remember it correctly I could use the disk.nx to extend the persistent RAM once it gets full.

was8bit 2022-01-31 22:34


SP4CEBAR 2022-03-06 12:01

I just fixed my collision system
When I changed it to place a sprite to represent terrain and use sprite hit between the player's sprite(s) and the terrain sprites, I lost my collision check offsets

The terrain sprites were only allowed to be placed on the terrain and offsetted by the background scrolling, so the only data my collision system was getting is whether or not the player's sprites were hitting the terrain
So I fixed it by allowing the terrain sprites to be ofsetted based on the offset of the player's collision box
With this change, the collision system can detect when a vertical surface is hit, when a horizontal surface is hit and when it's clipping into a surface and also when it touches land (so it can give it friction)

SP4CEBAR 2022-03-06 12:02

Now I can continue my mining mechanics

was8bit 2022-03-06 13:15

Offsets give me headaches ! ;)

SP4CEBAR 2022-03-06 22:35

In this case I really needed to use them
working with offsets is nowhere near as bad as working with atan(dy/dx) and atan2 is even worse!

SP4CEBAR 2022-03-06 22:35

Either way, I imported my gun mechanics!

SP4CEBAR 2022-03-06 22:59

And now you can break blocks by shooting

was8bit 2022-03-07 04:43

Atan... i completely agree!!!

SP4CEBAR 2022-03-07 22:46 (Edited)

One solution I found to replace atan is to reduce your situation to a unit vector:
First, divide a vector (x,y) by it's length
L = sqr(x^2 + y^2) (Pythagoras)
X = X / L
Y = Y / L
and then you can use
acos(x) * sgn(y)
to get the angle between that vector and the x-axis

SP4CEBAR 2022-03-07 22:49

Either way, I just made the guns more satisfying by adding
- knockback (it's strength based on a weapons accuracy, which works for all weapons except shotguns)
- more bullet speed
- fixed "no bullet" bug
- projectiles now have a random rotation instead of a random dy, which makes it more realistic

was8bit 2022-03-08 00:11

You're building a very powerful system :)

SP4CEBAR 2022-03-08 08:04

Thank you

SP4CEBAR 2022-03-08 13:35

The flip of the terrain cells is now randomized, now the ground doesn't have a repeating pattern, which makes it look more natural

SP4CEBAR 2022-03-08 19:28 (Edited)

I just realized atan2 in javascript takes two parameters (dx and dy) instead of the one (dy/dx) parameter I kept giving it, that's why atan2 never worked for me
however, I still prefer using unit vectors over atan2:
I once tried to make atan2 from scratch for my orbit simulator, and it was hard and buggy, unit vectors take much less code and are so much cleaner!

I'm glad I used atan2 in JavaScript wrong, because I would otherwise never have thought of using unit vectors for this

SP4CEBAR 2022-03-16 21:49

Added explosion mechanics
- random explosion sprites
- effects controller (keep it going for a few more frames)
- manhattan pattern for breaking blocks

And all projectiles now have gravity (to demonstrate the concept)

SP4CEBAR 2022-03-16 22:06 (Edited)

If you want to have some fun, search for
DATA 60 ,0 ,1.5 ,1,1,-1
and replace it with
DATA 1 ,0.4 ,1.5 ,1,1,-1
This line of data changes the properties of the rocket launcher
With the new data it's destructive power is enormous

was8bit 2022-03-16 22:51

Yea, rapid fire ... way cool :D

SP4CEBAR 2022-03-17 11:43

I just added grenades

was8bit 2022-03-17 14:35

Oooo,, coool :)

SP4CEBAR 2022-03-21 23:06

Added trees, atmosphere layer's colors, and a background

was8bit 2022-03-22 00:49

Very cool :)

SP4CEBAR 2022-03-22 15:27 (Edited)

I improved the background
Now you can see the foreground (because of the contrast)
And the background will slowly get out of view as you move upwards

was8bit 2022-03-22 17:41

Nice touch :)

SP4CEBAR 2022-03-23 08:49

It now has platforms!
although they do need some more work
I think the platforms will make it a lot more interesting

SP4CEBAR 2022-03-24 23:01

I made the journey to space look nice (press left B to switch vehicle (to rocket))
And I've worked some more on the platforms

SP4CEBAR 2022-03-24 23:02

And the CPU usage is 100%, that's a bit of an issue...

SP4CEBAR 2022-03-25 08:50

I just added a tree corrector corrector which removes the floating tree tops created by the tree corrector which corrects trees that are cut off at a chunk border

was8bit 2022-03-25 11:49

While in car mode, i got the tires to spin faster than helicoptor blades... then when going up and floating it up and down in the air, i totally crashed your game... of seized and froze up...

SP4CEBAR 2022-03-25 17:25

Like... an infinite loop?

SP4CEBAR 2022-03-25 17:27

also I just fixed the buildings (they spawn reliably) and I fixed the grenade throwing (now it doesn't have a muzzle flash and gun sounds, and it's thrown from the arms instead of being pooped out or something) I also made a throwing sound for the grenades, and I changed the look of the automatic rifle

SP4CEBAR 2022-03-25 19:27

I fixed the bug where, if the player were to shoot while standing on a flat surface, the projectile would immediately hit the ground
I looked at the cell checking function, got triggered by the "... \ ..." and replaced it with "int( ... / ... )", this is because "... \ ..." does some funky things with negative numbers, and this is a scenario where negative numbers could occur
And that fixed it! no debugging needed!
Man, I love coding with experience

SP4CEBAR 2022-03-25 19:46 (Edited)

I got a plan on how I'm going to orbit around my flat earth

  1. Implement "SpaceExplorer" as a viewmode (it just so happened to be there already)
  2. Do the orbit math
  3. Figure out which gravity influence is the most relevant at any point
  4. Calculate the distance (R) between the player's spacecraft and the center of the gravity source, we also need to calculate the angle (ANG) of the spacecraft, from the perspective of the gravity source, the angle is in radians, we now have polar coordinates!
  5. Our main chunkloader graphics renderer (with the flat earth) is going to set the player Y coordinate to R, and the X coordinate to -ANG*R (thanks to radians we don't need to care about 2*PI (we're using the circumference of a perfectly circular orbit at our radius))

Once there's a new relevant gravity source, this viewmode can immediately be switched to the new space object (moon, planet, star)

The only issue remaining is that I didn't manage to simulate orbits in our solar system yet
The new plan for that is to sample our orbit, and generate the appropriate cone section formula, I attempted this before, I hope I can pull it off this time

was8bit 2022-03-26 08:52 (Edited)

Ok, so take your latest posted update... and as a guy, fire awhile without moving... you will find yourself going downward....

Now, switch to the car and try to move... you will find yourself going upward and your wheels spin around your car slowly like clockhands.... with a little effort you can get your wheels spinning around your car so fast you can't see your wheels anymore...

SP4CEBAR 2022-03-27 21:52

I don't seem to be able to reproduce it, how long is the "a while" in the first sentence of your post, is it something like five seconds, or a minute?

I'm not too worried about this bug, the buggy is buggy and needs some rework anyway

was8bit 2022-03-28 04:35

Oh, ok... I am rather good at testing things and finding things... ;) I enjoy trying to play games in a manner not intended by the creator ;)

SP4CEBAR 2022-03-28 11:33 (Edited)

Finding exploits is always fun :)

SP4CEBAR 2022-04-02 08:58 (Edited)

I just added enemies and gave them motion physics and collision (subprogram is shared with the player)
The bug you discovered doesn't just happen to the car, go fast with minigun knockback and you'll see

SP4CEBAR 2022-04-02 09:46 (Edited)

I noticed that if you hit the ground while going at a decent speed, you'll get bumped backwards, this is your transition from drag to friction (the game has a ground-touch detector) the drag fromula uses velocity squared, which could mean that hitting the ground at really high speeds, gives you quite an enormous punch backwards, into the farlands

SP4CEBAR 2022-04-02 10:31

I fixed the resistance, now only drag has the velocity squared, and friction is multiplied by the velocity (it's a percentage of the velocity)

SP4CEBAR 2022-04-02 10:33

@was8bit, I hope this has fixed it

was8bit 2022-04-02 10:53

Yep, the spinning still occurs, BUT at a slower rate, and it can recover normal movement ... no more helicopter speed spinning :D

SP4CEBAR 2022-04-02 10:56 (Edited)

@was8bit I did nothing to the car, I just changed the motion physics a bit, you should be able to glitch to the farlands anymore

Also the helicopter spinning couldn't have caused the car to warpdrive to the farlands, because currently the rotation can't affect the position: it just tries to find the right direction to tip over to (it was originally made for the rocket)

SP4CEBAR 2022-04-02 10:56

Also, now you can now blow enemies up

SP4CEBAR 2022-04-02 11:22

I fixed the rocket

Next I've got to finish my sprite manager to divide the 64 sprites in a dynamic, organized, and efficient way
And then it's time to write the enemy behavior, and I think I can then finally make a game out of this (I plan to make a bunch of games in this engine)

was8bit 2022-04-02 13:12

Cool !! :D

SP4CEBAR 2022-04-03 10:56 (Edited)

I just added two more menu's: inventory, and space orbit viewer
Press pause to access them

SP4CEBAR 2022-04-03 13:14 (Edited)

The blocks you mined will now show up in your inventory
Blocks mined by explosions count too, but that count isn't very accurate

SP4CEBAR 2022-04-03 13:51

The chunkloader will now load the right chunks when it's reopened (when the inventory is closed)

SP4CEBAR 2022-04-03 13:52

I hope I'm not spamming your inboxes too much...

nathanielbabiak 2022-04-03 18:11

Not at all! It's cool to see the update posts at the bottom of the upload. Much easier than trying to guess at the thing that got updated by playing the upload.

SP4CEBAR 2022-04-03 18:25

Okay :D

SP4CEBAR 2022-04-03 18:26 (Edited)

I just added stone and ores, let the mining adventure begin!
(Press left A to mine, or just use grenades)

SP4CEBAR 2022-04-03 18:55

The mines now have a background, and the mined ores (currently only iron) will end up in your inventory

SP4CEBAR 2022-04-03 19:41 (Edited)

I updated the controls of the space cam from planetarium so that planet switching works again (use right A,B)

SP4CEBAR 2022-04-03 19:43

I'll be taking a break from this project

was8bit 2022-04-04 04:02

Dang, lots of cool stuff... I am abit jealous... it's fun to be able to work on a project :)

SP4CEBAR 2022-04-04 23:12

It really is, this project is so versatile, the coding tasks could be about math, physics, logic, custom files

was8bit 2022-04-05 01:45


Log in to reply.