Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera following player #3982

Closed
mirosssc opened this issue Apr 29, 2017 · 8 comments
Closed

Camera following player #3982

mirosssc opened this issue Apr 29, 2017 · 8 comments
Labels
🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim

Comments

@mirosssc
Copy link

So basically what is the most efficient way to do this with Pixi? I want

  1. camera basically to follow a player and change the background according to the position of player
  2. scroll the world only if player is for example 400 px from right end of canvas and 300 px from left end of canvas
  3. things like enemies, things on the ground and objects adapt accordingly(adapt with the scenery too).

Thanks in advance

@ivanpopelyshev
Copy link
Collaborator

ivanpopelyshev commented Apr 29, 2017

make "Camera" container, put everything there.

camera.position.set(renderer.screen.width/2, renderer.screen.height/2);
camera.pivot.copy(player.position);

//EVERY FRAME

var targetPivot = player.position;

//LERP IT, dt is something between 0 and 1.
// i use dt = 1 - Math.exp(-deltaInMillis / 100);
// or you can just assign targetpivot to pivot
camera.pivot.x = (targetPivot.x - camera.pivot.x) * dt + camera.pivot.x;
camera.pivot.y = (targetPivot.y - camera.pivot.y) * dt + camera.pivot.y;

//big square

var mapRect = new PIXI.Rectangle();
mapRect.x = camera.pivot.x - renderer.screen.width/2;
mapRect.y = camera.pivot.x - renderer.screen.height/2;
mapRect.width  = renderer.screen.width;
mapRect.height = renderer.screen.height;
mapRect.pad(400,400); // -this line was updated

//every time camera changes position


var newRect = new PIXI.Rectangle();
newRect .x = camera.pivot.x - renderer.screen.width/2;
newRect .y = camera.pivot.x - renderer.screen.height/2;
newRect .width  = renderer.screen.width;
newRect .height = renderer.screen.height;
if (newRect.x < mapRect.x || newRect.right > mapRect.right ||
   newRect.y < mapRect.y ||  newRect.bottom > mapRect.bottom) {
   mapRect = newRect;
   //ADJUST THE BACKGROUND AND STUFF
   //CLEAR AND FILL THE TILEMAP: https://github.com/pixijs/pixi-tilemap
}

@ivanpopelyshev ivanpopelyshev added the 🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim label Apr 29, 2017
@ivanpopelyshev
Copy link
Collaborator

ivanpopelyshev commented Apr 29, 2017

Somebody, please make that a pixi wiki article. I think Im explaining that thing for 10th time, we need a tutorial on that stuff

@mirosssc
Copy link
Author

Thank you. It does the job however I'm not able to accomplish 2. point. The result of mapRect.enlarge(400); is NaN. According to documentation there's supposed to be another rectangle.
If you could just explain it in a little more detail that would be great.
Thank you.

@ivanpopelyshev
Copy link
Collaborator

OK, use "pad(400,400)"" instead ;)

@mirosssc
Copy link
Author

mirosssc commented May 3, 2017

There still seems to be the problem because in every frame newRect.x is always gonna be bigger. I am missing something huge in here i guess :(

@ivanpopelyshev
Copy link
Collaborator

ivanpopelyshev commented May 3, 2017

@mirosssc

mapRect = newRect;
mapRect.pad(400,400);

also make sure you create newRect every time. If you dont, please use "mapRect.copy(newRect);" instead of assignment.

I wrote pseudo-code. I hope you copied that stuff into different functions like gameStart, gameFrame or so? Its not supposed to be in one function.

@GoodBoyDigital
Copy link
Member

Hi there! Closing this issue for now due to its inactivity. Feel free to give us a poke if you would like this issue reopened. Thanks 👍

@lock
Copy link

lock bot commented Feb 23, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Feb 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🤔 Question User question, similar to Help Wanted or Needs Help. These can be addressed whenever someone has tim
Projects
None yet
Development

No branches or pull requests

3 participants