Point(planet) gravity, constant speed

Discuss issues specific to the Flash/AS3 port of Box2D
khali
Posts: 1
Joined: Fri Aug 08, 2014 3:31 am

Point(planet) gravity, constant speed

Postby khali » Fri Aug 08, 2014 3:42 am

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? :)

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Point(planet) gravity, constant speed

Postby irresistible force » Fri Aug 08, 2014 7:21 am

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


Return to “Flash”



Who is online

Users browsing this forum: No registered users and 2 guests