libgdx - moving a KinematicBody in a circular path - libgdx

I'm trying to achieve a circular movement of KinematicBody around another body. I tryied this function:
public void updateCircular(float speed, Vector2 center){
Vector2 radius = center.cpy().sub(this.body.getPosition());
Vector2 force = radius.rotate90(1).nor().scl(speed);
this.body.setLinearVelocity(force.x, force.y);
}
But it's work fine only for DynamicBody connected with DistanceJoint. Changing body type to KinematicBody changes the behavior of the body. The body ceases to move in a circular orbit and passes to a spiral motion (Constantly increases the radius as if the centrifugal force is consumed by it). I need as much as possible a perfect circular motion. How can I achieve this?
Sorry for bad English.

The easiest way, in my opinion, is to just create a distance joint between some static body and your kinematics body. This way the body stays a certain radius from the other(creates a circle) and then you could just use set velocity to change its speed.

If the rotating body has to be a kinematic body, then to get uniform circular motion with fixed radius, we can add an extra velocity component perpendicular to the velocity vector. We can calculate the difference in distance of rotating body with the center. The velocity can be calculated by dividing this distance by time step per frame. This will move the body towards or outwards from center whenever rotating body is going outwards or coming inwards to the center. Add the below velocity to your calculation.
float dst=rotatingBody.getPosition().dst(pivotBody.getPosition());
//radius is the distance of center to the rotating body
float delta=dst-radius ;
//dt is time step per frame
float c2=delta/(dt);
Vector2 centripetalVector=new Vector2(pivotBody.getPosition()).sub(rotatingBody.getPosition()).nor().scl(c2);
I have written a tutorial on how to achieve a rotating shield around a moving dynamic body as center in libgdx. It can be found here along with the full code.

Related

Libgdx Box2D bounds

I have a body that during the game will be falling and can be moved left and right, but it needs to stay within the camera's bounds (basically not going off the screen). The camera follows the Y position of the body but stays fixed on 0 for it's X position. What is the best method to prevent the body from going outside of these bounds?

AS3 spin text around center?

I am trying to get an effect like this:
http://www.welcomeanimations.com/welcome_animated_gifs_rotating_sign_orange_chrome_k_1.htm
I have tried all sorts of things:
Matrix translation/rotation - spins the text around the 'Z' axis, instead of 'Y'
Adding TextField to a sprite, and Sprite.rotationY++: reg. point is upper left corner
Adding to MovieClip - same as above (an article said MovieClip's reg. point was centered).
This should be trivial?!?! Help me stackoverflow, you're my only hope!
So you have to remember, Display objects scale and rotate around their local coordinate system. so when you put a textfield in a sprite, you need to center it in that sprite's coordinate system. And doing that for textfields is annoying because their width/height isn't always accurate but there is trick for that: get visual bounds, but normally you can take half of somethings width and height
I've created a prototype for you on wonderfl so you can see the solution working in action. Click on the blue square to see how the local coordinate system messes with the rotation
Finally as you use thing you might find things not rotating in 3D space quite right, this should be able to fix that.

HTML5/Javascript: how to get real size/position of a 3D transformed div?

I wonder if someone can help me resolving this problem:
I have a DIV,
I apply 3d transform on it to rotate it and translate it in the 3d space.
Now: how can I get the REAL (on screen, 2D) position and size of that transformed DIV?
thanks all

scaling and positioning with tweenlite

I have a movieclip with the mesurements of a rectangle. When the application is launched the movieclip is being scaled before placed on the stage as following.
menu.width = 400;
menu.scaleY = menu.scaleX;
this is a smaller size than the original.
the position of the movieclip at this moment is in the middle on the x and top of the stage on the y.
when i click iti would like to do a tween with tweenlite wich scales it to its original(bigger) width and height and position it in the center of the stage on x and y.
the problem is when i position it with tweenlite, it gets done according to its old scale and not according to its new(bigger) scale so the movieclip isnt placed in the exact center of the stage.
Anyone know how i can resolve this?
I tried to compensate by adding a bigger number on the position so it gets positioned in the right way.
But when i click it again i would like it to rescale to its beginning scale and position so it would be very messy to compensate again. Is there any easier way to do this kind of tween?
I doubt that i'm being clear to what i want but i hope i am after all.
The easy and way of tween position and scale is probably to add the menu to a container.
You would then on one hand tween the position of the container, and on the other apply the scale to the menu it self without having to recalculate the proportional position if the scale changes.
This way you can even control the registration point of the menu within the container. e.g. to always scale from the middle...
This can be done with a matrix as well if you want to avoid to stack objects, but honestly it can get really tricky whereas the container method is bullet-proof.

Falling object in Box2D should rotate due to centre of mass?

I'm trying to simulate a falling balloon in Box2DAS3. What is important is that balloon falls the such that the bottom part were you blow it up rotates towards the bottom if it's knock sideways or is dropped at an angle.
alt text http://lh4.ggpht.com/_gjsCWAV_CZc/Sw7zqHahTJI/AAAAAAAAC3s/YIJka4AsM5s/s144/Untitled.jpg
I've tried offsetting the center of mass of the body and also joining two bodies together with the denser one representing the tie. In both cases the body falls at the same angle without rotating.
An object in free fall (vacuum), without any initial linear or angular velocity, will never start to spin by itself, no matter where its center of gravity lies. You need to simulate fluid drag and buoyancy. A simple way to this is to add a small force on the top of the balloon that points upwards.
Add a drag force to all objects. Something like -velocity / drag_amount