Libgdx first person camera controll - java

I just started playing arround with 3D in libgdx. I allready know how to draw basic Models and i tryed to play arround with the CameraController. Now i want to create a FirstPersonCamera or FirstPersonCameraController. I thought about extending PerspectiveCamera and adding a MyMovingObject target to it. The MyMovingObject would hold a x, y, z position, where y is a constant value, cause i can't move up/down at the moment. So my movement is basicly in 2D. The MyMovingObject would also store the left/right rotation, needed for its moving direction/ xSpeed, zSpeed. But the Player should also be able to look up and down, and this up/down rotation is not really needed for the MyMovingObject, as it only changes the view and no other properties. So i am not sure if i go the right way.
I want to be able to go forward, left, right, backward by using W,A,S,D and rotate left right by using the mouse. Also i want to look up and down by using the mouse, like in most First Person games.
Should i use another way, not creating my own camera by extending PerspectiveCamera?
Or is this approach good and i just have to store the up/down rotation in the MyMovingObject to, also if it is only needed for the view?
Or would it be better to controll the camera with W,A,S,D and mouse and update the MyMovingObjects position, depending on cameras position and rotation?
I hope you understand what I mean. It seems a bit complicated to explain it (at least for me).
EDIT: I am now using Vector3 direction, Vector3 position and Vector3 size for my NPCs and the player. I calculate the speed by doing: xSpeed = direction.x / (direction.x + direction.z) * speed; the same for zSpeed. By doing this i "filter" the y value out of it and i get only the percent of x and y. The only problem is, that when i look straight up x and z are 0. I could fix this by using an UpVecotr, which gets rotated when i do a "Pitch-rotation". But how do i rotate him? I need to rotate it arround the sideway Vector. Thanks
EDIT: The rotation and movement work now (see my answer), but i have really big problems with the limitation of the "Pitch-rotation". I am using: if (direction.y < 0.9 && angle > 1) doPitchRotation(); else if (direction.y > -0.9 && angle < 1) doPitchRotation(); so if i rotate down and i still look down at least at -0.9 y it just does not perform the rotation. But what really happens: I rotates to - 0.9 then it rotates arround the Y-Axis and at the other side it rotates up, even if i move my mous down. Can you explain why? Why does the Y-Axis flip when i turn arround by looking down?
EDIT: It works now. It seems like my upVector got some wrong values sometimes. For landbased cams you can also use crossproduct of Y-Axis and direction Vector. No need for upVector.

Hey thanks for sharing this link. I found it very useful.
Here's my code on rotating a land based camera and it seems to work without problems.
private int mouseX = 0;
private int mouseY = 0;
private float rotSpeed = 0.2f;
#Override
public boolean mouseMoved(int screenX, int screenY) {
int magX = Math.abs(mouseX - screenX);
int magY = Math.abs(mouseY - screenY);
if (mouseX > screenX) {
cam.rotate(Vector3.Y, 1 * magX * rotSpeed);
cam.update();
}
if (mouseX < screenX) {
cam.rotate(Vector3.Y, -1 * magX * rotSpeed);
cam.update();
}
if (mouseY < screenY) {
if (cam.direction.y > -0.965)
cam.rotate(cam.direction.cpy().crs(Vector3.Y), -1 * magY * rotSpeed);
cam.update();
}
if (mouseY > screenY) {
if (cam.direction.y < 0.965)
cam.rotate(cam.direction.cpy().crs(Vector3.Y), 1 * magY * rotSpeed);
cam.update();
}
mouseX = screenX;
mouseY = screenY;
return false;
}
This works for landbased cameras. If you want to make a flightcontroll camera, you have to do a pitch rotation arround the cam.direction.crs(cam.up). Instead of using the Vector3.cpy() i would store a Vector3 help, which gets those temporary values, because Vector3.cpy() creates a new Vector3 and this operation is performed every render loop.
For flightcontroll cameras you also need to add a roll rotation and do the yaw rotation arround the cam.up Vector.

