Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Announcements and Showcase
Raycast vehicle physics with Cannon js

okeoke
Registered User
Quote
2022-10-20 23:52:48

Hi,

Here is the demo: https://youtu.be/GrxBSINym_8

Currently, I'm still working on my sheep racing game. I was never satisfied with the physics I could implement myself, and started to look into the 3rd party libraries that can do a car physics for me. So last week or so I was toying with cannon js.

After some investigation it appears that I can sort of integrate it to run on windows target. It works, but after a minute of running the demo I receive "out of memory" exception. It seems to me, that the library creates a lot of Vec3 type objects, which are never removed by garbage collector. Unfortunately, this is not something I can fix.

So I decided to try using webgl + electron combination. And it seems to work quite well. At rarely exceeds 50mb of ram and feels pretty smooth.

You can find both webgl and desktop projects in the video description. If you're interested in the implementation details please feel free to ask.

Sheep driving slowly becomes "need for speed: sheep driving":)


j9907
Guest
Quote
2022-10-21 03:08:35

WOW! That is amazing!

How did you even load / use it in CC?


adeb
Registered User
Quote
2022-10-21 10:17:17

Hi,


try ccbCleanMemory()


okeoke
Registered User
Quote
2022-10-21 22:05:44

@j9907
With desktop I made some changes to the pre-built library and just initialize everything inside action attach to "before draw do smth".
Webgl version is initialized as a regular js script inside the produced html.
Physics and collisions are turned off in coppercube, so all simulations run by cannon js. Coppercube objects just change the positions and rotations accordingly. Obviously, there are more to that, like heigtmap should be the same between CCB and cannon js, simulation is scaled down in size in cannon, cannon js operates with quaterions instead of euler coordinates, z axis has different direction is cannon and etc:)

@adeb
Tried that - unfrotunately, it's still the same. It also might be that I'm just initializing something wrong. I'm going to run electron because I can also make a decent engine sfx with a pitch shifter and add controller support that way.


hadoken
Guest
Quote
2022-10-21 22:16:56

Great Job @okeoke! Thanks for the demo.

Making Cannon.js accessible for CopperCube coding makes me wonder about two questions:

How did you get to this?

Would it be possible from your point of view to include more javascript library APIs in the future except for Bluebird.js and Cannon.js?

From my testing unfortunately ccbCleanMemory() doesn't fix the issue, sorry @adeb.

I also already managed to locally build desktop apps from CopperCube webgl + electron combination. So we can build and distribute CopperCube projects on several platforms like others can e.g. with godot or unity.


quest
Guest
Quote
2022-10-21 23:08:26

i tryed the demo you provided and it was not so good in my opinion the car shakes much when moving.


Robo
Guest
Quote
2022-10-22 04:39:12

Great work - I wonder if it will be better to use for drag n drop items than CC physics...?


okeoke
Registered User
Quote
2022-10-22 11:02:54

Hi guys, thank you!

@hadoken

I think it should be possible to use other libraries with electron. Cool thing about it is that it runs node js as a backend and chromium as a front, so theoretically it's possible to run both node js and browser libraries. Currently, I run cannon js on the front end, but I think it also should be possible to do it on back, so the physics simulation will run in a separate thread and doesn't affect FE performance. Theoretically, it's even possible to spawn a separate thread for the physics simulation:) I'm actually looking on running cannon js on the back end side, since the library itself doesn't seem to use any browser specific APIs.

@quest

You actually right, I uploaded the broken version. I broke it while trying to fix out of memory issue. You can find the fixed version here: https://drive.google.com/file/d/1twCikZh8s9pEs1LsnUIcMQUxThO-zsp8/view?usp=sharing

@Robo
I'm not sure if it will be better, but definately harder to setup:)


hadoken
Guest
Quote
2022-10-22 12:10:19

@okeoke

Have you fixed the memory problem so that Cannon.js physics can also be used with Windows .exe targets now? That would be great. If yes, could we change collision shapes/dimensions at runtime then? That would be even better. Or am I wrong?


okeoke
Registered User
Quote
2022-10-22 15:06:09

@hadoken

Nope, unfortunately, I only fixed the jumpy moving. Out of memory issue is still there.


andgameplay
Registered User
Quote
2022-10-22 17:21:51

It is amazing! Congratulation and thanks for sharing this great project with us! I think is very hard to bring physics for a car in Coppercube using differets librarys! Amazing work!


just_in_case
Moderator
Quote
2022-10-23 12:16:00

Finally a better physics demo.... Thanks for sharing :)


Guest
Guest
Quote
2022-10-23 12:48:02

The webgl open source engine need some love


andgameplay
Registered User
Quote
2023-06-06 16:49:46

Hi okeoke, your project is great! I'm trying to use your project but still there is a problem about the end of memory :( Any news about this? There is a update?


okeoke
Registered User
Quote
2023-06-06 22:14:35

Hi andgameplay,

Unfortunately, I never managed to make it work with no issues.
It uses the old version of cannon js library https://schteppe.github.io/cannon.js/, which I slightly modified to be usable with coppercube.

I put numerous hours into trying to fix the error, but didn't succeed. I've ran the webgl version and collect memory heap data (I also build an electron app and run the physics simulation inside node js, it was almost the same). It seems like cannon js create a lot of vec3 and polyhedrons objects during the runtime. For some reason it's not the issue with browser, since js garbage collectors removes them, but for coppecube it works differently - there is also no way to learn what exactly happens, since it's not possible to access coppercube heap memory (or whatever it's called in c). Probably, the way to fix is to reuse existing Vec3 objects and arrays instead of creating the new ones here and there, but cannon js library is 13k lines of code, so I'm not sure I want to modify it to figure out if this helps or not:)

There is also a new tool called neutralino https://neutralino.js.org/docs/. It's a browser wrapper which allows you to run web application on desktop. Unlike electron this one is pretty lightweight: basic application is ~200 mb with electron and 8 mb with it. My best suggestion would be build a browser game, use neutralino to create an exe file. And also run cannon inside a separate process using web workers like it's described here: https://github.com/pmndrs/cannon-es/blob/master/examples/worker.html.

I probably, can build a prototype and see if it works if you're interested. But it also means you have to use webgl for the game you're making.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internatio?al" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact