use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.
the class TiledMapObjectLoadingTest method render.
@Override
public void render() {
Gdx.gl.glClearColor(0.55f, 0.55f, 0.55f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
shapeRenderer.setProjectionMatrix(camera.combined);
batch.setProjectionMatrix(camera.combined);
shapeRenderer.setColor(Color.BLUE);
Gdx.gl20.glLineWidth(2);
MapLayer layer = map.getLayers().get("Objects");
AnimatedTiledMapTile.updateAnimationBaseTime();
for (MapObject mapObject : layer.getObjects()) {
if (mapObject instanceof TiledMapTileMapObject) {
batch.begin();
TiledMapTileMapObject tmtObject = (TiledMapTileMapObject) mapObject;
TextureRegion textureRegion = tmtObject.getTile().getTextureRegion();
// TilEd rotation is clockwise, we need counter-clockwise.
float rotation = -tmtObject.getRotation();
float scaleX = tmtObject.getScaleX();
float scaleY = tmtObject.getScaleY();
float xPos = tmtObject.getX();
float yPos = tmtObject.getY();
textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
batch.draw(textureRegion, xPos, yPos, tmtObject.getOriginX() * scaleX, tmtObject.getOriginY() * scaleY, textureRegion.getRegionWidth() * scaleX, textureRegion.getRegionHeight() * scaleY, 1f, 1f, rotation);
// We flip back to the original state.
textureRegion.flip(tmtObject.isFlipHorizontally(), tmtObject.isFlipVertically());
batch.end();
} else if (mapObject instanceof EllipseMapObject) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse();
shapeRenderer.ellipse(ellipse.x, ellipse.y, ellipse.width, ellipse.height);
shapeRenderer.end();
} else if (mapObject instanceof RectangleMapObject) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle();
shapeRenderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
shapeRenderer.end();
} else if (mapObject instanceof PolygonMapObject) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
Polygon polygon = ((PolygonMapObject) mapObject).getPolygon();
shapeRenderer.polygon(polygon.getTransformedVertices());
shapeRenderer.end();
}
}
batch.begin();
font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
batch.end();
}
use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.
the class SuperKoalio method updateKoala.
private void updateKoala(float deltaTime) {
if (deltaTime == 0)
return;
if (deltaTime > 0.1f)
deltaTime = 0.1f;
koala.stateTime += deltaTime;
// check input and apply to velocity & state
if ((Gdx.input.isKeyPressed(Keys.SPACE) || isTouched(0.5f, 1)) && koala.grounded) {
koala.velocity.y += Koala.JUMP_VELOCITY;
koala.state = Koala.State.Jumping;
koala.grounded = false;
}
if (Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A) || isTouched(0, 0.25f)) {
koala.velocity.x = -Koala.MAX_VELOCITY;
if (koala.grounded)
koala.state = Koala.State.Walking;
koala.facesRight = false;
}
if (Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D) || isTouched(0.25f, 0.5f)) {
koala.velocity.x = Koala.MAX_VELOCITY;
if (koala.grounded)
koala.state = Koala.State.Walking;
koala.facesRight = true;
}
if (Gdx.input.isKeyJustPressed(Keys.B))
debug = !debug;
// apply gravity if we are falling
koala.velocity.add(0, GRAVITY);
// clamp the velocity to the maximum, x-axis only
koala.velocity.x = MathUtils.clamp(koala.velocity.x, -Koala.MAX_VELOCITY, Koala.MAX_VELOCITY);
// If the velocity is < 1, set it to 0 and set state to Standing
if (Math.abs(koala.velocity.x) < 1) {
koala.velocity.x = 0;
if (koala.grounded)
koala.state = Koala.State.Standing;
}
// multiply by delta time so we know how far we go
// in this frame
koala.velocity.scl(deltaTime);
// perform collision detection & response, on each axis, separately
// if the koala is moving right, check the tiles to the right of it's
// right bounding box edge, otherwise check the ones to the left
Rectangle koalaRect = rectPool.obtain();
koalaRect.set(koala.position.x, koala.position.y, Koala.WIDTH, Koala.HEIGHT);
int startX, startY, endX, endY;
if (koala.velocity.x > 0) {
startX = endX = (int) (koala.position.x + Koala.WIDTH + koala.velocity.x);
} else {
startX = endX = (int) (koala.position.x + koala.velocity.x);
}
startY = (int) (koala.position.y);
endY = (int) (koala.position.y + Koala.HEIGHT);
getTiles(startX, startY, endX, endY, tiles);
koalaRect.x += koala.velocity.x;
for (Rectangle tile : tiles) {
if (koalaRect.overlaps(tile)) {
koala.velocity.x = 0;
break;
}
}
koalaRect.x = koala.position.x;
// top bounding box edge, otherwise check the ones to the bottom
if (koala.velocity.y > 0) {
startY = endY = (int) (koala.position.y + Koala.HEIGHT + koala.velocity.y);
} else {
startY = endY = (int) (koala.position.y + koala.velocity.y);
}
startX = (int) (koala.position.x);
endX = (int) (koala.position.x + Koala.WIDTH);
getTiles(startX, startY, endX, endY, tiles);
koalaRect.y += koala.velocity.y;
for (Rectangle tile : tiles) {
if (koalaRect.overlaps(tile)) {
// this removes bouncing :)
if (koala.velocity.y > 0) {
koala.position.y = tile.y - Koala.HEIGHT;
// we hit a block jumping upwards, let's destroy it!
TiledMapTileLayer layer = (TiledMapTileLayer) map.getLayers().get("walls");
layer.setCell((int) tile.x, (int) tile.y, null);
} else {
koala.position.y = tile.y + tile.height;
// if we hit the ground, mark us as grounded so we can jump
koala.grounded = true;
}
koala.velocity.y = 0;
break;
}
}
rectPool.free(koalaRect);
// unscale the velocity by the inverse delta time and set
// the latest position
koala.position.add(koala.velocity);
koala.velocity.scl(1 / deltaTime);
// Apply damping to the velocity on the x-axis so we don't
// walk infinitely once a key was pressed
koala.velocity.x *= Koala.DAMPING;
}
use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.
the class ScissorStack method popScissors.
/** Pops the current scissor rectangle from the stack and sets the new scissor area to the new top of stack rectangle. In case
* no more rectangles are on the stack, {@link GL20#GL_SCISSOR_TEST} is disabled.
* <p>
* Any drawing should be flushed before popping scissors. */
public static Rectangle popScissors() {
Rectangle old = scissors.pop();
if (scissors.size == 0)
Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST);
else {
Rectangle scissor = scissors.peek();
HdpiUtils.glScissor((int) scissor.x, (int) scissor.y, (int) scissor.width, (int) scissor.height);
}
return old;
}
use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.
the class ScissorStack method getViewport.
/** @return the current viewport in OpenGL ES window coordinates based on the currently applied scissor */
public static Rectangle getViewport() {
if (scissors.size == 0) {
viewport.set(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
return viewport;
} else {
Rectangle scissor = scissors.peek();
viewport.set(scissor);
return viewport;
}
}
use of com.badlogic.gdx.math.Rectangle in project libgdx by libgdx.
the class Group method drawChildren.
/** Draws all children. {@link #applyTransform(Batch, Matrix4)} should be called before and {@link #resetTransform(Batch)}
* after this method if {@link #setTransform(boolean) transform} is true. If {@link #setTransform(boolean) transform} is false
* these methods don't need to be called, children positions are temporarily offset by the group position when drawn. This
* method avoids drawing children completely outside the {@link #setCullingArea(Rectangle) culling area}, if set. */
protected void drawChildren(Batch batch, float parentAlpha) {
parentAlpha *= this.color.a;
SnapshotArray<Actor> children = this.children;
Actor[] actors = children.begin();
Rectangle cullingArea = this.cullingArea;
if (cullingArea != null) {
// Draw children only if inside culling area.
float cullLeft = cullingArea.x;
float cullRight = cullLeft + cullingArea.width;
float cullBottom = cullingArea.y;
float cullTop = cullBottom + cullingArea.height;
if (transform) {
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible())
continue;
float cx = child.x, cy = child.y;
if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom)
child.draw(batch, parentAlpha);
}
} else {
// No transform for this group, offset each child.
float offsetX = x, offsetY = y;
x = 0;
y = 0;
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible())
continue;
float cx = child.x, cy = child.y;
if (cx <= cullRight && cy <= cullTop && cx + child.width >= cullLeft && cy + child.height >= cullBottom) {
child.x = cx + offsetX;
child.y = cy + offsetY;
child.draw(batch, parentAlpha);
child.x = cx;
child.y = cy;
}
}
x = offsetX;
y = offsetY;
}
} else {
// No culling, draw all children.
if (transform) {
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible())
continue;
child.draw(batch, parentAlpha);
}
} else {
// No transform for this group, offset each child.
float offsetX = x, offsetY = y;
x = 0;
y = 0;
for (int i = 0, n = children.size; i < n; i++) {
Actor child = actors[i];
if (!child.isVisible())
continue;
float cx = child.x, cy = child.y;
child.x = cx + offsetX;
child.y = cy + offsetY;
child.draw(batch, parentAlpha);
child.x = cx;
child.y = cy;
}
x = offsetX;
y = offsetY;
}
}
children.end();
}
Aggregations