Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8168193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:26:23+00:00 2026-06-06T20:26:23+00:00

I’m using AndEngine/Box2d to develop a game. I have a ball that bounces around

  • 0

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

How might I do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:26:25+00:00Added an answer on June 6, 2026 at 8:26 pm

    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);
        }
    }
    

    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).

    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.

    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.

    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,…

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.