Point(planet) gravity, constant speed

Hi!

I'm creating something similar to planet gravity. I have a body and I'm calculating vector from it's position to center of the screen, then I'm applying force to my body giving that vector. It's working, my body moves towards center of the screen. Now I want it to move around so I added second force that is perpendicular to previous calculated vector. And it's working too.

What I want to do is to move my body at constant speed, always. Right now it simulates physics so it accelerates when it's moving towards center of the screen and decelerating when it's moving in other direction. I want it to move with constant speed towards center of my screen, without acceleration.

My code looks like this (pseudocode based on C++/Java):

Code: Select all

    vec2 bodyPosition= body.getPosition();
    vec2 screenCenter = new vec2(ScreenWidth/2, ScreenHeight/2);
    vec2 vectorToCenter = screenCenter - bodyPosition;
    vec2 perpendicularVector = new vec2(screenCenter.x, -screenCenter.y);
    body.applyForceToCenter(perpendicularVector); // move body around
    body.applyForce(vectorToCenter, bodyPosition); //move body towards center of the screen


Any suggestions? 🙂
khali wrote:I want it to move with constant speed towards center of my screen, without acceleration.

So you mean, it should move to the center of the screen and stop?

If so, this might be useful:
http://stackoverflow.com/questions/2451 ... n/24519641

Somehow I think that is not what you mean, but your description is a bit confusing. For example, if you want it to move to the center of the screen, why would you apply the perpendicular force... ?

Original thread: https://box2d.org/forum/viewtopic.php?t=9871