Libgdx Box2D bounds - libgdx

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?

Related

libgdx - moving a KinematicBody in a circular path

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.

How to pan openseadragon mutli image viewer within the bounds

I have a multi image openseadragon viewer, where the coordinates of each tiled image seem to be calculated based on the size of the image to the container viewer size. When the user clicks on a specific image I am trying to fit the image to the center of the viewport by using fitBounds method. Here I am passing the bound rect of the image the user has clicked. This is working perfectly. Now I have a requirement where say a panel is placed on the right side, on top of the viewer, I need to consider the boundary till the left of the panel(Viewer area includes the area under the right side panel). What this means is that, though the viwer need to occupy full width, the image that need to be fit should concider only the center between the left of the viewer upto left of the panel(excludes the area under the panel). I tried doing PanTo but couldn't understand how to calculate the center for a multi image scenario.It doesn't seem to be accurate. The x coordinate of the 4th image seem to be at 4.9 and am trying to see how I can calculate a correct coordinate center for each image where I can panTo. Can someone point me in the right direction?

Make stage draggable, but keep a specific shape always in the same relative position (KineticJS)

does anybody know, how to drag the stage "behind" a certain shape or shape-group, so the shape doesn't get dragged around with all the other items on the stage and of course, the stage itself?
In my example I have a stage with many items outside of the visible part of the<canvas>-tag which can only be reached by dragging the stage to the left/right, but one of the shapes should be always on position (0, 0) of the <canvas>-element and shouldn't change its position while the stage is dragged.
I couldn't find a way to do this yet, so I would be very glad if anyone knows a way to get this working.
If the one shape left at (0,0) of the canvas is always the same shape and never changes, you can add all other elements to a group excluding the shape that is positioned at (0,0) and drag the group instead of dragging the stage.
If you must drag the stage, then you'll have to calculate the distance of x that the mouse has moved while dragging, and apply that to the position of the shape at (0,0).

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.

How to prevent external translation of a movieclip object on stage in AS3?

I have a MovieClip object, which is exported for actionscript (AS3) in an .swc file.
When I place an instance of the clip on the stage without any modifications, it appears in the upper left corner, about half off stage (i.e. only the lower right quadrant of the object is visible). I understand that this is because the clip has a registration point which is not the upper left corner.
If you call getBounds() on the movieclip you can get the bounds of the clip (presumably from the "point" that it's aligned on) which looks something like (left: -303, top: -100, right: 303, bottom: 100), you can subtract the left and top values from the clip x and y:
clip.x -= bounds.left;
clip.y -= bounds.top;
This seems to properly align the clip fully on stage with the top left of the clip squarely in the corner of the stage.
But! Following that logic doesn't seem to work when aligning it on the center of the stage!
clip.x = (stage.stageWidth / 2);
etc...
This creates the crazy parallel universe where the clip is now down in the lower right corner of the stage.
The only clue I have is that looking at:
clip.transform.matrix
and
clip.transform.concatenatedMatrix
matrix has a tx value of 748 (half of stage height) ty value of 426 (Half of stage height)
concatenatedMatrix has a tx value of 1699.5 and ty value of 967.75
That's also obviously where the movieclip is getting positioned, but why? Where is this additional translation coming from?
I believe that I have solved the problem.
It seems that when the stage is not set to specific dimensions, the scale of the items (or this particular vector object in my case) on the stage is relative to some preset scale/dimensions (The origin of which I'm not sure -- with some testing it looks like it is affected by the ratio and dimensions of the stage).
Increasing the size of the stage increases that scaling value, which through some set of concatenations of matrices results in the movieclip getting scaled and translated at that same percentage (as the stage), even if you explicitly set the x/y/width/height values.
(Note: not just resizing dynamically. Resizing the window, closing and re-opening the window at that new size produces the same behavior)
I suspect this only happens with vector objects?
Anyway, the fix is to set the stage to an explicit width and height. Then the translation and scaling of the object appear to use an identity parent matrix, and everything works like you'd expect.