I am learning libgdx, I have passed around some of tutorials but I always face problems understanding the batch.draw() method
public void draw(Texture texture,
float x,
float y,
float originX,
float originY,
float width,
float height,
float scaleX,
float scaleY,
float rotation,
int srcX,
int srcY,
int srcWidth,
int srcHeight,
boolean flipX,
boolean flipY)
I read its documentation and still got confused of the following statement:-
The rectangle is offset by originX, originY relative to the origin.
WHAT DOES THIS MEAN?. Which origin are talking about here?
Also i did a simple sketch of what I visual understand the draw() method. Am I on the right path?
Thanks.
The rectangle is offset by originX, originY relative to the origin.
Scaling and Rotation performed around originX, originY.
batch.begin();
batch.draw(texture,300,200,50,50,100,100,1,1,0,100,100,50,50,false,false);
batch.end();
And Output is
Now rotate by 90 degree :
batch.draw(texture,300,200,50,50,100,100,1,1,90,100,100,50,50,false,false);
so rectangle offseted around centre by 90 degree
originX and originY is in the center of Rectangle so no offset appears in x,y
Let's put originX and originY at left bottom of rectangle and rotate by 90 degree
batch.draw(texture,300,200,0,0,100,100,1,1,90,100,100,50,50,false,false);
Related
I am trying to rotate my perspective camera around my model. The model is at the centre (0,0,0) point. This is my rotate camera method:
private void rotateCameraAroundModel() {
camera.position.set(0,0,0);
camera.position.rotate(Vector3.Y, 5);
camera.position.add(0f, 0f, 200f);
camera.up.set(Vector3.Y);
camera.lookAt(0,0,0);
camera.update();
}
I am trying to go to the centre, rotate by 5 degrees, then return to the same distance away. However, the rotate doesn't seem to be working and I can't figure out why, any help is greatly appreciated.
I figured it out :)
private void rotateCameraAroundModel(float angle) {
camera.position.set(centreX, centerY, centerZ);
camera.rotate(Vector3.Y, (float) Math.toDegrees(angle)); // Rotate around Y axis by angle in degrees
float x = (float) (centerX + radius * (Math.sin(totalPhi)));
float z = (float) (centerZ + radius * (Math.cos(totalPhi)));
camera.position.add(x, 0f, z); //Move back out 2m using pythagorean theorem to calculate the position on the circle
camera.up.set(Vector3.Y);
camera.lookAt(0, 0, 0);
camera.update();
}
So I needed to calculate the new position on the circle whilst rotating, which I did using some basic trigonometry
Here i'm load texture and make textureregion and TiledDrawable instances:
textures = new Texture(Gdx.files.internal("somefile.png"));
bg_grass_region = new TextureRegion(textures, 631, 175, 116, 662);
bg_grass_tiled = new TiledDrawable(bg_grass_sprite);
and inside render method of Screen instance i draw it like this:
batch.begin();
bg_grass_tiled.draw(batch, 0, 0, bg_grass_sprite.getWidth(), bg_grass_sprite.getHeight()*3);
batch.end();
The problem is that bg_grass_region is TextureRegion instance, and it complitly won't to scale...
So, i have a one simple question: How to scale/resize bg_grass_tiled or bg_grass_region objects?
TiledDrawable draws a TextureRegion repeatedly to fill the area, instead of stretching it. Implemented by TransformDrawable, however It not support scaling and rotation.
so you can't use below method of TiledDrawable, it throws UnsupportedOperationException
draw (Batch batch, float x, float y, float originX, float originY, float width, float height, float scaleX,float scaleY, float rotation)
Why don't you use draw method of SpriteBatch with TextureRegion.
draw (TextureRegion region, float x, float y, float originX, float originY, float width, float height,float scaleX, float scaleY, float rotation);
I'm trying to rotate an image over its x axes but its not working
Texture one = new Texture(Gdx.files.internal("img/one.jpg"));
oneImg = new Image(one);
oneImg.setOrigin(oneImg.getWidth() / 2, oneImg.getHeight() / 2);
oneImg.setPosition(stage.getWidth() / 2-32 , stage.getHeight()/2);
oneImg.setScale(2f,2f);
oneImg.addAction(rotateBy(360, 0.5f));
You can rotate your texture right when you draw it on the batch.
SpriteBatch.draw(textureRegion.getTexture(), x, y, originX, originY, width, height, scaleX, scaleY, rotation, srcX, srcX, srcWidth, srcHeight, false, false);
So above is the exact code that you need to rotate your image in x-axis, y-axis or both.
where :
x - the x-coordinate in screen space
y - the y-coordinate in screen space
originX - the x-coordinate of the scaling and rotation origin relative to the screen space coordinates
originY - the y-coordinate of the scaling and rotation origin relative to the screen space coordinates
width - the width in pixels
height - the height in pixels
scaleX - the scale of the rectangle around originX/originY in x
scaleY - the scale of the rectangle around originX/originY in y
rotation - the angle of counter clockwise rotation of the rectangle around originX/originY
srcX - the x-coordinate in texel space
srcY - the y-coordinate in texel space
srcWidth - the source with in texels
srcHeight - the source height in texels
I am drawing a series of rectangles on a Canvas. The rectangles are supposed to move on an angle. For some reason, when they move, they scale up:
xPos += xSpeed;
yPos += ySpeed;
updateBounds(xPos, yPos, width, height);
My UpdateBounds method:
public void updateBounds(double x, double y, double w, double h) {
bounds.setRect(x, y, w, h);
}
Bounds is a Rectangle2D object.
And my Drawing method:
g.fillRect((int) bounds.getX(), (int) bounds.getY(),
(int) bounds.getMaxX(), (int) bounds.getMaxY());
Why am I getting this behaviour?
Graphics.fillRect() accepts a width and height parameter, not the largest x and y position of the rectangle to draw.
The third and fourth parameters to fillRect should be Rectangle2D's getWidth() and getHeight().
As a reference, a link to what getMaxX() would give you.
I tried the code below, which draws a good approximation of a circle if the rectangle's width is the same as its height; but it doesn't draw a great oval, the "corners" are very pointed. Any suggestions?
float width = rect.width();
float height = rect.height();
float centerX = rect.width() / 2;
float centerY = rect.height() / 2;
float diameter = Math.min(width, height);
float length = (float) (0.5522847498 * diameter/2);
path.moveTo(0, centerY);
path.cubicTo(0, centerY - length, 0, centerX - length, 0, centerX, 0);
path.cubicTo(centerX + length, 0, width, centerY - length, height, centerY);
path.cubicTo(width, centerY + length, centerX + length, height, centerX, height);
path.cubicTo(centerX - length, height, 0, centerY + length, 0, centerY);
You should scale length according to which axis it's on, so that the distance from each arc endpoint to the adjacent control points is (not fixed but) a fixed fraction of the axis you're moving parallel to at that point.
If it's a true rectangle, with right angles, then it should be easy. The major and minor axes of the ellipse equal the lengths of the sides of the rectangle, and the center of the ellipse is located at the intersection of the rectangle's diagonals.
I don't know how to express it as Bezier splines off the top of my head, but the classic equation for an ellipse should be easy enough to write, as long as you transform to the appropriate coordinate system first (e.g. x-axis along the major axis of the rectangle/ellipse, y-axis along the minor axis of the rectangle/ellipse).