Box2d Light Example

Make a 2d light for new game. Whant to share it with community. :partying_face:
SOURCES

21 Likes

I use native box2d to make it.
You can create
1)Point light(360 degree)
2)Cone light(1-359 degree)

Idea is simple
1)cast rays around point
2)change mesh vertices position
3)draw mesh as light

4 Likes

Make this light for “Ninja Mouse” game. Play. It cool:)
https://poki.com/en/g/ninja-mou

7 Likes

This is really great! Thanks for sharing! :smiley:

2 Likes

Thank you so much for sharing! This is amazing and super useful for me! :heart:

2 Likes

I have problems running the example from the repo:

:confused:

I think algorithm header is missing in light.cpp, perhaps?

2 Likes

Thanks.
Add #include in light.cpp

3 Likes

It’s working!

Thank you for a lightning fast fix! :smiling_face_with_three_hearts:

5 Likes

Good job!

2022-09-19 00_39_05-d954mas

8 Likes

Added another few! :partying_face:
It’s not only the lighting example, it’s a well written backbone!

image

4 Likes

Don’t you mind explaining what actually needs to be done to import tilemap from Tiled to the json’s used there?

If I understand correctly - we should export from Tiled to tmx AND also to Lua, right? (but not JSON exported from Tiled, because it’s not working :confused: )

Then how do you create the JSON out of this?
How do you produce the JSONs from catalog levels/editor/result?

The main idea described here
https://github.com/d954mas/defold-tiled-example
1)Create level in tile
2)Export that level as lua file
3)Parser will read that lua file, and convert to json

In assets\bundle\common\levels\ you have update_levels_editor.sh
That script run tiled in command line to export all levels in lua
Then it call parser.lua. Parser wil save levels as .json

It should worked. But you will need to install some libs from luaRocks in your system.

1 Like

It is a little bit complex, sorry:)
But it gives more power to level creation in tiled. Like using objects or properties

1 Like

Thank you!
I’m getting familiar with this, so yeah, I need to install some luarocks yet and then - this parser script is run automatically in this project somewhere, right? So, for example if I:

  1. Make some changes in Tiled to level_1.tmx
  2. Run the script or just export Lua manually from tiled and overwrkte this level_1.lua
  3. Run the game - the parser will produce a new level_1.json (overwrite the current one) and it will be used in the game then, right?

Or I run the parser script - and it converts tmx to Lua and then Lua to json automatically?

I think, I just want to ask, if I can just convert Lua exported manually from Tiled - to json? :grin:

Parser also run in system not in game. Parser also can run inside the game but it is more complex way of using it:)

Parser need libs from luaRocks because it is a lua script that run on your system.
1)Export from tiled. By script or manualy
2)Run parser. lua ./parser/parser.lua
3)Run game

sh resave_tiled_maps_editor.sh
lua ./parser/parser.lua
1 Like

I’m on Ubuntu, I installed:

sudo apt install -y lua5.3
sudo apt-get install -y luarocks
sudo luarocks install luafilesystem
sudo luarocks install lua-cjson
sudo luarocks install luabitop

I modified the script to use proper Tiled image:

mkdir -p ./editor/lua
rm -r ./editor/lua/

#replace path to tiled
#TILED_PATH="/C/Program\ Files/Tiled/tiled.exe"

echo resave tilesets;
/home/pjarosz4/Desktop/tiled/Tiled-1.9.0.AppImage --export-map --embed-tilesets lua tilesets/tilesets.tmx ./tilesets/tilesets.lua

echo resave maps;

for f in $(find ./editor/sources -name '*.tmx'); do
	fname=`basename $f`
	newname=${fname%.*}.lua
	echo $f;
	/home/pjarosz4/Desktop/tiled/Tiled-1.9.0.AppImage --export-map lua $f ./editor/lua/$newname
done;

But still it fails:

    08:23:48 ~/Downloads/defold-box2d-light-demo-master (1)/defold-box2d-light-demo-master/assets/bundle/common/levels  $ sh update_levels_editor.sh 1
    resave tilesets
    resave maps
    ./editor/sources/level_1.tmx
    Failed to export map to target file.
    ./editor/sources/level_11.tmx
    Failed to export map to target file.
    ./editor/sources/level_2.tmx
    Failed to export map to target file.
    lua: error loading module 'lfs' from file '/usr/local/lib/lua/5.3/lfs.so':
    	/usr/local/lib/lua/5.3/lfs.so: undefined symbol: luaL_register
    stack traceback:
    	[C]: in ?
    	[C]: in function 'require'
    	./parser/parser.lua:9: in main chunk
    	[C]: in ?
Press any keyupdate_levels_editor.sh: 3: read: arg count

luaL_register doesn’t exist in Lua 5.3.
It exists in Lua 5.1 and 5.2.
Note that Defold uses Lua 5.1.

1 Like

Thank you!
Used 5.1, but now it doesn’t know lfs:

lua: ./parser/parser.lua:9: module 'lfs' not found:
	no field package.preload['lfs']
	no file './lfs.lua'
	no file '/usr/local/share/lua/5.1/lfs.lua'
	no file '/usr/local/share/lua/5.1/lfs/init.lua'
	no file '/usr/local/lib/lua/5.1/lfs.lua'
	no file '/usr/local/lib/lua/5.1/lfs/init.lua'
	no file '/usr/share/lua/5.1/lfs.lua'
	no file '/usr/share/lua/5.1/lfs/init.lua'
	no file '..\..\..\..\lfs.lua'
	no file './lfs.so'
	no file '/usr/local/lib/lua/5.1/lfs.so'
	no file '/usr/lib/x86_64-linux-gnu/lua/5.1/lfs.so'
	no file '/usr/lib/lua/5.1/lfs.so'
	no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
	[C]: in function 'reqf'
	./parser/parser.lua:9: in main chunk
	[C]: ?
Press any keyupdate_levels_editor.sh: 3: read: arg count

I can’t install lfs for lua 5.1 using just luarocks or I’m doing something wrong :confused:

10:22:21 ~/Downloads/defold-box2d-light-demo-master (1)/defold-box2d-light-demo-master/assets/bundle/common/levels $ sudo luarocks install lfs

10:22:21 ~/Downloads/defold-box2d-light-demo-master (1)/defold-box2d-light-demo-master/assets/bundle/common/levels  $ sudo luarocks install lfs
    
Error: No results matching query were found.
10:23:11 ~/Downloads/defold-box2d-light-demo-master (1)/defold-box2d-light-demo-master/assets/bundle/common/levels  $ sudo luarocks install luafilesystem
Installing https://luarocks.org/luafilesystem-1.8.0-1.src.rock
gcc -O2 -fPIC -I/usr/include/lua5.1 -c src/lfs.c -o src/lfs.o
gcc -shared -o lfs.so -L/usr/local/lib src/lfs.o
Warning: /usr/local/lib/lua/5.1/luafilesystem_1_8_0_1-lfs.so is not tracked by this installation of LuaRocks. Moving it to /usr/local/lib/lua/5.1/luafilesystem_1_8_0_1-lfs.so~
luafilesystem 1.8.0-1 is now installed in /usr/local (license: MIT/X11)

Maybe you should move this to another thread?

I don’t think I have such powers :sweat_smile: but I would love to move it (it rather fits defold-tiled example more)