問題描述
在 2D 汽車遊戲中模擬下壓力 (Simulate Downforce in a 2D Car Game)
我正在嘗試為 2d 汽車遊戲添加下壓力,以給人一種“在軌道上”的感覺。但我在掙扎。我使用 box2d 作為物理引擎,我可以創建一輛有點像小行星遊戲中的船移動的汽車,但我不確定我需要添加哪些額外的力量以及在哪裡(以及何時)添加它們?目前,當我加速時我正在施加一個向前的力,當我轉彎時我將汽車旋轉一個角度。
那麼,如果你以下面的小行星克隆為例,我將如何模擬下壓力效應?或者甚至在“在軌”之間進行調整。和“在冰上”?
https://piqnt.com/planck。 js/小行星
我已經嘗試為像下面這樣的簡單 cos/sin 數學放棄 box2d,這確實得到了我想要的效果,但我希望使用 box2d 來增加更多的可控性。另外,我想調整下壓力的水平:
this.velocity.x += Math.cos(this.angle.rad()) * this.acceleration;
this.velocity.y += Math.sin(this.angle.rad()) * this.acceleration;
this.position.add(this.velocity);
任何幫助或指導將不勝感激。非常感謝,J。
參考解法
方法 1:
Well, after about a month of searching! I found the answer in this video (at the 32min mark): https://youtu.be/0Quv9U9_a8c
As I suspected, it was a rather simple fix. I needed to remove the sideways velocity using dot product. The guy in the video explains it much better than me and also demonstrates exactly what i'm looking for.
As a bonus for me he also demonstrates tweaking the slide from full grip ("on rails") to really slippery ("on ice").