How to rotate Box2d body? - java

I'm using libgdx to create my game and I use box2d as physics engine, I have a body with a polygon shape set as box .5x.5(1x1 meter) and I want to rotate it around it's center. I tried to do this:
shape.setAsBox(0.5f, 0.5f, new Vector2(0.25f, 0.25f), 0);
I'm not sure but I think that the Vector2 is used to set the origin of the shape, so the origin is half of 0.50x0.50 so it means it is in the center but it still doesn't work which means there something wrong with my code or my understanding on how to set the origin.

Two things you should try:
Check if the 3rd parameter is actually the origin.
Try setting the origin to 0.5, 0.5 instead. Maybe it's relative to the size.

Related

LibGDX: Where is the X-/Y-Coordinate of a camera?

I'm wondering how the orthographic camera in LibGDX is positioned.
Is X bottom left or center or on right(etc)? And how it is with the Y?
I know its a simple question, but I'm messing around with my cam at this moment and I need some help :D
Libgdx camera coordinates is always CENTER of your screen.
For example if your viewportWidth and viewportHeight like
(800, 480)
it's coordinates will be
(400, 240)
In LibGDX, we have lots of coordinate systems (not only LibGDX, this applies to other engines/frameworks too). Camera is also a game object like other objects and thus is positioned like other objects.
The only difference about cameras is that they don't have width and height in the same sense of other objects. They are always a zero size point and can capture a rectangle (which is called viewport) centered on this point.
In your game if you use only one camera, what you see is the viewport that the only existent camera captures. So, if a sprite is on (0, 0) and also your camera is on (0, 0), you'll see that sprite exactly on the center of your screen.
Here's an example project using orthographic camera.

Android translated canvas collision of rectangles

im trying do develop a Zelda like game. So far i am using bitmaps and everything runs smooth. At this point the camera of the hero is fixed, meaning, that he can be anywhere on the screen.
The problem with that is scaling. Supporting every device and keeping every in perfect sized rects doesnt seem to be that easy :D
To prevent that i need a moving camera. Than i can scale everything to be equally sized on every device. The hero would than be in the middle of the screen for the first step.
The working solution for that is
xCam += hero.moveX;
yCam += hero.moveY;
canvas.translate(xCam,yCam);
drawRoom();
canvas.restore();
drawHero();
I do it like this, because i dont wand to rearrange every tile in the game. I guess that could be too much processing on some devices. As i said, this works just fine. the hero is in the middle of the screen, and the whole room is moving.
But the problem is collision detection.
Here a quick example:
wall.rect.intersects(hero.rect);
Assuming the wall was originally on (0/0) and the hero is on (screenWitdh/2 / screenHeight/2) they should collide on some point.
The problem is, that the x and y of the wall.rect never change. They are (0/0) at any point of the canvas translation, so they can never collide.
I know, that I can work with canvas.getClipBounds() and then use the coordinates of the returned rect to change every tile, but as I mentioned above, I am trying to avoid that plus, the returned rect only works with int values, and not float.
Do you guys know any solution for that problem, or has anyone ever fixed something like this?
Looking forward to your answers!
You can separate your model logic and view logic. Suppose your development dimension for the window is WxH. In this case if your sprite in the model is 100x100 and placed at 0,0, it will cover area from 0,0 to 100, 100. Let's add next sprite (same 100x100 dimension) at 105,0 (basically slightly to the right of the first one), which covers area from 105,0 to 205,100. It is obvious that in the model they are not colliding. Now, as for view if your target device happens to be WxH you just draw the model as it is. If your device has a screen with w = 2*W, h = 2*H, so twice as big in each direction. You just multiply the x and y by w / W and h / H respectively. Therefore we get 2x for x and y, which on screen becomes 1st object - from 0,0 to 200, 200, 2nd object - from 210,0 to 410, 200. As can be seen they are still not colliding. To sum up, separate your game logic from your drawing (rendering) logic.
I think you should have variables holding the player's position on the "map". So you can use this to determine the collision with the non changing wall. It should look something like (depensing on the rest of your code):
canvas.translate(-hero.rect.centerX(), -.rect.centerY());
drawRoom();
canvas.restore();
drawHero();
Generally you should do the calculations in map coordinates, not on screen. For rendering just use the (negative) player position for translation.