This article is really helpful in my opinion. I have found a solution which should work, but i haven't tryed it yet. My MovingObjects all have a Vector3 position, Vector3 direction, Vector3 size and Vecotr3 upVector. The Player class extends this MovingObject class and adds Mouse and Keycontroll to the movement.
In the MovingObject class i have the moethods:
rotateYaw(float degrees): rotates the Vector3 direction arround the Y-Axis by the given degrees (libgdx has a rotate function for Vector3)--> Simple
rotatePitch(float degrees): rotates the Vector3 direction arround the: direction.cross(Vector3.Y), which is the rotated side Vector of your MovingObject, by the given degrees. Also a Pitch-Rotation has to rotate the upVector, so you rotate the upVector arround the same axis, by the given degrees. As soon as you understand this it is simple.
move(delta) moves your MovingObject in x,z direction by doing:
if (direction.y == 1) {
// You are looking straight up, no x,z direction, move in the opposite
// direction of upVector
xSpeed = upVector.x / (Math.abs(upVetor.x) + Math.abs(upVector.z)) * (-speed);
zSpeed = upVector.z / (Math.abs(upVetor.x) + Math.abs(upVector.z)) * (-speed);
position.add(xSpeed * delta, 0, ySpeed * delta);
} else if (direction.y == -1) {
// You are looking straight down, no x,z direction, move in the direction of
// upVector
xSpeed = upVector.x / (Math.abs(upVetor.x) + Math.abs(upVector.z)) * speed;
zSpeed = upVector.z / (Math.abs(upVetor.x) + Math.abs(upVector.z)) * speed;
position.add(xSpeed * delta, 0, ySpeed * delta);
} else {
// You are not looking straight up or down, so you have x,z direction. Use
// that.
xSpeed = direction.x / (Math.abs(direction.x) + Math.abs(direction.z)) * speed;
zSpeed = direction.z / (Math.abs(direction.x) + Math.abs(direction.z)) * speed;
position.add(xSpeed * delta, 0, ySpeed * delta);
}
I did not test this until now, but i think it should work. Note, that in the Pitch-rotation you should also limit it to straight up/ straight down. Do this by checking the signum of x and z. If they change while you are doing a Pitch-rotation you rotated over 90 degrees.
I am stil waiting for other answers and if i am wrong please correct me!
EDIT: I tested it. It works like this, but there are a few things to take care of:
direction.cross(upVector) changes the direction Vector. So store that data somewhere first! After using it reset the direction Vector.
The Pitch limitation has a problem: If you controll signum change, as i suggested the following happens: you look straight up, signum x and signum z are 0. You look down, signum changes and your action (limiting) starts. So take care that you also check, if it is not zero.
I stil don't know how to do the pitch limitation and i edit my question to explain my issue.
Think about normalizing your direction and upVector whenever you change something!
I think this should work pretty good. If you have any improvements let me know and i will update this here. If you have another solution please add an answer! Thanks

I know that this question already has good answers. But I had some issues with the selected answer. And I just want to help someone who is looking for the same solution.
I noticed some strange behaviour with the selected answer. I doesn't keep the Y exis as up. Which is of course very important on a fps. So this one is not perfect but I wanted to put this here.
// put into the create() method.
Gdx.input.setInputProcessor(new InputProcessor() {
private int dragX, dragY;
float rotateSpeed = 0.2f;
// dont' forget to override other methods.
#Override
public boolean mouseMoved(int screenX, int screenY) {
Vector3 direction = cam.direction.cpy();
// rotating on the y axis
float x = dragX -screenX;
// change this Vector3.y with cam.up if you have a dynamic up.
cam.rotate(Vector3.Y,x * rotateSpeed);
// rotating on the x and z axis is different
float y = (float) Math.sin( (double)(dragY -screenY)/180f);
if (Math.abs(cam.direction.y + y * (rotateSpeed*5.0f))< 0.9) {
cam.direction.y += y * (rotateSpeed*5.0f) ;
}
cam.update();
dragX = screenX;
dragY = screenY;
return true;
}
});
NOTE: Don't use any camera controllers.
NOTE2: This might come handy: Gdx.input.setCursorCatched(true);
EDIT: I just wanted to share the walking function I use to change position of the camera with wasd keys. When W key is down the forward is true. And when the key is up forward is false. Other directions have the same principle. To detect the key down and up, please use the InputProcessor in the above code.
This creates movement in the two dimensional space (X-Z axises). The direction of the camera will not change the direction of the movement as we are eliminating the Y axis. of the direction.
One must now even if camera is directed to the sky (at a non 90 degree angle with the ground), the length of the direction vector is not fixed to 1.0f. So there will not be any loss of movement speed.
To test this I rotated camera up and down (moved the mouse forward and backward) and the x and z values of the direction vector didn't change. So when the y axis of the direction vector is eliminated, we have a 2d direction vector which doesn't effected by the Y angle of the camera.
private void walking(float timeElapsed) {
float speed = movementSpeed;
if ((forward | back) & (right | left)) {
speed /= Math.sqrt(2);
}
System.out.println(speed);
if (forward) {
Vector3 v = cam.direction.cpy();
v.y = 0f;
v.x *= speed * timeElapsed;
v.z *= speed * timeElapsed;
cam.translate(v);
cam.update();
}
if (back) {
Vector3 v = cam.direction.cpy();
v.y = 0f;
v.x = -v.x;
v.z = -v.z;
v.x *= speed * timeElapsed;
v.z *= speed * timeElapsed;
cam.translate(v);
cam.update();
}
if (left) {
Vector3 v = cam.direction.cpy();
v.y = 0f;
v.rotate(Vector3.Y, 90);
v.x *= speed * timeElapsed;
v.z *= speed * timeElapsed;
cam.translate(v);
cam.update();
}
if (right) {
Vector3 v = cam.direction.cpy();
v.y = 0f;
v.rotate(Vector3.Y, -90);
v.x *= speed * timeElapsed;
v.z *= speed * timeElapsed;
cam.translate(v);
cam.update();
}
}

