问题描述
我使用的是AndEngine / Box2D的开发一款游戏。我有一个球的弹跳在屏幕上。我已经成功地使它无视重力通过施加相反的力,但它有一个租赁的初始冲动后慢,甚至与弹性设置为1。基本上我想:
I'm using AndEngine/Box2d to develop a game. I have a ball that bounces around the screen. I've successfully made it ignore gravity by applying an opposite force, but it has a tenancy to slow down after the initial impulse, even with the elasticity set to 1. Essentially I want to:
如果(速度< 若干的)在运动方向上施加的力或脉冲(哪个更好?)
if(speed < a number)apply force or impulse (which is better?) in direction of motion
我怎么可以这样做?
推荐答案
好可惜,球与其他对象,因此设定的速度没有工作的互动,但我已经找到了解决办法!
Well unfortunately, the ball is interacting with other objects so setting velocity did not work, but I have found a solution!
使用力量和相当广泛的触发,我终于想出了:
Using forces and fairly extensive trig, I finally came up with:
private static class Ball extends Sprite {
Body body;
public Ball(final float pX, final float pY, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
body = PhysicsFactory.createCircleBody(mPhysicsWorld, this, BodyType.DynamicBody, PhysicsFactory.createFixtureDef(0, 1, 0));
body.applyLinearImpulse(((float)5),(float) 5, body.getWorldCenter().x, body.getWorldCenter().y);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this, body, true, true));
scene.attachChild(this);
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
body.applyForce(new Vector2(0,-SensorManager.GRAVITY_EARTH), new Vector2(body.getWorldCenter()));
float vx = body.getLinearVelocity().x, vy = body.getLinearVelocity().y, vr=(float) Math.sqrt(vx*vx+vy*vy);
float t= (float) Math.atan(Math.abs(vy/vx));
if(vr<destroyerVelocity){
if(vx>0&&vy<0)
body.applyForce((float) ((destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) (-(destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
else if(vx>0&&vy>0)
body.applyForce((float) ((destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) ((destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
else if(vx<0&&vy>0)
body.applyForce((float) (-(destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) ((destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
else if(vx<0&&vy<0)
body.applyForce((float) (-(destroyerVelocity-vr)*Math.cos(t)*body.getMass()), (float) (-(destroyerVelocity-vr)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
}
if(vr>destroyerVelocity){
if(vx>0&&vy<0)
body.applyForce((float) (-(vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) ((vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
else if(vx>0&&vy>0)
body.applyForce((float) (-(vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) (-(vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
else if(vx<0&&vy>0)
body.applyForce((float) ((vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) (-(vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
else if(vx<0&&vy<0)
body.applyForce((float) ((vr-destroyerVelocity)*Math.cos(t)*body.getMass()), (float) ((vr-destroyerVelocity)*Math.sin(t)*body.getMass()), body.getWorldCenter().x, body.getWorldCenter().y);
}
super.onManagedUpdate(pSecondsElapsed);
}
}
从本质上讲这是什么做的是建立一个机构,并应用一种冲动,它得到它移动(做了很多好于某种原因力)。在每次更新时,施加力相反的重心,以保持球高空(浮动,本质上)。弹性被设置为1,所以碰撞是几乎完全弹性。现在棘手的问题:我计算x和y速度(分别为VX和VY),并利用这些计算得到的速度(VR)和角度,他们的旅行(T)
Essentially what this does is create a body and apply an impulse to it to get it moving (worked a lot better than a force for some reason). On every update, a force is applied opposite that of gravity in order to keep the ball aloft (floating, essentially). The elasticity is set to 1, so collisions are almost perfectly elastic. And now the tricky part: I calculated the x and y velocities (vx and vy respectively) and used these to calculate the resultant velocity (vr) and the angle that they were traveling in (t).
如果vr为小于欲(destroyerVelocity)的速度,那么一个力施加到撞它备份到破坏者速度。由于F = MV /吨,我只是用T = 1,在一个方向上施加的力等于想要的速度 - 实际速度* X / Y方向(这是COS / SIN)*物体的质量。如果对象是行驶在正x方向和负y方向,则(X,-y)的施加力,并依此类推。
If vr is less than the velocity I want (destroyerVelocity), then a force is applied to bump it back up to the destroyer velocity. Since F=mv/t and I just used t=1, the force applied in one direction is equal to the wanted velocity - the actual velocity * the x/y (that's the cos/sin) * the mass of the object. If the object is traveling in the positive x direction and negative y direction, then a force of (x,-y) is applied, and so on.
为了保持速度尽可能靠近destroyerVelocity越好,我必须施加力在相反的方向,如果它发生在大于destroyerVelocity。这是在以相同的方式进行,只是具有相反的力
In order to keep the velocity as close to the destroyerVelocity as possible, I had to apply a force in the opposite direction if it happened to be greater than the destroyerVelocity. This was done in the same manner, just with an opposite force.
运行合成速度的日志报表(与destroyerVelocity为7),它读取类似:6.900001,6.9995001,7.00028,7.13005,...
Run a log statement of the resultant velocity (with the destroyerVelocity being 7) and it reads something like: 6.900001, 6.9995001, 7.00028, 7.13005,...
虽然球会秒杀像15或20,有时,它通常只需要2个或3环内+得到它 - .5 7,这是对我不够好!希望这可以帮助别人寻找同样的事情。
Although the ball will spike to like 15 or 20 sometimes, it usually only takes 2 or 3 loops to get it within +- .5 of 7, which is good enough for me! Hope this helps anyone else looking for the same thing.
这篇关于我该如何申请一个力于一身的它行驶(Box2D的)的方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!