如何使重力中心Libgdx与Box2D的重力、中心、Box2D、Libgdx

2023-09-06 06:44:57 作者:逢场作戏。

如何设置重力任何机构或屏幕中心?我想这就是逻辑。

How do I set gravity to any body or screen center? I wondering that's logic..

我有两个圆形的身体aBody是staticBody,bBod​​y是动态的,世界重力(0,0)

I have two circle body aBody is staticBody, bBody is dynamic and world gravity(0,0).

推荐答案

所有你需要做的是应用的力量,将模拟重力与屏幕中心(让我们想象一下,在中心,有非常非常重的物体会拉拉其他对象)。

All you have to do is to apply a force that will simulate gravity with center of screen (let's imagine that in the center there is very very heavy object that will be pulling other objects).

方程是众所周知的,容易实现 - 看这里看一下吧

The equation is well known and easy to implement - look here to read about it.

有公式,你只需要实现它,如:

Having equation you just have to implement it like:

Body body, centerBody;
Vector2 center = new Vector2(0, 0);
...

//in render method
float G = 1; //modifier of gravity value - you can make it bigger to have stronger gravity

float distance = body.getPosition().dst( center ); 
float forceValue = G / (distance * distance);

Vector2 direction = center.sub( body.getPosition() ) );

body.applyForce( direction.scl( forceValue ), body.getWorldCenter() );

当然,你可以修改的重心与修改的中心 Vector2。