Related

Java little ball runs away from cursor

I'm trying to make a graphic project where a Ball runs away from my cursor, I already did the other way around where the ball seeks my cursor and when she arrives she loses velocity so it's like she's running fast until she comes around a range of 10 pixels and then she loses velocity until she touches the cursor.
The thing is, I can't find a way to make the ball run away from the cursor in a way that when I enter a diameter(from the ball), she runs slow, if I approach more she starts to run faster to get away but when my cursor leaves the diameter, she runs slow until she stops once again.
I hope I made it clear, I thought about a solution but I don't know if there's a library or some built function in Java that I could use guys:
-have like a percentage from 0 to 100 where the distance between my cursor and the ball fits inside, 0% is velocity=0, 100% is velocity=4 for example, do you have any idea if there is such thing that I could implement?
Thank you in advance!
I've made a Vector class where I change it and access the X and Y coordinates to make the ball move, I used basic trigonometry to make the vector ALWAYS the same length.
code of my ball (Chaser) class:
public class Chaser {
private double x;
private double y;
private double vel = 1;
private double hyp;
private Vector vector = new Vector(0, 0);
private double distance;
public Chaser(int width, int height){
x = width/2;
y = height/2;
}
public void setVel(Point m){
if(m.x != x)
hyp = Math.sqrt(Math.pow(Math.abs(m.x - x), 2) + Math.pow(Math.abs(m.y - y), 2));
else
hyp = Math.abs(m.y - y);
}
public void setDirection(Point m){
if(hyp == 0) return;
vector.change((m.x - x)/hyp, (m.y - y)/hyp);
}
public void draw(Graphics g){
g.setColor(Color.RED);
g.fillOval((int)x - 10, (int)y - 10, 20, 20);
g.setColor(Color.BLACK);
g.drawLine((int)x, (int)y, (int)(vector.getX()*15*vel) + (int)x, (int)(vector.getY()*15*vel) + (int)y);
}
public void move(Point m){
setVel(m);
setDirection(m);
useVector();
}
public void useVector(){
if(vector == null) return;
x -= vector.getX() * vel;
y -= vector.getY() * vel;
}
public void calculateVelocity(Point m){
if(vector == null) return;
// I don't know what to do yet
}
}
If you want to just push the ball around you can do something simple. Let's use vectors to make it easier to understand. Say ball holds the ball's center (x,y) and mouse contains the mouse position (x,y).
You can compute the distance between ball and mouse, that is (mouse - ball).length() to get how far away the mouse is from the ball.
If the distance > ball radius then the mouse is outside.
Otherwise you can do:
tmp = ball - mouse // get the vector from mouse to the ball.
tmp = tmp / tmp.length() * ball_radious // change the vector's length to match the radious of the ball.
ball = mouse + tmp // Move the ball such that the mouse will be on the edge.
As you move the mouse the ball will get pushed by the mouse.
If you want a bit of inertia, so the ball doesn't just stop when you don't push it anymore then you need to keep an additional vector speed and use tmp to get an acceleration.
Something like this:
tmp = ball - mouse // get the vector from mouse to the ball.
force = max(0, ball_radious - tmp.length()) // how strong we push the ball.
acceleration = tmp / tmp.legnth() * f(force) // compute the acceleration vector. f(force) is some function based on force, try k*f or k*f*f and see what looks better for your setup.
speed = speed * kDrag + acceleration // update the speed, kDrag should be between 0 and 1, start with something like 0.8 and try different values.
ball = ball + speed * time_delta // Update the ball's position.
You can play with the constants to get the right feel that you're looking for. time_delta is meant to normalize the speed between frams so you don't need to worry too much if there's some inconsistency between them. You can use a constant as well, but the movement might become jerky at times.
All operations above are vector operations.

