Working on FP.camera x and y makes the center of camera not being player anymore[SOLVED]


(billy2000) #1

So i am working on a new project. Over here the player should be in the center of the camera.But also i want to make the camera follow the mouse a bit.This being said , i have this code:

override public function update():void
	{
		super.update();
		cameraFollowTurgetAndPlayer();
	}
	public function cameraFollowTurgetAndPlayer():void
	{
		calculateingMouseDiff();
		FP.camera.x = ((G.Px + 4) - FP.halfWidth) - mouseDiffX;
		FP.camera.y = ((G.Py + 4) - FP.halfHeight) - mouseDiffY;
		
	}
	public function calculateingMouseDiff():void
	{
		//px,py - player coordinates
		
		if (G.Px > G.Tx) {
			mouseDiffX = (G.Px - Input.mouseX) / 10;
		}
		else {
			mouseDiffX = (Input.mouseX - G.Px) / 10;
			mouseDiffX = -mouseDiffX;
		}
		
		if (G.Py > G.Ty) {
			mouseDiffY = (G.Py - Input.mouseY) / 10;
		}
		else {
			mouseDiffY = (Input.mouseY - G.Py) / 10;
			mouseDiffY = -mouseDiffY;
		}
	}

The problem is that when the player moves, mouseDiffX and mouseDiffy variables increase. And here is the part when i have no ideas left how to fix it. Do you guys have any? Thx.


(Ssnyder Coder) #2

The problem is that Input.Mouse gives you the screen coordinates of the mouse, not the world coordinates that entities have. To get the world coordinates of the mouse position, you would have to offset it by the camera’s world coordinates. Something like this:

override public function update():void
{
	super.update();
	cameraFollowTurgetAndPlayer();
}
public function cameraFollowTurgetAndPlayer():void
{
	calculateingMouseDiff();
	FP.camera.x = ((G.Px + 4) - FP.halfWidth) - mouseDiffX;
	FP.camera.y = ((G.Py + 4) - FP.halfHeight) - mouseDiffY;

}
public function calculateingMouseDiff():void
{
	//px,py - player coordinates
	//Get mouse's world coordinates
	var mouseX:Number = FP.camera.x - FP.halfWidth + Input.mouseX;
	var mouseY:Number = FP.camera.y - FP.halfHeight + Input.mouseY;
	if (G.Px > G.Tx) {
		mouseDiffX = (G.Px - mouseX) / 10;
	}
	else {
		mouseDiffX = (mouseX - G.Px) / 10;
		mouseDiffX = -mouseDiffX;
	}

	if (G.Py > G.Ty) {
		mouseDiffY = (G.Py - mouseY) / 10;
	}
	else {
		mouseDiffY = (ImouseY - G.Py) / 10;
		mouseDiffY = -mouseDiffY;
	}
}

You’ll have to modify this solution slightly, otherwise the screen will never stop moving since the mouse’s world position will constantly keep updating.

EDIT: A better solution would be to first center the camera on your player and then offset it based on the mouse position. Something like this:

override public function update():void
{
	super.update();
	cameraFollowTurgetAndPlayer();
}
public function cameraFollowTurgetAndPlayer():void
{
	centerCameraOnPlayer();
	offsetCameraFromTarget();
}
public function centerCameraOnPlayer():void
{
	FP.camera.x = ((G.Px + 4) - FP.halfWidth);
	FP.camera.y = ((G.Py + 4) - FP.halfHeight);
}
public function offsetCameraFromTarget():void
{
	FP.camera.x += (Input.mouseX - FP.halfWidth) / 10;
	FP.camera.y += (Input.mouseY - FP.halfHeight) / 10;
}

(Bora Kasap) #3

why you’re getting this mouse diff stuff if your player always at the center?

just take the difference between mouse and stage center

then add this difference to camera position by dividing it to some number

i mean you can work with Input.mouseX, Input.mouseY instead of mouseX, mouseY if your player at the center always


(billy2000) #4

Ty guys for the response^^. @ssnyder​​Coder Great.It worked. Ty :smile: @Alobar​Non: Ty for the idea but i think(if i understood right) it will not work in my situation because the player also moves on the map,and the camera should fallow player and mouse.And that is why i used mouseDiff, to make a link between mouse and player for camera to follow.


(Bora Kasap) #5

that’s why you’re using “camera” :slight_smile: anyway… you solved your problem :smiley: no matter