I’m probably doing something really dumb, but I spent all night trying to figure this out and I am officially stumped. Basically, I have an object (which in this case is a particle emitter) that gets rendered at 0, 0. When I press the arrow buttons, the camera moves, and I want the particle emitter to stay in it’s location (so if the camera moves right, the particle emitter will no longer be visible). Right now, it is maintaining its 0, 0 position of the camera so it is always visible. Is this due to me not translating pixels to world coordinates? I’m also using an isometric view so I’m not sure if that has anything to do with it. I have tried setting the particle emitter x, y on one of the tile’s x, y, however it still scrolls around with the camera at 0, 0. I’ll put some relevant code examples below, but the whole code is here https://github.com/IanFell/GameTemplate
Here is how the screen is rendered:
public void render(float delta) {
// If game screen has not been initialized, go ahead and initialize it.
if (!hasBeenInitialized) {
initializeGameScreen();
hasBeenInitialized = !hasBeenInitialized;
}
clearScreenAndSetScreenColor();
// Screen only shakes when needed, but we must update it at all times just in case it needs to shake.
screenShake.update(delta, camera);
updateCamera();
myGame.renderer.batch.begin();
mapRenderer.renderMap(myGame, mapEditor);
myGame.renderer.batch.end();
// If a screenshake happened, reset camera to it's original position before shake.
resetCameraAfterScreenShake();
/**
* Since these objects use a ShapeRenderer we must draw them after sprite batch has ended,
* otherwise they will block the sprite batch from being rendered.
*/
drawAdditionalObjectsOnGameScreenThatDontUseSpriteBatch();
// Update objects associated with GameScreen.
updateGameScreen();
// Perform debug testing on GameScreen so we know different scenarios work.
debugger.debugGameScreen(myGame, mapEditor);
}
Here’s 'drawAdditionalObjectsOnGameScreenThatDontUseSpriteBatch()
private void drawAdditionalObjectsOnGameScreenThatDontUseSpriteBatch() {
myGame.gameObjectLoader.enemy.draw(myGame.renderer.batch);
myGame.gameObjectLoader.player.draw(myGame.renderer.batch);
particleEmitterRed.drawParticleEmitter(myGame.renderer.batch, "Red");
particleEmitterYellow.drawParticleEmitter(myGame.renderer.batch, "Yellow");
particleEmitterOrange.drawParticleEmitter(myGame.renderer.batch, "Orange");
}
And here is update camera:
protected void updateCamera() {
myGame.renderer.batch.setProjectionMatrix(camera.combined);
myGame.renderer.batch.setTransformMatrix(matrix);
camera.update();
}
Here is the draw method for the particle emitter:
/**
*
* @param SpriteBatch batch
* @param ParticleType particleType
*/
public void drawParticleEmitter(SpriteBatch batch, String particleType) {
drawFire(batch, particleType);
}
/**
* Draws fire particle effects.
*
* @param SpriteBatch batch
* @param String particleType
*/
private void drawFire(SpriteBatch batch, String particleType) {
if (particleType.equalsIgnoreCase("Red")) {
for (int i = 0; i < redParticles.length; i++) {
redParticles[i].draw(batch);
}
}
if (particleType.equalsIgnoreCase("Yellow")) {
for (int i = 0; i < yellowParticles.length; i++) {
yellowParticles[i].draw(batch);
}
}
if (particleType.equalsIgnoreCase("Orange")) {
for (int i = 0; i < orangeParticles.length; i++) {
orangeParticles[i].draw(batch);
}
}
}
And here’s the update method for the particle emitter:
public void updateParticleEmitter(MyGame myGame) {
if (particleType.equalsIgnoreCase("Red")) {
for (int i = 0; i < redParticles.length; i++) {
redParticles[i].updateParticles(this);
}
}
if (particleType.equalsIgnoreCase("Yellow")) {
for (int i = 0; i < yellowParticles.length; i++) {
yellowParticles[i].updateParticles(this);
}
}
if (particleType.equalsIgnoreCase("Orange")) {
for (int i = 0; i < orangeParticles.length; i++) {
orangeParticles[i].updateParticles(this);
}
}
}
Here’s how the camera is created:
public void createCamera() {
int width = GameAttributeHelper.SCREEN_WIDTH;
int height = GameAttributeHelper.SCREEN_HEIGHT;
camera = new OrthographicCamera(width, height);
viewport = new ExtendViewport(width, height, camera);
camera.position.set(0, 0, 0);
}
And here is how the camera is initialized to be isometric:
private void initializeCameraForIsometricView() {
camera = new OrthographicCamera(
10,
10 * (GameAttributeHelper.SCREEN_HEIGHT / (float)GameAttributeHelper.SCREEN_WIDTH)
);
camera.position.set(5, 5, 10);
camera.direction.set(-1, -1, -1);
camera.near = 1;
camera.far = 100;
matrix.setToRotation(new Vector3(1, 0, 0), 90);
}
and when I move the camera, I use camera.translate(someValue).
What I’m really attempting to do, is have the player always in the middle of the screen, and scroll the camera around the world. But I need to fix this problem. I know this is a long post, but I would really appreciate any advice with this.