Moving Body using force in Libgdx based off of touch input

What I am trying to do is take in an inputted user touch down, convert the camera coordinates of the touch down to world coordinates, then create a vector to use in order to apply force to my body.
This is what I have for that:
Vector3 worldPos = new Vector3(newX, newY, 0);
GameScreen.gameCam.unproject(worldPos);
newX = worldPos.x;
newY = worldPos.y;
Vector2 direction = new Vector2();
direction.set((newX - xPos), (newY - yPos));
if (newX <= xPos){
direction.set((xPos - newX), (yPos - newY));
}
b2Body.applyForceToCenter(direction, true);
I know that this really isn't right at all as far as creating the vector goes, but the coordinates are being converted properly.
So my question is, how can I get an accurate vector, even in the negative direction, when I have both the world coordinates of the body and touch down?
I have tried doing some research to get a better understanding of all of this but I am having a bit of trouble. Any help would be greatly appreciated, thank you for your time.
If you have on newX, newY screen touch coordinates and next change it to world coords you can get direction by subtraction touchWorldPos to currentObjectPos:
Vector3 worldPos = new Vector3(newX, newY, 0);
GameScreen.gameCam.unproject(worldPos);
newX = worldPos.x;
newY = worldPos.y;
Vector2 direction = new Vector2();
direction.set((newX - xPos), (newY - yPos));
b2Body.applyForceToCenter(direction, true);
without this if statement:
if (newX <= xPos){
direction.set((xPos - newX), (yPos - newY));
}
in box2d this direction might have small values and you must muliply this one by some value like 100(mayby more)

Android Java - postRotate not rotating around center?

I believe this is more of a logic question than a java question, sorry.
My intent is rather straightforward, i want the ship to move and rotate with a matrix, with the bitmap ship1 being the center pivot of the rotation. The code works great except the pivot is off by a strange offset. (picture of conundrum linked at bottom)
The default value rotation at 0 works but all the other values seem to slide away from the center, with 180 being the furthest from the center.
centerX = playerValues[Matrix.MTRANS_X] + ship1.getWidth()/2;
centerY = playerValues[Matrix.MTRANS_Y] + ship1.getHeight()/2;
newRotation = ((float) Math.toDegrees(Math.atan2(fingery1 - centerY, fingerx1 - centerX)));
matrix.postRotate((newRotation - prevRotation), centerX, centerY);
prevRotation = newRotation;
if (fingerx1 > playerX) {
xspeed = 1;
} else
if (fingerx1 < playerX) {
xspeed = 0;
} else
if (fingery1 > playerY) {
yspeed = 1;
} else
if (fingery1 < playerY) {
yspeed = 0;
}
matrix.postTranslate(xspeed, yspeed);
matrix.getValues(playerValues);
I tried to draw how the relation of the bitmap looks at different angles. (the blue dot is where I intend to rotate the bitmap around, the arrow pointing right is the only correct one).
http://i.stack.imgur.com/2Yw76.png
Please let me know if you see any errors or any feedback helps! I just need a second pair of eyes on this because mine are going to explode soon.
Consider studying a good computer graphics text re matrix math. Foley and Van Dam is always a safe bet.
The matrix A is applied to point x with multiplication Ax. You have A = RT a rotation with translation post multiplied. The result is RTx which is R (T x) meaning the point is translated then rotated, when you probably meant the opposite.
Additionally it appears you are concatenating incremental changes repeatedly. Floating point errors will accumulate, visible as worsening distortions. Instead maintain orientation parameters x, y, theta for each ship. These are controlled by the UI. Set the matrix from these in each rendering. The transform will be rotation about the point (w/2, h/2) followed by translation to (x, y). But the matrix to effect this is the translation post multiplied by the rotation! Also you must reset the matrix for each ship.

LibGDX - Orthogonal Camera won't stop at edge of map