LibGDX Sprite + Box2D CircleShape: Positioning while taking into account origin

My problem is, that my Box2D Body has another position than the LibGDX Sprite I want to render for that body. In my render-loop, for each body, I'm setting the position of it's sprite to the one of the body, and then rendering it.
When creating a Box2D Shape other than a circle, Box2D does not move it from it's origin, and neither does the LibGDX-sprite. If I now move or set the position of the body, my sprite will always follow it. But, unfortunately, this is not possible with CircleShapes: Since LibGDX's Sprite#setPosition does not take into account the origin of the sprite (Which is only used for scaling and rotating), the sprite is set by it's lowerleft corner. So here is the problem: The Box2D CircleShape is moved by taking into account the origin! So my sprite always starts in the origin of the shape. Does anyone know how to fix that? And, ultimately, I'd want to always move both while taking into account the origin. How do I do that?
Box2d body origin is never changed. The Circle and Box shape's origin is middle and the polygon shape's origin is the bottom left corner.
The only way to fix it, you change the sprite origin to middle that is Sprite.setOriginCenter();.
If body is a circle or box shape, the sprite position set like as sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);.
If body is a polygon shape the code should be like as sprite.setPosition(body.getPosition().x, body.getPosition().y);.

Sprite Rotation around a point Andengine/Java

Does anyone know how can I get a sprite/bitmap to rotate by a certain angle where the point on one the sides will always remain in the same position.
I have a blue line in the image and I need one of the sides to remain in the same position at all times.
It would be great help if somebody could tell me how to do this.
Im trying to do this in Andengine/Java. So a solution in either would be most helpful.
Thanks
Regards,
Yaro
The AndEngine has a good method called setRotationCenter. :) By default rotation center set to center of sprite (half of width, half of height). You can easily change this point.
The AndEngine is quite limited with regard to rotation and positioning, I've faced a similar problem on my own, and the only way to solve it was overriding methods and extending classes.
I'd recommend you to implantation the a new method which will set the rotation axis, and override the setRotation() method, to use this axis, I guess you could figure out how to rotate the sprite on a different axis by changing its position which changing its rotation
I just find out a solution for rotate a sprite at any center point. First you create a virtual entity and attach your sprite into this entity (notice: position of sprite is relative with your entity), then you move rotation center of the sprite (default at center of image) to overlap the rotation center of the entity. Final, you set rotation for the entity instead of the sprite. Hope this helpful ^^

LWJGL / OpenGL Rendering Questions (Center Render/Bending Polygons)

So I am creating a simple 3d render engine in the LWJGL, and I am having issues with the fact that when I back up from an object, it travels to the left and upwards (towards 0,0) when I really need it to travel to screenResX/2,screenResY/2 (maybe 0 I dont know). Is there a way that I can set some kind of variable that will let it fade towards the center?
Additionally, In this engine, I need to bend the polygons to render as if the player was looking at them. At the moment, I only have polygons rendering if you are looking straight at them, and I know that that's not what is needed, can anyone help me?
Thanks in advance for your help, this is my first 3d engine and I'm pretty clueless.
So for the first question: Scaling always takes place around the origin. Meaning that you have to translate your object to the origin, scale it and then translate it to its destination. This is better explained here: OpenGL: scale then translate? and how?
But the basics are something like this (from the linked question):
//this moves the scaled
glTranslatef(destCenter.x, destCenter.y, 0.0);
//scale to the desired factor
glScalef(scaleX, scaleY, 0.0);
//move the center of the scaling operation into the origin
glTranslatef(sourceCenter.x * -1.0, sourceCenter.y * -1.0, 0.0);
I still don't quite understand your second question, but I am guessing that you mean that you want something to be drawn in perspective rather than in an orthogonal fashion. Have a look at gluPerspective(...) or glFrustum(...) instead of glOrtho(...).

Categories

Resources