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 9176471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:06:54+00:00 2026-06-17T17:06:54+00:00

I try to make a very simple 2d physic functions to solve rigidBody with

  • 0

I try to make a very simple 2d physic functions to solve rigidBody with with sphere particles as colliders,

I have so a rigidBody and particle class :

    function _rigidBody () {
        this._position = cc.p(0.0, 0.0);
    this._angle = 0;
    this._linearVelocity = cc.p(0.0, 0.0);
    this._angularVelocity = 0.0;
    this._force = cc.p(0.0, 0.0);
    this._angularMoment = 0;
    this._mass = 1.0;

    this._particles = [];
    }
    function _particle () {
    this._initialPosition = cc.p(0.0, 0.0);
    this._relativePosition = cc.p(0.0, 0.0);
    this._worldPosition = cc.p(0.0, 0.0);
    this._linearVelocity = cc.p(0.0, 0.0);
    this._force = cc.p(0.0, 0.0);
    this._radius = 10.0;
    }

and so basically, each frame I compute the new position of each particles store in each rigidBody, then I check for particle to particle collision and in case of collision, I compute a force to repulse them (base on radius and how munch there are inside them), then I store this force in the particle,

And I compute the LinearVelocity and AngularVelocity by adding and impulse to my rigidBody for all the force store on all particle it contain.

my function addImpulse (with impulse the force store in particle and position the world pos of my particle) :

    this.AddImpulse = function(bodyIdx,impulse,position){
    var b = g_Bodies[ bodyIdx ];
    if(position == null){position = b._position;}

    b._linearVelocity.x += (impulse.x/ b._mass);
        b._linearVelocity.y += (impulse.y/ b._mass);
        b._angularVelocity +=  ((position.x - b._position.x) * impulse.y -                
                                (position.y - b._position.y) * impulse.x);
};

My problems is, this function works pretty fine for linearVelocity by for the angular velocity it seam to get really big at the most little collision,
for now, I don’t use any damping (linear or angular), and I don’t have inertia for my angularVelocity,

I guess the miss of inertia is my problem, but I get a little confuse when I look in internet how to compute it,

I look in box2d to see were the inertia is store and it seams to be a variable of rigidBody (as mass).
I find in internet that the Inertia = mass * (r*r)
problem => what is r …

-if is the distance for my gravity center (so the position of my rigidBody), is obviously equal to 0 all the time (body.position – body.position == 0 )

  • in box2d I found :
    this.m_I = massData.I – this.m_mass * (massData.center.x * massData.center.x +
    massData.center.y * massData.center.y);
    And massData.center seam to be the center of the shape from the rigidBody,
    problem in my case my shape is 2 sphere one at (0,10) and one at (0,-10), so the center of that is (0,0) ….

  • in an other website I found that :
    for(i < numberOfParticle)
    I += ( massOfParticle(i) * ( RelativePosOfParticle(i)*RelativePosOfParticle(i) )
    In this case is better, because as I square the distance of each particle (instead of searching the center first), my I will be greater than 0.
    But so I’m not sure is the correct formula to use …

If someone can explain me in a simple way, how to compute a Inertia ? If I can compute the inertia for my rigidBody directly (and not by particle) ? if I can store the inertia or if it deepens of where is apply the impulse (I read that also in an other website…) ?
and/or why can I do wrong in my function of impulse to get a such big angular velocity (maybe is normal and the damping solve this problems is the others physic engine).

And also if someone could answer (just by curiosity), does box2d use a simple version of the inertia because is quicker to compute and the result are good enough ? Or does the calculation of Box2d is correct, and I miss something when I read the code ?

Thanks a lot ^^,

Marius.

  • 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-17T17:06:55+00:00Added an answer on June 17, 2026 at 5:06 pm

    First: you are correct, the problem is that you do not consider the moment of inertia (which is the rotational analog of mass). Look at the code:

    b._linearVelocity.x += (impulse.x/ b._mass);
    b._linearVelocity.y += (impulse.y/ b._mass);
    b._angularVelocity +=  ((position.x - b._position.x) * impulse.y -
                                (position.y - b._position.y) * impulse.x);
    

    When you calculate linear velocity, you divide by mass; you should do something similar for angular motion.

    If the position of the rigidBody is its center of gravity, then the job will be easy.

    The moment of inertia is mr2, summed over the particles. That is, each particle contributes m*r*r, where m is the mass of the particle, and r is the distance from the particle to the center of gravity.

    For example, if your shape is 2 spheres, one at (0,10) and one at (0,-10), both of mass 5, then the center of gravity is at

    C = (5*(0,10)+5*(0,-10))/(5+5) = (0,0)
    

    and the moment of inertia is

    I = 5*10*10 + 5*(-10)*(-10) = 1000
    

    The change in angular velocity will be

    b._angularVelocity += (((position.x - b._position.x) * impulse.y -
                            (position.y - b._position.y) * impulse.x))/I;
    

    If your shape is 2 spheres, one at (0,10) with mass 5, and one at (0,-10) with mass 10, then the center of gravity is at

    C = (5*(0,10)+10*(0,-10))/(5+10) = (0,-10/3)
    

    and the moment of inertia is

    I = 5*(40/3)*(40/3) + 5*(-20/3)*(-20/3) = 10000/9
    

    EDIT:

    A problem appears in the special case of a single sphere (or collocated spheres). These spheres are treated as point masses, so the moment of inertia (I) will be zero. And all impulses are directed through the center of a sphere, so the torque (the angular “force”) will be zero. So there should be no angular velocity, but by the method above it will be 0/0. One solution: each sphere makes a small constant contribution to the inertial moment (this means that the sphere is not a point mass– it has a slight extent, so it can spin). Now the contribution of each sphere is m*r*r + d, where d is, e.g. 0.0001. So the lone sphere has a positive moment of inertia (and will not rotate), and all of the other objects still have realistic physics.

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

Sidebar

Related Questions

I have a very simple Java JTable question. I am creating this class to
I have a very simple Groovy class where I'm just trying to select a
I have a very simple Resource like this for my model 'Presentacion' class PresentacionResource(ModelResource):
I have a very simple class if(!isset($_GET['page'])) { $_GET['page'] = home; } class be_site
I am a beginner, I'm trying to make a very simple app, one button
I have a very simple app that has one CoreData Entity with a circular
We have a very simple ASP.Net page for uploading a file to our webserver.
Background I have written very simple BBCode parser using C# which transforms BBCode to
I'm trying to make a very simple 'counter' that is supposed to keep track
I want to have a very simple editor for my website that i wrote

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.