i'm fairly new to libgdx and java but am learning everything I can! I'm working on an orthogonal 2d tiled game. Basically, i've got my character walking around and the camera following him. I am trying to set it up so the camera stops when the character is near the edge of the map so no black space is seen. The camera should stop at the edge and the player should continue moving.
This is my code for moving the camera. It's a bit messy now as i've been trying everything. The map is 30x30.
if (mainPlayer.getPosition().x >= 15 && mainPlayer.getPosition().x <= 30) {
camera.position.x = mainPlayer.getPosition().x;
}
camera.position.y = mainPlayer.getPosition().y;
camera.update();
camera.apply(gl);
This is inside my render method. I've only messed with the x part, so ignore the y for now.
EDIT I think I need to reword that. I know how to make it stop..It works using my code, but I can't figure out how to determine where to stop it. I'm using 15 in my code above which is half the map..which of course doesn't work.
EDIT I think I need to reword that. I know how to make it stop..It
works using my code, but I can't figure out how to determine where to
stop it. I'm using 15 in my code above which is half the map..which of
course doesn't work.
The minimum x would need to be:
map.position.x + camera.viewportWidth/2;
And the maximum x would need to be:
map.position.x+mapwidth-camera.viewportWidth/2;
Assuming the map position is in (0,0) you can just ignore the map.position.x part.
You need to fix the bounds of the Camera rectangle to inside the world rectangle. A function such as below would help:
public void fixBounds() {
float scaledViewportWidthHalfExtent = viewportWidth * zoom * 0.5f;
float scaledViewportHeightHalfExtent = viewportHeight * zoom * 0.5f;
// Horizontal
if (position.x < scaledViewportWidthHalfExtent)
position.x = scaledViewportWidthHalfExtent;
else if (position.x > xmax - scaledViewportWidthHalfExtent)
position.x = xmax - scaledViewportWidthHalfExtent;
// Vertical
if (position.y < scaledViewportHeightHalfExtent)
position.y = scaledViewportHeightHalfExtent;
else if (position.y > ymax - scaledViewportHeightHalfExtent)
position.y = ymax - scaledViewportHeightHalfExtent;
}

Rotating an image gradually from one degree to another

This is my first post, so please bear with me. I'll try and be as clear as I can here about my problem.
First off, let me say that I suck at math. I failed it in school, and it pains me to no end that I can't grasp simple math concepts, especially since I love to code. Someone who wants to program, and can't perform basic math operations? Bad combo.
Aaaanyway, on to the problem.
Some context. I am writing an asteroids-type game. It's coming along quite nicely, and I've overcome all my hurdles so far thanks to this site (thank you!), and google. I've searched high and low to a solution to my problem, but it always seems like I run into a solution that either doesn't work, or I just don't understand and can't incorporate it into my code.
The issue involves rotation of the ship. I have an onscreen joystick class that returns the angle that the joystick is being pushed. I use that angle to point the ship in that same direction.
What I want to do is gradually turn the ship towards the angle the user wants to go, using the shortest turn, left or right. In my mind I'm thinking "How the hell do I go to say.. 350 degrees from 5 degrees, going left?". I don't know...
Here is my draw code:
public void draw(Canvas canvas){
canvas.save();
canvas.rotate((float) (fAngle + 90), (float) (dX + (mShip.getIntrinsicWidth() / 2)), (float) (dY + (mShip.getIntrinsicHeight() / 2)));
mShip.setBounds((int)dX, (int)dY, (int)dX + mShip.getIntrinsicWidth(), (int)dY + mShip.getIntrinsicHeight());
mShip.draw(canvas);
canvas.restore();
}
The angle is passed to the fAngle variable from the joystick getangle method. The angle is then increased by 90 degrees because of the image facing.
user.fAngle = oJoystick.getAngle();
So on each game tick, I want to turn the ship towards whatever direction the player wants to go, degree by degree. Any help with this would be greatly appreciated!
Thanks for reading!
In your tick function, instead of:
user.fAngle = oJoystick.getAngle();
Use this (adjust increment to your liking - this controls the rotation speed):
const float increment = 1.0;
float direction;
float joy = oJoystick.getAngle();
float ang = user.fAngle;
float fudge = 5.0;
if (abs (joy - ang) > fudge) {
if (joy > ang) {
if (joy - ang < 180)
direction = 1;
else
direction = -1;
} else if (joy < ang) {
if (ang - joy < 180)
direction = -1;
else
direction = 1;
}
} else // already pointing right direction
direction = 0;
user.fAngle = ang + direction * increment;
if (0 > user.fAngle)
user.fAngle += 360;
if (360 < user.fAngle)
user.fAngle -= 360;

Categories

Resources