0
\$\begingroup\$

I am again trying to create a simple game to learn some libgdx coding skills. The game is a ball stuck on a track the track runs sideways, and there is also one that runs upwards which you have to hit by pressing SPACE. Once you get onto the upward path, the sideways one reappears at the top of the platform you are on and when you reach the new sideways one, the camera moves to put that platform on bottom of screen and then the process repeats.

It works ok , but for some reason it likes to 'stick' at the top of the "upwards" platform (when I say stick, i mean the direction variable seems to constantly flip "direction *= -1" and the ball is sort of hanging off the edge vibrating.

Here is my code, it is fairly simple but i just cant seem to find my problem. Any help is massively appreciated thanks

package com.megabro.megaball.screens; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.utils.viewport.FitViewport; import com.badlogic.gdx.utils.viewport.Viewport; import com.megabro.megaball.objects.Ball; import com.megabro.megaball.tools.Constants; public class PlayScreen implements Screen { Texture ballTexture; Texture platformTexture; SpriteBatch batch; OrthographicCamera camera; Viewport viewport; Ball ball; Rectangle platform1Rect; Rectangle platform2Rect; Rectangle currentPlatformRect; float platformLongSide; float platformFatSide; float transitionTimer = 0; float transitionDelay = 0.33f; boolean isTransitioning = false; public PlayScreen(SpriteBatch batch){ this.batch = batch; ballTexture = new Texture(Gdx.files.internal("badlogic.jpg")); platformTexture = new Texture(Gdx.files.internal("track.jpg")); camera = new OrthographicCamera(); camera.setToOrtho(false); viewport = new FitViewport(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT, camera); camera.position.set(viewport.getWorldWidth() / 2, viewport.getWorldHeight() / 2, 0); camera.update(); ball = new Ball(ballTexture); platformLongSide = viewport.getWorldWidth() * 0.75f; platformFatSide = ball.width * 2; platform1Rect = new Rectangle(viewport.getWorldWidth() / 2 - platformLongSide / 2, 0, platformLongSide, platformFatSide); ball.setPosition(platform1Rect.x + 10, platform1Rect.y); currentPlatformRect = platform1Rect; platform2Rect = new Rectangle(); } @Override public void show() { } public void update(float dt){ Rectangle previousPlatformRect = currentPlatformRect; if (currentPlatformRect == platform1Rect){ if (platform2Rect.y != currentPlatformRect.y + currentPlatformRect.height && ball.movingSideways) { platform2Rect.set(MathUtils.random(currentPlatformRect.x, currentPlatformRect.x + currentPlatformRect.width), currentPlatformRect.y + currentPlatformRect.height, platformFatSide, platformLongSide); } } else { platform1Rect.set(viewport.getWorldWidth() / 2 - platformLongSide / 2, currentPlatformRect.y + currentPlatformRect.height - platform1Rect.height, platformLongSide, platformFatSide); } if (ball.movingSideways) { if (ball.getX() + ball.getWidth() >= currentPlatformRect.x + currentPlatformRect.width || ball.getX() <= currentPlatformRect.x) { ball.directionX *= -1; } } if (!ball.movingSideways){ if (ball.getY() + ball.height >= currentPlatformRect.y + currentPlatformRect.height || ball.getY() < currentPlatformRect.y) { transitionTimer += dt; if (transitionTimer >= transitionDelay){ isTransitioning = false; } if (!isTransitioning) { ball.directionY *= -1; } } } if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)){ if (currentPlatformRect == platform1Rect) { ball.movingSideways = false; currentPlatformRect = platform2Rect; isTransitioning = true; transitionTimer = 0f; } else { ball.movingSideways = true; currentPlatformRect = platform1Rect; camera.position.set(camera.position.x, currentPlatformRect.getY() + viewport.getWorldHeight() / 2,0); isTransitioning = true; transitionTimer = 0f; } ball.toggleMovement(); } ball.update(dt); camera.update(); } @Override public void render(float delta) { update(delta); Gdx.gl.glClearColor(1, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); batch.setProjectionMatrix(camera.combined); batch.begin(); batch.draw(platformTexture, platform1Rect.x, platform1Rect.y, platform1Rect.width, platform1Rect.height); batch.draw(platformTexture, platform2Rect.x, platform2Rect.y, platform2Rect.width, platform2Rect.height); ball.draw(batch); batch.end(); } @Override public void resize(int width, int height) { viewport.update(width, height); } @Override public void pause() { } @Override public void resume() { } @Override public void hide() { } @Override public void dispose() { } } 

And the ball class:

public class Ball extends Sprite { public float width = 32; public float height = 32; public Vector2 speed; public float directionX = 1; public float directionY = 1; public boolean movingSideways = true; public boolean isAlive = true; public Ball(Texture texture){ super(texture); setSize(width, height); speed = new Vector2(200,0); } public void update(float dt){ if (isAlive) { setPosition(getX() + (speed.x * dt) * directionX, getY() + (speed.y * dt) * directionY); } } public void toggleMovement(){ if (speed.x != 0){ speed.y = speed.x; speed.x = 0; } else { speed.x = speed.y; speed.y = 0; } } public void killBall(){ isAlive = false; } 

}

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Sorry to waste your pages here. I have remembered how to do it.

For anyone if they read this needing similar help. I needed to set the position of the ball to be equal to the platform it was bouncing off at the point of collision detection like so:

if (ball.movingSideways) { if (ball.getX() + ball.getWidth() >= currentPlatformRect.x + currentPlatformRect.width) { ball.setPosition(currentPlatformRect.x + currentPlatformRect.width - ball.width, ball.getY()); ball.directionX *= -1; } if (ball.getX() <= currentPlatformRect.x) { ball.setPosition( currentPlatformRect.x, ball.getY()); ball.directionX *= -1; } } if (!ball.movingSideways){ if (ball.getY() + ball.height >= currentPlatformRect.y + currentPlatformRect.height) { ball.setPosition(ball.getX(), currentPlatformRect.y + currentPlatformRect.height - ball.height); transitionTimer += dt; if (transitionTimer >= transitionDelay){ isTransitioning = false; } if (!isTransitioning) { ball.directionY *= -1; } } if (ball.getY() < currentPlatformRect.y) { ball.setPosition(ball.getX(), currentPlatformRect.y); transitionTimer += dt; if (transitionTimer >= transitionDelay){ isTransitioning = false; } if (!isTransitioning) { ball.directionY *= -1; } } 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.