[SOLVED]Platformer camera follow vertical movement problem


(Kyle) #1

Hi =) This will be my first post so yeah.

I have this helper class which makes the camera follows a certain entity. The entity has some leeway distance from the center of the screen before the camera mover class considers to follow it. It works just fine when the player moves horizontally. But when the movement is vertical, the camera starts doing weird things. The camera would shift its position vertically. What I mean is that the camera would teleport vertically on a up-down-up-down manner every frame(think vertical screen shake). This mostly happens when the entity is moving down the screen. I also noticed that if the player is somewhere in the middle of the world height, the camera would do the weird thing regardless of the players vertical velocity. My game is a platformer game. The weird thing doesn’t affect the camera along the x-axis.

Here is the relevant code for repositioning the camera along the y-axis:

// upper bounds
if (entity.y < FP.camera.y + halfCameraHeight - yThreshold)
{
	FP.camera.y = (entity.y - halfCameraHeight + yThreshold);
}
// lower bounds
else if (entity.y + entity.height > FP.camera.y + halfCameraHeight + yThreshold)
{
	FP.camera.y = entity.y + entity.height - halfCameraHeight - yThreshold;
}

Any ideas?


(Kyle) #2

Found the problem. Forgot to set the yThreshold to something. So it was actually zero. I don’t understand why it happens though.


(MartĂ­ Angelats i Ribera) #3

It’s probably made becouse of the yThreshold value and the way you made this. It probably is in both positions simultaneously.

To avoid this i recommend to use half of the height to get the center y position, wich is the one to be compares with the center y position of the screen. This will also be independent of the entity’s size.

The code would be something like:

if (entity.y + entity.height/2 < FP.camera.y + halfCameraHeight + yThreshold)
{
	FP.camera.y = entity.y + entity.height/2 - yThreshold;
}
else if (entity.y + entity.height/2 + yThreshold > FP.camera.y + halfCameraHeight)
{
	FP.camera.y = entity.y + entity.height/2 + yThreshold;
}

PD: If you had set yTheshold to 0, they are both being triggered simultaneously.


(Kyle) #4

Ooohhh… that’s why. I also liked your suggestion of using the entity’s center for comparing values. But I used a different approach to that. I rewrote my camera code into:

// get entity.center distance from camera.center
var deltaX:Number = (entity.x + entity.width / 2) - (FP.camera.x + halfCameraWidth);
var deltaY:Number = (entity.y + entity.height / 2) - (FP.camera.y + halfCameraHeight);
			
// Check if the distance is over the xThresholds
if (Math.abs(deltaX) > xThreshold)
{
	FP.camera.x += deltaX - (FP.sign(deltaX) * xThreshold);	// if it is, move the camera
}
// Do same thing on y-axis
if (Math.abs(deltaY) > yThreshold)
{
	FP.camera.y += deltaY - (FP.sign(deltaY) * yThreshold);
}
			
// Restrict camera inside bounds
FP.clampInRect(FP.camera, 0, 0, xBounds, yBounds, 0);

And as a plus, it also fixed the zero threshold issue. No more screen shakes. As well as being much shorter and readable. :relaxed: Thanks!

And one question, what does “PD” in your reply mean?


(MartĂ­ Angelats i Ribera) #5

It’s PS (post scriptum) (Sorry about it, it’s different in my native lenguage where we use post data instead. Becouse it’s a latin form i assumed it was the same in english).


(Zachary Lewis) #6

PD stands for Pizza Dog.


(MartĂ­ Angelats i Ribera) #7

How do you find this videos? Too many internet? It seems you have one prepared for every single one of my fails XD


(Zachary Lewis) #8

Too many internet. :stuck_out_tongue: