Skip to main content
Mod Moved Comments To Chat
added 1234 characters in body
Source Link
J22o
  • 482
  • 2
  • 4

Now, in your case there are a few things you should change:

public void updateCamera(){
 a = MouseInfo.getPointerInfo();
 b = a.getLocation();
 mX = (int)b.getX();
 mY = (int)b.getY();
 cX = (mX - xPos) * dist + xPos;
 cY = (mY - yPos) * dist + yPos;
 
 float dirX = (cX - lX);
 float dirY = (cY - lY);
 float length = (float)Math.sqrt(dirX * dirX + dirY * dirY) - 0.6f;
 
 float outX = lX;
 float outY = lY;

 if(length > minDist) {
     dirX /= length;
     dirY /= length;
     dirX *= delta + 0.5f;
     dirY *= delta + 0.5f;
     outX = lX + dirX;
     outY = lY + dirY;
 }
 x = (int)outX;
 y = (int)outY;
}

Also, before you ask: I have taken a look at your code and haven't found the camera being used anywhere. So I thought you were having problems with that as well. Here is a solution: You can add an x and y offset to the position of every thing that is rendered, like entities and the player, but not the UI. And in each update you set that x and y offset to the negative x and y position of the camera. In that way the world will move around, creating the feeling that there is a camera.

Now, in your case there are a few things you should change:

public void updateCamera(){
 a = MouseInfo.getPointerInfo();
 b = a.getLocation();
 mX = (int)b.getX();
 mY = (int)b.getY();
 cX = (mX - xPos) * dist + xPos;
 cY = (mY - yPos) * dist + yPos;
 
 float dirX = (cX - lX);
 float dirY = (cY - lY);
 float length = (float)Math.sqrt(dirX * dirX + dirY * dirY) - 0.6f;
 
 float outX = lX;
 float outY = lY;

 if(length > minDist) {
     dirX /= length;
     dirY /= length;
     dirX *= delta + 0.5f;
     dirY *= delta + 0.5f;
     outX = lX + dirX;
     outY = lY + dirY;
 }
 x = (int)outX;
 y = (int)outY;
}

Also, before you ask: I have taken a look at your code and haven't found the camera being used anywhere. So I thought you were having problems with that as well. Here is a solution: You can add an x and y offset to the position of every thing that is rendered, like entities and the player, but not the UI. And in each update you set that x and y offset to the negative x and y position of the camera. In that way the world will move around, creating the feeling that there is a camera.

added 1024 characters in body
Source Link
J22o
  • 482
  • 2
  • 4

Lets start of by defining a few variables. Lets say that o is the position of the player, d is the point that we want the player to look at, c is the position of the camera and dist is how much do we want to move c towards d, were dist is a float between 0 and 1.

float cX = (dX - oX) * dist + oX;
float cY = (dY - oY) * dist + oY;

In case you want to move smoothly towards that point you can use the following method, where L the previous camera position, C is the one we just calculates using the method above, out is the smooth position, and minDist the distance from C that when reached the camera will stop moving:

float dirX = (cX - lX);
float dirY = (cY - lY);
float length = (float)Math.sqrt(dirX * dirX + dirY * dirY) - 0.6f;
dirX /= length;
dirY /= length;
dirX *= delta;
dirY *= delta;
float outX = lX + dirX;
float outY = lY + dirY;

In sum, all of the above would look something like this:

float cX = (dX - oX) * dist + oX;
float cY = (dY - oY) * dist + oY;

float dirX = (cX - lX);
float dirY = (cY - lY);
float length = (float)Math.sqrt(dirX * dirX + dirY * dirY) - 0.6f;

float outX = lX;
float outY = lY;
    
if(length > minDist) {
    dirX /= length;
    dirY /= length;
    dirX *= delta + 0.5f;
    dirY *= delta + 0.5f;
    outX = lX + dirX;
    outY = lY + dirY;
}

Lets start of by defining a few variables. Lets say that o is the position of the player, d is the point that we want the player to look at, c is the position of the camera and dist is how much do we want to move c towards d, were dist is a float between 0 and 1.

float cX = (dX - oX) * dist + oX;
float cY = (dY - oY) * dist + oY;

Lets start of by defining a few variables. Lets say that o is the position of the player, d is the point that we want the player to look at, c is the position of the camera and dist is how much do we want to move c towards d, were dist is a float between 0 and 1.

float cX = (dX - oX) * dist + oX;
float cY = (dY - oY) * dist + oY;

In case you want to move smoothly towards that point you can use the following method, where L the previous camera position, C is the one we just calculates using the method above, out is the smooth position, and minDist the distance from C that when reached the camera will stop moving:

float dirX = (cX - lX);
float dirY = (cY - lY);
float length = (float)Math.sqrt(dirX * dirX + dirY * dirY) - 0.6f;
dirX /= length;
dirY /= length;
dirX *= delta;
dirY *= delta;
float outX = lX + dirX;
float outY = lY + dirY;

In sum, all of the above would look something like this:

float cX = (dX - oX) * dist + oX;
float cY = (dY - oY) * dist + oY;

float dirX = (cX - lX);
float dirY = (cY - lY);
float length = (float)Math.sqrt(dirX * dirX + dirY * dirY) - 0.6f;

float outX = lX;
float outY = lY;
    
if(length > minDist) {
    dirX /= length;
    dirY /= length;
    dirX *= delta + 0.5f;
    dirY *= delta + 0.5f;
    outX = lX + dirX;
    outY = lY + dirY;
}
Source Link
J22o
  • 482
  • 2
  • 4

Lets start of by defining a few variables. Lets say that o is the position of the player, d is the point that we want the player to look at, c is the position of the camera and dist is how much do we want to move c towards d, were dist is a float between 0 and 1.

float cX = (dX - oX) * dist + oX;
float cY = (dY - oY) * dist + oY;