android - How do I apply a force to a body in the direction it is traveling (Box2D)? -


i'm using andengine/box2d develop game. have ball bounces around screen. i've made ignore gravity applying opposite force, has tenancy slow down after initial impulse, elasticity set 1. want to:

if(speed < a number) apply force or impulse (which better?) in direction of motion

how might this?

well unfortunately, ball interacting other objects setting velocity did not work, have found solution!

using forces , extensive trig, came 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);     } } 

essentially create body , apply impulse moving (worked lot better force reason). on every update, force applied opposite of gravity in order keep ball aloft (floating, essentially). elasticity set 1, collisions elastic. , tricky part: calculated x , y velocities (vx , vy respectively) , used these calculate resultant velocity (vr) , angle traveling in (t).

if vr less velocity want (destroyervelocity), force applied bump destroyer velocity. since f=mv/t , used t=1, force applied in 1 direction equal wanted velocity - actual velocity * x/y (that's cos/sin) * mass of object. if object traveling in positive x direction , negative y direction, force of (x,-y) applied, , on.

in order keep velocity close destroyervelocity possible, had apply force in opposite direction if happened greater destroyervelocity. done in same manner, opposite force.

run log statement of resultant velocity (with destroyervelocity being 7) , reads like: 6.900001, 6.9995001, 7.00028, 7.13005,...

although ball spike 15 or 20 sometimes, takes 2 or 3 loops within +- .5 of 7, enough me! hope helps else looking same thing.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -