[英]How to slow down box2d body linear or angular velocity
我有一個圓形動態的身體,模擬一個彈跳球,我將恢復原狀設置為2,它只是失去控制,它不會停止上下彈跳。 所以我想使用阻尼減慢球的線性或角速度。
if(ball.getLinearVelocity().x >= 80 || ball.getLinearVelocity().y >= 80)
ball.setLinearDamping(50)
else if(ball.getLinearVelocity().x <= -80 || ball.getLinearVelocity().y <=-80)
ball.setLinearDamping(50);
當球的線速度達到80或以上時,我將其線性阻尼設置為50,然后它就會超級慢動作。 有人可以解釋一下Damping是如何工作的以及如何正確使用.setLinearDamping()
方法,謝謝。
編輯
這就是我所做的,如果線速度超出了我的需要,它將球線性阻尼設置為20,如果不總是設置為0.5f。 這會產生並影響重力不斷變化的瞬間變化。 但是@minos23的答案是正確的,因為它更自然地模擬球你只需要設置你需要的MAX_VELOCITY。
if(ball.getLinearVelocity().y >= 30 || ball.getLinearVelocity().y <= -30)
ball.setLinearDamping(20);
else if(ball.getLinearVelocity().x >= 30 || ball.getLinearVelocity().x <= -30)
ball.setLinearDamping(20);
else
ball.setLinearDamping(0.5f);
這是我用來限制身體速度的方法:
if(ball.getLinearVelocity().x >= MAX_VELOCITY)
ball.setLinearVelocity(MAX_VELOCITY,ball.getLinearVelocity().y)
if(ball.getLinearVelocity().x <= -MAX_VELOCITY)
ball.setLinearVelocity(-MAX_VELOCITY,ball.getLinearVelocity().y);
if(ball.getLinearVelocity().y >= MAX_VELOCITY)
ball.setLinearVelocity(ball.getLinearVelocity().x,MAX_VELOCITY)
if(ball.getLinearVelocity().y <= -MAX_VELOCITY)
ball.setLinearVelocity(ball.getLinearVelocity().x,-MAX_VELOCITY);
請在render()方法中嘗試此代碼,它將限制您正在制作的球體的速度
祝好運
你的代碼設置了一個強大的線性阻尼,但從不釋放它,因此當球達到特定的速度時,它將切換到它的行為就像膠合一樣。
我希望通過在每個幀中檢查並重置它來限制最大速度:
float maxLength2 = 80*80;
Vector2 v = ball.getLinearVelocity();
if (v.len2() > maxLength2) {
v.setLength2(maxLength2);
}
當物體移動不太快時,線性阻尼模仿稱為阻力的現象。 它基本上由每個時刻的以下等式描述:
dragInducedForce = -dragCoefficient * velocity;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.