I have a problem with my game (jumper type game), when a player is falling down or move left/right it isn't smooth, I can see sprite multiple times (like some kind of shadow). After searching the internet I think it's something with deltaTime but I cannot find out how to fix it.
The code for render method in play screen is:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//game running
if(state == RUNNING){
//make world step
doPhysicsStep(delta);
//update player and camera
player.update(delta);
if(player.getBody().getPosition().x < bottomLeft.x)
player.getBody().setTransform(bottomRight.x, player.getBody().getPosition().y, player.getBody().getAngle());
else if(player.getBody().getPosition().x > bottomRight.x)
player.getBody().setTransform(bottomLeft.x, player.getBody().getPosition().y, player.getBody().getAngle());
camera.position.y =player.getBody().getPosition().y > camera.position.y ? player.getBody().getPosition().y: camera.position.y;
camera.update();
levelGenerator.generate(camera.position.y + camera.viewportHeight /2,camera.position.y - camera.viewportHeight /2);
}
//game ended
if(player.getBody().getPosition().y< camera.position.y - camera.viewportHeight/2 -player.HEIGHT/2 && state == RUNNING){
gameOverScreen.updateScore(score.getText().toString().substring(7));
state = END;
}
if(state == END){
gameOverScreen.setVisible(true);
}
//drawing game
batch.setProjectionMatrix(camera.combined);
batch.begin();
renderBackground();
renderPlatform();
renderPlayer();
Assets.sparks.draw(batch, delta);
batch.end();
}
public void renderPlatform(){
//world.getBodies(tmpBodies);
//for(Body body : tmpBodies)
// if(body.getUserData() != null && body.getUserData() instanceof Platform)
for(int i=0;i<platforms.size();i++){
Body body = platforms.get(i).getBody();
Sprite sprite = platforms.get(i).getSprite();
sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(batch);
}
}
public void renderPlayer(){
world.getBodies(tmpBodies);
for(Body body : tmpBodies)
if(body.getUserData() != null && body.getUserData() instanceof Player){
Player player = (Player) body.getUserData();
Sprite sprite = player.getSprite();
sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
//sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(batch);
if(player.isPlayEffect()){
Assets.sparks.setPosition(body.getPosition().x , body.getPosition().y - sprite.getHeight()/2);
Assets.sparks.setDuration(200);
Assets.sparks.start();
}
}
}
public void renderBackground(){
world.getBodies(tmpBodies);
for(Body body : tmpBodies)
if(body.getUserData() != null && body.getUserData() instanceof Background){
Background background = (Background) body.getUserData();
Sprite sprite = background.getSprite();
//sprite.setPosition(body.getPosition().x - sprite.getWidth()/2, body.getPosition().y - sprite.getHeight()/2);
sprite.setBounds(bottomLeft.x, camera.position.y - camera.viewportHeight /2, camera.viewportWidth, camera.viewportHeight);
sprite.draw(batch);
}
}
private void doPhysicsStep(float deltaTime) {
// fixed time step
// max frame time to avoid spiral of death (on slow devices)
float frameTime = Math.min(deltaTime, 0.05f);
accumulator += frameTime;
while (accumulator >= TIMESTEP) {
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
accumulator -= TIMESTEP;
}
}
and player update:
public void update(float delta) {
stateTime += delta;
if(state==JUMP){
sprite.setRegion(jump.getKeyFrame(stateTime));
if(jump.isAnimationFinished(stateTime)){
state = IDLE;
stateTime = 0;
}
}else{
sprite.setRegion(idle.getKeyFrame(stateTime));
}
if(Gdx.app.getType() == ApplicationType.Android )
velocity.x = -Gdx.input.getAccelerometerX() * (movementForce / 10f);
if(applyJump){
body.applyLinearImpulse(0, jumpPower, body.getWorldCenter().x, body.getWorldCenter().y, true);
applyJump = false;}
body.setLinearVelocity(velocity.x, body.getLinearVelocity().y);
}
where applyJump variable is setted at contact in postSolve() method.
Thanks in advance
Well, that's two different problems then, right? It isn't smooth, and its drawing multiple sprites when it shouldn't be?
Lets tackle the second one first, that should be pretty obvious. Either you're making multiple calls to render the sprite somewhere, or you're not clearing the frame properly. It looks like you're clearing it, so you've gotta go through all your render code and see how you're drawing that texture twice. Somewhere its happening.
world.getBodies(tmpBodies);
for(Body body : tmpBodies)
Why does this code exist? Does the player character have multiple sprites? I wouldn't handle it this way; does the number change over time? Are there new players being created? You should probably create a separate Player class and allow it to manage its own state and data. Use OOP principles! Too much here is being done by a single GOD class, very procedural style. Bad Java.
If left/right movement isn't smooth you need to work on your physics.
Related
so I've been looking at other people's questions and I've tried to apply the answers to my code but I do not seem to find how to do so. All I am trying to do is that when the Sprite moves is going to rotate at the same time. This is where I am calling the rotate function inside my Player class (which extends Sprite):
public void playerMovement(float dt){
if(Gdx.input.isPeripheralAvailable(Input.Peripheral.Accelerometer)) {
float roll = Gdx.input.getRoll();
float pitch = Gdx.input.getPitch();
final float MAX_SPEED = 0.4f;
velocity.x = roll;
velocity.y = pitch;
x = position.x;
y = position.y;
if (Math.abs(velocity.x) > 1) {
x = Math.max(0, Math.min(Gdx.graphics.getWidth(), x + velocity.x * MAX_SPEED));
position.x = x;
position.y = y;
rotate(90 * dt); //NOT WORKING
}
if (Math.abs(velocity.y) > 1) {
y = Math.max(0, Math.min(Gdx.graphics.getHeight(), y + velocity.y * MAX_SPEED));
position.x = x;
position.y = y;
rotate(90* dt); //NOT WORKING
}
}
}
before it used to extend Actor and use setRotate(90 * dt) and it was working fine. I decided to change to Sprite because I did not want to depend on the stage.
here's how I am rendering the player inside my GameScreen:
game.batch.begin();
game.batch.draw(atlas2.findRegion("player"), player.getPosition().x, player.getPosition().y, player.PLAYER_DIMENSION , player.PLAYER_DIMENSION);
game.batch.end();
I tried to use the player.draw(game.batch) function from the Sprite class, yet I did not work because I needed a Texture and the texture I am using is just an AtlasRegion and it did not let my type casted to a Texture
so if there are any suggestions on how to make my Sprite rotate I will appreciate!
Sprite holds the geometry, color, and texture information for drawing 2D sprite. In geometry, it carry sprite position, size, rotation and scale.
By using below method you're only passing position and size of player Sprite.
game.batch.draw(atlas2.findRegion("player"), player.getPosition().x, player.getPosition().y, player.PLAYER_DIMENSION , player.PLAYER_DIMENSION);
Use in this way :
TextureAtlas atlas2=....;
Sprite player=new Sprite(atlas2.findRegion("player"));
and draw by using draw method of Sprite
player.draw(game.batch);
EDIT
public class Player extends Sprite {
public Player(TextureRegion region){ // <- Add this constructor
super(region);
...
}
}
And create object of Player
Player player=new Player(atlas2.findRegion("player");
Synopsis
Well, I'm making a little top-down JRPG and today I was like 'Yeah, I'm gonna bust out this whole map collision thing!'. I failed.
Problem
So I went on the internet and looked up 'LibGDX Tiled Map Collision Detection' and found a really neat post about Map Objects so I added in a map object layer and did all that biz and came out with this little method to ensure the player can move freely around the map but at the same time can't exit it but each time I've tried it ends up with a horrible result such as the player moving off the screen. The latest error is that the player gets stuck doing a walk animation and can't move anywhere else!
Code
package com.darkbyte.games.tfa.game.entity.entities;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.math.Rectangle;
import com.darkbyte.games.tfa.game.entity.Entity;
import com.darkbyte.games.tfa.game.entity.SpriteSheet;
import com.darkbyte.games.tfa.game.world.map.MapManager;
import com.darkbyte.games.tfa.render.Batch;
import com.darkbyte.games.tfa.render.Camera;
public class Player extends Entity {
// The constructor for the player class
public Player(String name, SpriteSheet spriteSheet) {
super(name, spriteSheet);
direction = Direction.DOWN;
collisionBox = new Rectangle(x, y, 64, 64);
}
// A flag to see if the player is moving
private boolean isMoving;
// The variable that holds the state time
private float stateTime;
// The player's walking animations
private Animation[] walkAnimations = {
spriteSheet.getAnimation(8, 8, 1 / 16f),
spriteSheet.getAnimation(9, 8, 1 / 16f),
spriteSheet.getAnimation(10, 8, 1 / 16f),
spriteSheet.getAnimation(11, 8, 1 / 16f) };
// The player's static frames
private TextureRegion[] staticFrames = {
spriteSheet.getTexture(8, 0),
spriteSheet.getTexture(9, 0),
spriteSheet.getTexture(10, 0),
spriteSheet.getTexture(11, 0) };
// The render code for the player
#Override
public void render() {
// Makes the camera follow the player
Camera.setCameraPosition(x, y);
Batch.getGameBatch().setProjectionMatrix(Camera.getCamera().combined);
// Updates the state time
stateTime += Gdx.graphics.getDeltaTime();
// Gets the player's direction, if the player's moving, it sets the
// current frame to the frame that would be played at the current moment
// based on the state time
// If the player isn't moving, it sets the current frame to the static
// frame associated to the direction
switch (direction) {
case UP:
if(isMoving) {
currentFrame = walkAnimations[0].getKeyFrame(stateTime, true);
} else
currentFrame = staticFrames[0];
break;
case LEFT:
if(isMoving) {
currentFrame = walkAnimations[1].getKeyFrame(stateTime, true);
} else
currentFrame = staticFrames[1];
break;
case DOWN:
if(isMoving) {
currentFrame = walkAnimations[2].getKeyFrame(stateTime, true);
} else
currentFrame = staticFrames[2];
break;
case RIGHT:
if(isMoving) {
currentFrame = walkAnimations[3].getKeyFrame(stateTime, true);
} else
currentFrame = staticFrames[3];
break;
}
}
// The tick code for the player
#Override
public void tick() {
// The object to represent the bounds of the land on the map
RectangleMapObject land = (RectangleMapObject) MapManager.getCurrentMap().getMap().getLayers().get("collision").getObjects().get("land");
// Checks if the player is within the bounds of the map
if(land.getRectangle().contains(collisionBox)) {
// If the player is moving but the arrow keys aren't pressed, sets isMoving to false
isMoving = (isMoving && (Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP)
|| Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)
|| Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN)
|| Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT)));
// Checks to see if the arrow / WASD keys are pressed and moves the
// player in the correct direction at the speed of 1.5 pixels/tick
// (45/second)
// It also sets the players state to moving and corresponds it's
// direction to the key pressed
// Doesn't move if opposing keys are pressed
if(Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP)) {
if(!(Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN))) {
direction = Direction.UP;
y += 1.5f;
isMoving = true;
}
}
if(Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)) {
if(!(Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT))) {
direction = Direction.LEFT;
isMoving = true;
x -= 1.5f;
}
}
if(Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN)) {
if(!(Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP))) {
direction = Direction.DOWN;
y -= 1.5f;
isMoving = true;
}
}
if(Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT)) {
if(!(Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT))) {
direction = Direction.RIGHT;
x += 1.5f;
isMoving = true;
}
}
} else {
if(!isMoving) {
// If the player's just spawned puts the player to the map's spawn point
x = MapManager.getCurrentMap().getPlayerSpawnX();
y = MapManager.getCurrentMap().getPlayerSpawnY();
} else { // If not, it just moves them back till they're no longer out of the map
if(x > (land.getRectangle().getX() + land.getRectangle().getWidth())) x -= 1.5;
if(y > (land.getRectangle().getY() + land.getRectangle().getHeight())) y -= 1.5;
}
}
// Synchronises the collision box with the player's x and y position
collisionBox.x = x;
collisionBox.y = y;
}
// Returns if the player is moving
public boolean isMoving() {
return isMoving;
}
}
Can you guys make it so that when he reaches the border that he stops but he can still keep moving in other directions instead of staying static!
Thanks for reading!
At the moment it sounds you just copy/pasted it and you need to familiarize yourself with it first. If you don't know what it does then you should learn or stop the project imho.
Anyway, from what I can tell it's just a player class that handles the animation frames based on which direction it is moving. Nothing to do with collision detection at all. It does update some kind of collisionBox but functionality for this is handled elsewhere, perhaps in the parent class Entity?
My guess is that this is a tile map and units are restricted to the grid. It's pretty easy to detect if A tile exists or not.
private boolean tileExists(int tileX, int tileY, tile[][] map)
{
return tileX >= 0 && tileY >= 0 &&
tileX < map.length && tileY < map[0].length;
}
Now whenever a entity requests a move you should check if the destination is within the map bounds.
private void moveRequest(int destinationX, int destinationY, Tile[][] map)
{
//Just return if the tile is outside of the map
if (!tileExists(destinationX, destinationY, map) return;
//Same goes for your other checks...
//Return if the tile is not walkable
if (!tileIsWalkable(destinationX, destinationY, map) return;
//Return if the tile is already occupied
if (tileIsOccupied(destinationX, destinationY, otherEntities) return;
//etc..
//Now the move is valid and you can set it's state to moving in that direction.
}
Tile maps are not very hard to understand. I will make an attempt to give you some better insight into tile maps. You have a 2D array where you store your tiles in. Tiles have a width and a height and from that you can make your own tile engine:
//Find out which tiles to draw based on the camera position and viewport size.
int startX = (int)(camera.position.x - camera.viewportWidth / 2) / tileWidth;
int startY = (int)(camera.position.y - camera.viewportHeight / 2) / tileHeight;
int endX = (int)(startX + camera.viewportWidth / tileWidth) + 1;
int endY = (int)(startY + camera.viewportHeight / tileHeight) + 1;
//Loop using this data as boundaries
for (int y = startY; y < endY; y++)
{
for (int x = startX; x < endX; x++)
{
//If out of bounds continue to next tile.
if (!tileExists(x, y, map) continue;
//Now all we need to draw the on screen tiles properly:
//x == tile position x in array
//y == tile position y in array
//World position of this tile:
//worldX = x * tileWidth;
//worldY = y * tileHeight;
//Let's draw:
batch.draw(map[x][y].getTexture, worldX, worldY,
tileWidth, tileHeight)
}
}
There really is no magic involved here at all. Drawing only what is on screen like in the above example is very important for larger maps. Other then that you should draw thing in the back first. You have several options to do this, the easiest but least versatile is just a separate the ground from the objects that can obscure things and draw this later.
Characters, creatures or other entities can just use a world position and be easily converted back to tile position.
tileX = worldX / tileWidth;
tileY = worldY / tileHeight;
So if you want to move something with the world position calculate it's tile position first using the aforementioned method. Then lookup if this tile is valid to move to. Then block that tile for other and move to it.
I have seen many questions exactly like these with the same answer but it never works for me. I have a player who has a position vector that is constantly updating. I need my camera to constantly follow my players position. This is the player class
public void update(){
//velocity.y += MainScreen.GRAVITY.y;
velocity.x += MainScreen.GRAVITY.x;
oldPosition = getGridPosition();
position.x += velocity.x;
position.y += velocity.y;
if(oldPosition.x == getGridPosition().x){
hasMoved = false;
System.out.println("Hasn't moved");
System.out.println("normX- " + position.x);
System.out.println("oldX- " + oldPosition.x);
}
else{
hasMoved = true;
System.out.println("Has moved");
}
if(Gdx.input.isKeyPressed(Keys.A)){
velocity.x -= 10 * Gdx.graphics.getDeltaTime();
}
else if(Gdx.input.isKeyPressed(Keys.D)){
velocity.x += 10 * Gdx.graphics.getDeltaTime();
}else
velocity.x = 0;
}
And I have tried to set the camera position to my players position but it just does NOT for me. Here is my main render loop.
public void render () {
player.update();
camera.position.set(player.getPosition(), 0);
camera.update();
mapGrid.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mapGrid.render(tmpRend);
player.render(tmpRend);
}
I am at such a loss I don't know what I am doing wrong, here is how I initialize my camera in my create method
player = new Player();
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false);
//camera.position.set(MapGrid.MAP_WIDTH / 2, MapGrid.MAP_HEIGHT, 0);
camera.position.set(player.getPosition(), 0);
camera.zoom = 2f;
camera.update();
tmpRend = new ShapeRenderer();
tmpRend.setAutoShapeType(true);
tmpRend.setProjectionMatrix(camera.combined);
input = new InputListener(camera);
Gdx.input.setInputProcessor(input);
Please I have seen many other questions just like this and I am doing what they say but mine just doesn't work. Here is an image to show what happens.
I just don't know why it won't work when I have followed everything I have been told. Any help you can give me would be extremely appreciated!
I think that you already update wrong camera - you've created an instance of it but where it is connected with other part of program?
Use Scene2d to manage your actors, viewports and cameras. You can read more about it here:
https://github.com/libgdx/libgdx/wiki/Scene2d
You are creating new stage, add player (which should inherit from Actor class) to it and then just instead of creating new Camera just get your current stage camera just like
stage.getCamera()
all you should do in render is
...
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
this.viewport.update(this.screenWidth, this.screenHeight);
this.stage.act();
this.stage.draw();
I am making a game in Libgdx, in which I want to fall a ball from up word to down word and move background towards up word and update camera according to it. My code is given below...
public WorldRenderer(SpriteBatch spriteBatch, World world){
this.world = world;
this.camera = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
this.camera.position.set(FRUSTUM_WIDTH/2, FRUSTUM_HEIGHT/2, 0);
this.spriteBatch = spriteBatch;
positionBGY1 = 0;
positionBGY2 = 0;
}
public void render(World world, float deltaTime){
if(world.ball.position.y > - camera.position.y){
camera.position.y = world.ball.position.y;
}
if(camera.position.y<0)
camera.position.y=world.ball.position.y;
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
renderBackground();
renderObjects(world, deltaTime);
}
private void calculateBGPosition(){
positionBGY2 = positionBGY1 + (int)FRUSTUM_HEIGHT;
if(camera.position.y >= positionBGY2){
positionBGY1 = positionBGY2;
}
}
private void renderBackground() {
spriteBatch.disableBlending();
spriteBatch.begin();
spriteBatch.draw(Assets.gamebackgroundRegion, camera.position.x - FRUSTUM_WIDTH / 2, positionBGY1 - FRUSTUM_HEIGHT / 2, FRUSTUM_WIDTH, 1.5f * FRUSTUM_HEIGHT);
spriteBatch.draw(Assets.gamebackgroundRegion2, camera.position.x - FRUSTUM_WIDTH / 2, positionBGY2 - FRUSTUM_HEIGHT / 2, FRUSTUM_WIDTH, 1.5f * FRUSTUM_HEIGHT);
calculateBGPosition();
spriteBatch.end();
}
Problem with my code is background screen is not moving and camera is not updated with ball movement and ball fall down out of screen.
Well, first thing you need to do is change one of the bg variables, so in render somehwere do something like:
float speed = 1;
positionBGY1+=deltaTime*speed;
positionBGY2+=deltaTime*speed;
As far as the camera moving with the ball I would just do this:
camera.position.y = Math.min(ball.y,0);
And I would in turn omit all of this:
if(world.ball.position.y > - camera.position.y){
camera.position.y = world.ball.position.y;
}
if(camera.position.y<0)
camera.position.y=world.ball.position.y;
Best way to achieve this is using ParrallaxLayer and ParrallaxBackground classes
it optimised the background operation so that u dont have to worry about the performance
In order to build a tic-tac-toe game for testing, I have following routine. But problem is that I am getting too many events for just one touch. I suspect isTouched() returns all of down, up, and move. Is there any way to just get up event?
UPDATE: Resolved the issue by employing justTouched() instead.
#Override
public void render() {
// we update the game state so things move.
updateGame();
// First we clear the screen
GL10 gl = Gdx.graphics.getGL10();
gl.glViewport(0, 0, width, height);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Next we update the camera and set the camera matrix
camera.update();
camera.apply(Gdx.gl10);
...
}
private void updateGame() {
// the delta time so we can do frame independant time based movement
float deltaTime = Gdx.graphics.getDeltaTime();
// Has the user touched the screen? then position the paddle
if (Gdx.input.isTouched() && !isProcess) {
// get the touch coordinates and translate them
// to the game coordinate system.
isProcess=true;
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
int offx=-width/2;
int offy=-height/2;
float x = Gdx.input.getX();
float y = Gdx.input.getY();
float touchX = 480 * (x
/ (float) width - 0.5f);
float touchY = 320 * (0.5f - y
/ (float) height);
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++)
{
if(touchX >= offx+i*width/3 && touchX < offx+(i+1)*width/3 &&
touchY >= offy+j*height/3 && touchY < offy+(j+1)*height/3)
{
if(isCurrentO)
data[i][j]=CellStatus.O;
else
data[i][j]=CellStatus.X;
isCurrentO=!isCurrentO;
break;
}
}
}
isProcess=false;
}
}
An alternative to using justTouched is to implement the InputProcessor interface, as it has a touchUp(x,y,pointer,button) which gives you greater control over the input. There are several classes that implement this or you can have your class implement it.
You can create a board for example (with hash map) and each object in your game wants to be clickable add itself to that board if an object was touched and was in board it will catch the event. If not it will not catch the event. So easy! :)