I have an actor on the screen, with a DragListener to move him around the stage, and a ClickListener. After a click on it i want to draw a rectangle around him (using ShapeRenderer), the important thing is that the rectangle position is updating after moving the actor. Works good, until I resize the window (I'm using ExtendViewport). The rectangle is not at the correct position (I'm taking the coordinates using Actor's getX() and getY() methods). Tried the Viewport's toScreenCoordinates() method but still it is not correct. Any help with this problem would be very appreciated.
Are you forgetting to update the projection matrix of your ShapeRenderer after resize? Something like this:
viewport.update();
shapeRenderer.setProjectionMatrix(viewport.getCamera().combined);
Related
I am working on a program to visualize an n-body particle simulation. To do this I have created a camera which is used to project particle positions unto the screen. Then, using a spritebatch these particles are rendered in the correct positions. However, for this to work, the origin of the coordinate system must be located at the bottom left of the screen. This is often not the case and instead the origin is located about a fourth of the screen up and to the left in all cases, and this changes when resizing the screen.
How do I guarantee the coordinate system origin is always at the bottom left of the screen, instead of elevated by a few hundred pixels in either axis?
Thank you.
If you decided to use sprite you should stick with proposed workflow and draw them with:
sprite.draw(batch);
I mean, what's the purpose using sprites if you are getting textures and drawing them?
When you draw a texture to screen at position (0,0) it means that it's bottom left corner will be at screen bottom left corner. That' is sprite's "hot-spot" or "origin" is at it's left bottom corner by default.
But you can change that with setOrigin method:
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/Sprite.html#setOrigin-float-float-
That is, origin is kinda "center" of the sprite, so i.e. if it rotates it will rotate around that spot. Also when you draw sprite at some coordinates sprites origin will be exactly at those coordinates. That way you don't have to adjust sprite drawing position.
Check on that page what methods you have for sprite class...
I fixed my problem. Instead of using:
sprite.draw(batch);
I used:
batch.setColor(color);
batch.draw(sprite.getTexture(),sprite.getX(),sprite.getY(),sprite.getWidth()*sprite.getScaleX(),sprite.getHeight()*sprite.getScaleY());
I do not know why this worked.
I made a sketch with the behavior I'm trying to get.
Sketch
So I want to display a rectangle in the middle of the screen and a triangle that covers part of the rectangle. The problem is, when I use a Stackpanethe rectangle is centered but the triangle as well and I can't move it to the bottom right position. When I use a Groupit is not centered. Is there any way to get my intended behavior?
Set alignment of your triangle. Use static setAlignment(Node child, Pos value) method:
StackPane.setAlignment(triangle, Pos.BOTTOM_RIGHT)
I've been trying to draw an image of a block with an eye on the screen. I want to be able to animate more than one texture in the same way, so drawing the animations by hand is not an option. The problem is that when I move the eyelid sprite higher up than completely closed, part of the eyelid shows above the expected bounds like this. Obviously, this is a problem as it looks quite unnatural. I'd like to either have an alternative solution to this problem, or be able to crop the eyelid's Sprite object to fit behind the rest of the image. The final image consists of the eyeball, pupil (as I want to be able to animate this too), eyelid, main body and the outlines, drawn in that order. The render function looks like this:
public void render(float delta) {
float height = this.eyelid.getHeight();
float eyeHeight = height*0.7f;
this.eyelid.setY(this.eye.getY()+(eyeHeight*((100-this.lid)/100f)));
batch.begin();
this.eye.draw(batch);
this.pupil.draw(batch);
this.eyelid.draw(batch);
this.main.draw(batch);
this.shade.draw(batch);
batch.end();
}
this.lid is the % of how closed the eye is, and the image for the eyelid itself can be found here. How could I solve this problem, or how could I crop the sprite? I don't want to have to reload the texture as a sprite every frame.
I think what you're looking for is the ScissorStack class, which is documented on the libGDX wiki. You can use this object to clip around the eye's frame so that the top of the lid doesn't show.
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 ^^
I am trying to "resize" my game view within the screen using Libgdx. Indeed I need to let a black bar on the bottom and the left side of the screen. So as if you resize a square using your MS Paint or Gimp, I would like to do so with my game stage. (I've tried to upload a figure to set my issue clearer but apparently Stackoverflow forbids it for me)
I think I am mixing things up with Stage size, the Camera size and the Viewport. Any advice would be greatly appreciated !
If you are using stage then you can try to replace camera view your own with other viewport dimensions.
Camera newCamera = new OrthographicCamera(300, 200);
//... here is some other camera initialization code
newCamera.update();//recalculate camera matrices
stage.setCamera(newCamera);
//after that you may need to update sprite batch projection matrix
_gameStage.getSpriteBatch().setProjectionMatrix(newCamera.combined);