News for hackers. Stuff that matters.
Re:Show HN: Simple 2D car physics with JavaScript by minikomi on Wednesday January 01, 2020 @05:48AM (#21927553)
Happy new year, everyone

https://imgur.com/a/AIxvWUh

Re:Show HN: Simple 2D car physics with JavaScript by aricblunk on Wednesday January 01, 2020 @06:44AM (#21927679)
I had a good one, but I moved it to my other monitor and it got cropped because ShareX doesn't know about HDR and blows out white pixel color ranges, but you get the idea https://i.imgur.com/cYbxStI.png
Re:Show HN: Simple 2D car physics with JavaScript by krilly on Wednesday January 01, 2020 @08:30AM (#21927922)
Can we all stop using imgur now? It's been like an hour, one of those images has been flagged as adult content and the other is 404ing. postimages.org is good
Re:Show HN: Simple 2D car physics with JavaScript by benrbray on Wednesday January 01, 2020 @12:36PM (#21929171)
what do you recommend using instead? I don't know of any good alternatives
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @07:23AM (#21927762)
Beautiful!
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @05:54AM (#21927565)
Happy new year! :D
Re:Show HN: Simple 2D car physics with JavaScript by wiz21c on Wednesday January 01, 2020 @09:39AM (#21928181)
not 100% related but is there any good information source on the web to learn how to build a good car simulator ? I've looked around and it's not very helpful. Usually I end up with physics courses which always repeating the same things. But not much about about : pneumatic (especially how they influence the overall speed), independent wheel modelling, suspension, mass shift, frame deformation, ...

I've checked the source code of torcs but it seems quite simple (although the results are very convincing).

My goal was to model a (indoor) kart.

Re:Show HN: Simple 2D car physics with JavaScript by vsskanth on Wednesday January 01, 2020 @10:46AM (#21928446)
I work as a vehicle simulation engineer for a Motorsports team.

What you're looking for is broadly classified as Multibody Dynamics. You can model each part individually and connect them to make a complete simulation. There are two parts to building these types of models:

1. IDE and Language - Modelica, ADAMS, Project Chrono, SimScape etc. There are open source and commercial solutions for this.

2. Libraries - Claytex, Modelon etc. most detailed vehicle models and components are sold separately. These are typically written in Modelica. Some IDEs like MapleSim or Mathworks bundle it in their IDE.

You can build your own components from scratch but it takes time.

The closest to open source I found is: https://sbel.wisc.edu/2018/07/23/chronovehicle-template-base...

https://github.com/projectchrono/chrono

If you want to learn how to do it properly, you can easily find online versions of this book: https://www.elsevier.com/books/the-multibody-systems-approac...

Re:Show HN: Simple 2D car physics with JavaScript by wiz21c on Wednesday January 01, 2020 @12:04PM (#21928969)
great ! the book looks like what I need first. I've never came across it during my google searches... Thanks a lot !
Re:Show HN: Simple 2D car physics with JavaScript by dahart on Wednesday January 01, 2020 @12:10PM (#21929003)
I was vehicle physics lead for a couple of games that sold very well, but are not considered racing sims. Happy to answer questions as best I can.

What is missing from Torcs? It looks like a pretty good place to start, it has independent wheel modeling, and suspension, and mass shift of the car (I suspect it can handle mass shift of the driver if that’s what you’re after).

You can probably model frame deformation yourself by building a frame in a physics engine out of flexible parts and/or joints, but I’m skeptical that it’s worth the effort when modeling indoor karts, I’m not sure they flex enough to matter, but it depends on exactly what you’re trying to do.

The car physics my team built was made on top of the Bullet physics engine, so Bullet handled integration and collision dynamics, and we added transmission, braking, suspension, wheels, etc.

It’s definitely worth asking yourself how good you need the simulator to be, and how serious you are about this. It’s a multi-year project to get something “good” and/or better than Torcs. I would tend to agree with their answer to the FAQ question “should I start my own racing sim project?” http://torcs.sourceforge.net/index.php?name=Sections&op=view...

Re:Show HN: Simple 2D car physics with JavaScript by wiz21c on Wednesday January 01, 2020 @05:07PM (#21931068)
That's a very good answer. Torcs is good but somehow the code is not 100% clear to me (I didn't catch the independent wheels simulation for example).

Flexing frames maybe overkill, you seem better placed than me to estimate that.

The FAQ answer address the difficulty of running an OSS project. It doesn't address the difficulty regarding the physics (which, I understand, maybe even more mindbending).

This is a hobby-level project. Nothing serious. I'm more interested in understanding the maths and physics. Having watched hundreds of races (my kid drives), I've seen behaviors which are pretty specific to karts (I guess). For example :

- driver mass shifts do matter (pilots constantly use them)

- karts jump when taking corners too fast and too tight (they usually slide, but when it's too much, they do jump, and decelerate...)

- turning wheels left/right very fast is used to brake during pit stops.

but you're right, I may be out of my league here :-)

Re:Show HN: Simple 2D car physics with JavaScript by dahart on Wednesday January 01, 2020 @08:42PM (#21932267)
By all means, go for it, just do it with eyes open, it’s a lot of work.

The other thing I’d add is that the physics models is only half the battle. The car and track geometry is critical to making an accurate simulation. It’s easy to get wrapped up in the car physics and forget to deal with the angles and bumps and materials and temperature of the road surface.

Cameras are huge too, if you do anything other than a fixed interior cam. A naive follow can behind a car can totally undo all the physics.

Good luck!!

Re:Show HN: Simple 2D car physics with JavaScript by gmueckl on Wednesday January 01, 2020 @01:22PM (#21929542)
The FAQ answer is the kind if condescending tone that I don't like and some developers of long running OSS projects use after a while. I know that these projects are hard and take lots of effort. I've experienced it myself. Warning people that X is more complicated than it may first seem is perfectly OK. But telling them to just stop is wrong. There are valuable lessons to be learned by starting from scratch. Commanding them to help out others instead denies them this experience. Sometimes the journey itself is the goal.
Re:Show HN: Simple 2D car physics with JavaScript by dahart on Wednesday January 01, 2020 @02:07PM (#21929827)
Speaking personally, it would be safe to say my career successes are defined by ignoring warnings like this and doing something from scratch anyway.

So I completely agree, and I guess this FAQ answer is really meant to talk to the casual developer who doesn't know what they're getting into. The person who does know and does it anyway will (and should) ignore stuff this like. I don't think it was meant to be condescending, I took it more as a joking tone but with a real warning that it's a long road, which is true.

Re:Show HN: Simple 2D car physics with JavaScript by hesdeadjim on Wednesday January 01, 2020 @10:53AM (#21928498)
It may be lighter fare, but some of the game engine packages available for purchase are pretty advanced. Check out Edy’s vehicle physics for example.

https://assetstore.unity.com/packages/tools/physics/edy-s-ve...

Re:Show HN: Simple 2D car physics with JavaScript by LoSboccacc on Wednesday January 01, 2020 @12:12PM (#21929007)
depending on how much you want to cheat, since indoor kart don't jump (aren't supposed to, at least, been at a bad track that ruined my back once) can use a very simplified 2d model using acceleration to estimate weight transfer to render suspension/tires getting loaded.

this is a good starting model https://github.com/spacejack/carphysics2d

I wrote an extension for it that handles loss of traction, power drifting and mapped terrain friction that I can share, it's all mit licensed after all, but it's in Java, it's simple enough to port it whatever language you want because it's just math primitives

Re:Show HN: Simple 2D car physics with JavaScript by wiz21c on Wednesday January 01, 2020 @04:57PM (#21931014)
Hello :-) My kid has been driving indoor karts for 4 years now and he's around 0.5 sec behind top drivers (which is very good and not good at all, depending what you're looking after). I've observed him on hundreds of races and karts do jump :-) They jump when coming too fast in a corner and turning strongly. In that situation, the wheels slides a bit and then regain grip instantly. When that happens the kart starts bumping around the front wheel which stands outside the corner. This costs a few 0.1 of seconds (that's huge in races). This happens to experienced pilots as well but they usually manage to find the limit in the curve and better breaking points to avoid it.

Simulating this is what interests me because it seems absolutely non obvious.

As said by someone else, maybe I'm looking at a problem that his out of my reach (my physics skills are rather weak). That is, I have no idea of how that happens.

Re:Show HN: Simple 2D car physics with JavaScript by ficklepickle on Wednesday January 01, 2020 @06:12PM (#21931439)
I found this article that goes in depth into kart cornering[0]. It talks about chassis flex and briefly mentions wheel hop.

I'm not a physics person either, but wheel hop seems to occur when the force overcomes friction between tire/track, causing an oscillation between grip/no grip. It is a lot worse on vehicles with suspension, because they have more give.

I find it easier to visualize with a car, because it is more pronounced. Although with a car it is usually occurs on a standing start, whereas with karts it is a lateral force thing.

This karting forum suggests high center of gravity contributes to wheel hop.[1]

[0] https://www.paradigmshiftracing.com/racing-basics/how-karts-... [1] https://4cycle.com/karting/threads/wheel-hop.87876/

Re:Show HN: Simple 2D car physics with JavaScript by acomjean on Wednesday January 01, 2020 @10:45AM (#21928438)
Cars are pretty complex. Sony spends years modeling various ones in its “grand turisimo” game.

But simpler games model cars pretty well, I’m assuming with a dozen or so parameters (turning radius, acceleration, tire grip, suspension..) you should get something. The “pako ios car simulation” (not open source), has a bunch of cars you drive around that behave more or less like actual cars.

Re:Show HN: Simple 2D car physics with JavaScript by LoSboccacc on Wednesday January 01, 2020 @06:11AM (#21927598)
> multiplayer

before you venture there I suggest you move the control code outside of the physic code, abstracting car inputs into a throttle, brake, steer structure which is piloted by inputs elsewhere and feed into the physic update tick along with the car state, or it'll make exceptionally hard to build input prediction and lag compensation further on

Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @07:22AM (#21927759)
Yeah, I think that's about what I'm actually working with right now.. Will be live really soon!
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @08:19AM (#21927894)
Multiplayer is live now!
Re:Show HN: Simple 2D car physics with JavaScript by glaberficken on Wednesday January 01, 2020 @08:53AM (#21928000)
funny that you give multiplayer to people and they will use any chance they get to display creativity. look at all the pople trying to draw with their tyres!
Re:Show HN: Simple 2D car physics with JavaScript by egeozcan on Wednesday January 01, 2020 @09:54AM (#21928241)
We all know what everyone is trying to draw.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @10:50AM (#21928469)
Penis is pretty popular :D
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @09:00AM (#21928031)
:D
Re:Show HN: Simple 2D car physics with JavaScript by samwillis on Wednesday January 01, 2020 @03:04AM (#21927169)
Love it. Reminds me of playing with similar car simulations in Flash when I was at school.

Now just make it isometric with collision detection and use maps built with this (currently also on the front page):

https://news.ycombinator.com/item?id=21923940

Re:Show HN: Simple 2D car physics with JavaScript by lloeki on Wednesday January 01, 2020 @04:59AM (#21927425)
Serious vibes of Absolute Drift here, a very zen indie car game.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @05:01AM (#21927431)
Haha, yeah! But this one is open source ;)
Re:Show HN: Simple 2D car physics with JavaScript by adventured on Wednesday January 01, 2020 @03:53AM (#21927280)
It immediately reminded me of the original Grand Theft Auto, that overhead view.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @04:36AM (#21927377)
Yeah! We used to play Slicks ’n Slides a lot for example back in the days..
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @03:39PM (#21930463)
I used to be a flash developer, started in 1999 with Flash 4 ;)
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @03:07AM (#21927174)
Fun idea!
Re:Show HN: Simple 2D car physics with JavaScript by niix on Wednesday January 01, 2020 @09:14AM (#21928081)
This is great and inspiring. Every few months I spin up a socket.io server and attempt to make an RPG. I get about as far as having the character move around the screen and then usually give up :D
Re:Show HN: Simple 2D car physics with JavaScript by Waterluvian on Wednesday January 01, 2020 @09:28AM (#21928138)
I do a lot of making games just for the fun of it. Good advice I heard is that you're making a game _or_ a game engine. Pick which you care about most and focus on that.

I found that phaserjs is a good engine but you still have to do a ton of stuff that other engines give for free. Chiefly, the rich game dev UI that Unity, Godot, Unreal, etc. all have. Don't be discouraged by the different languages. Programming is like 15% of game dev and you can easily make a fun first RPG using examples and stack overflow, and get comfy with a new language along the way.

Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @10:51AM (#21928489)
Here’s socket server source btw: https://github.com/pakastin/car-ws
Re:Show HN: Simple 2D car physics with JavaScript by joemaller1 on Wednesday January 01, 2020 @09:31AM (#21928148)
Re:Show HN: Simple 2D car physics with JavaScript by eitland on Wednesday January 01, 2020 @02:47AM (#21927128)
That is totally amazing! Have played it for 5 minutes on my phone now :-)
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @02:58AM (#21927157)
Haha, great! Planning to add multiplayer support soon btw.. ;)
Re:Show HN: Simple 2D car physics with JavaScript by wruza on Wednesday January 01, 2020 @05:11AM (#21927450)
It would be nice to see something like cursors.io mmo. Sadly they abandoned it, as that dead stupid agar got more traction and ad views.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @05:44AM (#21927531)
Hmm..let’s see!
Re:Show HN: Simple 2D car physics with JavaScript by fjfaase on Wednesday January 01, 2020 @07:33AM (#21927794)
How long would it take to make the whole screen one color (excluding the red car)? And what is the best strategy: driving in circles or fast alternating between gas and brake?
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @08:19AM (#21927901)
Let's see :D
Re:Show HN: Simple 2D car physics with JavaScript by nkrisc on Wednesday January 01, 2020 @09:53AM (#21928233)
I see that it works on mobile but u can not for the life of me figure it what the control scheme is. I just touch the screen and the car and to randomly move.
Re:Show HN: Simple 2D car physics with JavaScript by mrspeaker on Wednesday January 01, 2020 @09:59AM (#21928260)
Figuring it out is part of the fun ;)

(But spoiler, it's just about moving relative to where you start touching... press then move up the screen to go straight.)

Re:Show HN: Simple 2D car physics with JavaScript by tomxor on Wednesday January 01, 2020 @11:03AM (#21928561)
I love how instantly hackable this is... clone the repo and serve the directory localy and still connects through websockets, no npm crap in the way. :)
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @12:44PM (#21929240)
Sorry, but I blocked connections outside main domain to keep it clean.. Started to see trains, moving sprites etc :D
Re:Show HN: Simple 2D car physics with JavaScript by tomxor on Wednesday January 01, 2020 @12:53PM (#21929304)
:( ah well, was fun while it lasted. I was the "HN" sprite :P
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @05:46PM (#21931276)
Looked fun, I was just worried about game performance and server bandwidth usage..

Both frontend and backend are open source and work straight away without bundling, if you want to fiddle around:

https://github.com/pakastin/car https://github.com/pakastin/car-ws

Pull requests also welcome! ;)

Re:Show HN: Simple 2D car physics with JavaScript by tomxor on Thursday January 02, 2020 @01:21PM (#21938072)
> Looked fun, I was just worried about game performance and server bandwidth usage..

Yes, that turned out to be a legitimate concern... later on I saw people go a step further and write entire sentences out of cars... they probably didn't realize but it was effectively DoSing your server and preventing anyone else's socket emits getting through so all you could see is the text.

I guess it's a difficult balance if you want to allow playful hacking but don't want to be DoSed. (BTW it's easy to bypass the origin restriction by just injecting code into the browser), perhaps a better restriction would be a max sockets per session or IP, that would still allow some hacking but reduce chance of DoS intentional or not.

Anyway I changed tack to try implementing some basic bots in smaller numbers. Was fun to see how people interact with them.

Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @11:33AM (#21928759)
Vanilla magic! :P
Re:Show HN: Simple 2D car physics with JavaScript by trekhleb on Wednesday January 01, 2020 @05:21AM (#21927471)
The physics is really nice! Played with it for couple of minutes and even managed to drift/draw "2020" trace on the ground :D
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @05:43AM (#21927530)
Haha, nice!
Re:Show HN: Simple 2D car physics with JavaScript by mechhacker on Wednesday January 01, 2020 @09:33AM (#21928152)
That's pretty cool! Just fooling around with this made me think about replicating it with an Elixir backend.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @11:33AM (#21928767)
Shouldn’t be too hard, current backend is here: https://github.com/pakastin/car-ws :D
Re:Show HN: Simple 2D car physics with JavaScript by 40four on Wednesday January 01, 2020 @08:41AM (#21927951)
Great work @pkstn! Works surprisingly well on a touchscreen. Really enjoyed deck of cards as well :)
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @08:49AM (#21927977)
Thank you! :P
Re:Show HN: Simple 2D car physics with JavaScript by n-gauge on Wednesday January 01, 2020 @09:10AM (#21928069)
Good work! Q: How well would multiplayer work if you used webRTC data sockets instead?
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @10:52AM (#21928494)
Not sure, since you’d need multi to multi connection, not only single to single..
Re:Show HN: Simple 2D car physics with JavaScript by smcl on Wednesday January 01, 2020 @06:22AM (#21927626)
I spent more time than I care to admit zooming around on this page
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @07:23AM (#21927764)
Haha :)
Re:Show HN: Simple 2D car physics with JavaScript by kmjoshi on Wednesday January 01, 2020 @05:33AM (#21927501)
This is very cool and fun! and its only one page of js code!
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @05:43AM (#21927528)
Yeah, very simple..
Re:Show HN: Simple 2D car physics with JavaScript by kmjoshi on Wednesday January 01, 2020 @06:19AM (#21927618)
haha
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @08:49AM (#21927980)
Wasn’t meant as joke, it actually is quite simple demo :D
Re:Show HN: Simple 2D car physics with JavaScript by wahlis on Wednesday January 01, 2020 @05:56AM (#21927569)
Wonderful! Please add some sounds for drifting :)
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @07:23AM (#21927765)
Great idea! :P
Re:Show HN: Simple 2D car physics with JavaScript by cabaalis on Wednesday January 01, 2020 @10:50AM (#21928473)
Networked spirograph simulator :)
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @10:55AM (#21928509)
Haha :D
Re:Show HN: Simple 2D car physics with JavaScript by alexchamberlain on Wednesday January 01, 2020 @04:17AM (#21927346)
This is nice. It would be cool to have an option of rendering the controls under my thumb?
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @04:35AM (#21927369)
True that, I’ll add those!
Re:Show HN: Simple 2D car physics with JavaScript by Spacemolte on Wednesday January 01, 2020 @03:56AM (#21927294)
Nice, but I do feel like it's missing a set of tracks from the other set of wheels.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @04:36AM (#21927373)
Yeah, let’s see how it evolves!
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @08:19AM (#21927893)
Multiplayer is live now!!! :D
Re:Show HN: Simple 2D car physics with JavaScript by driver2020 on Wednesday January 01, 2020 @08:26AM (#21927914)
At least on Chrome on Android it is now broken for me (previously working fine), as dragging to steer scrolls the page.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @09:03AM (#21928038)
Should work now?
Re:Show HN: Simple 2D car physics with JavaScript by planetis on Wednesday January 01, 2020 @04:12PM (#21930675)
Nice, can we get tracks too?
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @04:52PM (#21930976)
Sounds good! :P
Re:Show HN: Simple 2D car physics with JavaScript by marius_k on Wednesday January 01, 2020 @10:13AM (#21928300)
I like it how those patterns reveal different screen sizes/resolutions.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @10:51AM (#21928483)
Haha, true :D
Re:Show HN: Simple 2D car physics with JavaScript by ashisrai_ on Wednesday January 01, 2020 @05:59AM (#21927574)
Drifting is awesome.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @07:23AM (#21927766)
It is! :P
Re:Show HN: Simple 2D car physics with JavaScript by jakciewell on Wednesday January 01, 2020 @05:44AM (#21927532)
it's pretty cool and fun
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @05:54AM (#21927563)
I’ll add multiplayer soon, hope it’s even more fun then!
Re:Show HN: Simple 2D car physics with JavaScript by citrons on Wednesday January 01, 2020 @08:52AM (#21927992)
Nice!
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @09:00AM (#21928032)
Thanks! :)
Re:Show HN: Simple 2D car physics with JavaScript by djmips on Wednesday January 01, 2020 @03:52AM (#21927276)
It's fun! Nice work.
Re:Show HN: Simple 2D car physics with JavaScript by pkstn on Wednesday January 01, 2020 @04:35AM (#21927371)
Thanks!