Rotate player using libgdx touchpad to face moving direction - java

i have this Problem now for over 2 days. Constantly tweaking. I just cant get it done.
I have a Player texture ( Player is facing the left on it ) which i want to rotate using the touchpad. So the player will be facing his running direction.
So far i have this :
double facerotation = Math.atan2(touchpad.getKnobPercentY(), touchpad.getKnobPercentX());
spriteBatch.draw(runningFrame, player.getPosition().x, player.getPosition().y, Player.getSize() / 2, Player.getSize() / 2, Player.getSize(), Player.getSize(), 1, 1, facerotation * 100, false);
But with "roation*100" he spins like 2 times around and without he barely rotates. I even tried switching the X and Y values for the atan2 function above. But i never got him rotate only in the direction i am Moving. I also tried the atan function, also with swapping the X and Y values.
Please help me. I tried thousands of ways, Different calculations and things i saw on google. Nothing brought me the desired effect.

Just use a Vector2. Use it to store your knob percent y and x. Then you can get the rotatation in degrees with vector2.angle().
Vector2 v = new Vector2(touchpad.getKnobPercentX(), touchpad.getKnobPercentY());
float angle = v.angle();
runningFrame.setRotation(angle);

Related

LWJGL First Person Camera Rotation

I have run into a problem making a first person camera on LWJGL 2. I am using the following code to rotate the camera (up down left and right) based on how the mouse moves. This is basically what every other tutorial has, however, its movement is flawed and ends up spiraling out of control.
float mouseDX = Mouse.getDX();
float mouseDY = Mouse.getDY();
rotation.x = mouseDX;
rotation.y = mouseDY;
glRotatef(rotation.y, 1, 0, 0);
glRotatef(rotation.x, 0, 1, 0);
Rotation is a Vector3f
I am aware that the rotation.y is rotating the x access and the x is rotating the y. I am not totally sure why but it doesn't work for me unless its this way. The problem may be related to this.
Here is a video I made showing what I mean:
https://www.youtube.com/watch?v=V6Iu5oQuWo4&feature=youtu.be
In the video I attempt to show that both the x and y rotation work fine separately, but when used together they don't work at all.
I know this is only a small section of my code, but it is the only part dealing with rotation so the problem must be there somewhere.
The flaw that stands out to me is the value by which you rotate.
Mouse.getDY returns the change in y pixels so if you move your mouse half way down the screen you will move typically 300 pixels (800x600).
Now you also have glRotatef which rotates by radians which compared are tiny compared to degrees.(360 degrees -> 6.28 radians)
Now take 300 hundred pixels, use it as the number of radians to rotate by and you get 17188.7 degrees of rotation.
And that's the cause of your spiralling (47 revs/few milliseconds)
What you will need to do if divide your dy and dx by a good couple of hundred.
And you can also still use degrees by using Math.toRadians in the glRotatef method

Android translated canvas collision of rectangles

im trying do develop a Zelda like game. So far i am using bitmaps and everything runs smooth. At this point the camera of the hero is fixed, meaning, that he can be anywhere on the screen.
The problem with that is scaling. Supporting every device and keeping every in perfect sized rects doesnt seem to be that easy :D
To prevent that i need a moving camera. Than i can scale everything to be equally sized on every device. The hero would than be in the middle of the screen for the first step.
The working solution for that is
xCam += hero.moveX;
yCam += hero.moveY;
canvas.translate(xCam,yCam);
drawRoom();
canvas.restore();
drawHero();
I do it like this, because i dont wand to rearrange every tile in the game. I guess that could be too much processing on some devices. As i said, this works just fine. the hero is in the middle of the screen, and the whole room is moving.
But the problem is collision detection.
Here a quick example:
wall.rect.intersects(hero.rect);
Assuming the wall was originally on (0/0) and the hero is on (screenWitdh/2 / screenHeight/2) they should collide on some point.
The problem is, that the x and y of the wall.rect never change. They are (0/0) at any point of the canvas translation, so they can never collide.
I know, that I can work with canvas.getClipBounds() and then use the coordinates of the returned rect to change every tile, but as I mentioned above, I am trying to avoid that plus, the returned rect only works with int values, and not float.
Do you guys know any solution for that problem, or has anyone ever fixed something like this?
Looking forward to your answers!
You can separate your model logic and view logic. Suppose your development dimension for the window is WxH. In this case if your sprite in the model is 100x100 and placed at 0,0, it will cover area from 0,0 to 100, 100. Let's add next sprite (same 100x100 dimension) at 105,0 (basically slightly to the right of the first one), which covers area from 105,0 to 205,100. It is obvious that in the model they are not colliding. Now, as for view if your target device happens to be WxH you just draw the model as it is. If your device has a screen with w = 2*W, h = 2*H, so twice as big in each direction. You just multiply the x and y by w / W and h / H respectively. Therefore we get 2x for x and y, which on screen becomes 1st object - from 0,0 to 200, 200, 2nd object - from 210,0 to 410, 200. As can be seen they are still not colliding. To sum up, separate your game logic from your drawing (rendering) logic.
I think you should have variables holding the player's position on the "map". So you can use this to determine the collision with the non changing wall. It should look something like (depensing on the rest of your code):
canvas.translate(-hero.rect.centerX(), -.rect.centerY());
drawRoom();
canvas.restore();
drawHero();
Generally you should do the calculations in map coordinates, not on screen. For rendering just use the (negative) player position for translation.

Make the player move forward (facing towards its front) in LibGDX

I am currently working on a game in LibGDX. You may have seen another post. I have solved that problem, but now, I am facing the fact that I can't seem to move my player towards (it always moves up instead of pointing towards its front). I have tried everything, from using Box2D to answers I found online that suggested to use trig functions. Has anyone managed to do this before?
The movement of objects is usualy done by setting their x (left and right) and y (up and down) coordinates.
To know how to manipulate them, you need to know the direction he is faicng.
In a top-down game it could be a Vector2, a normalized Vector (a Vector of length=1), which gives you the relative movement in x and y direction. In a platformer instead it could be only a boolean, which defines if you are facing the rigth or the left. The up/down movement is then defined by jumping and other "up-forces" and the gravitation.
I think in your case it is a top-down-game, so you need a Vector2.
When you rotate your character you need to rotate the Vector2 to. To make sure, your Vector2 is normalized allways call nor() after manipulating it.
In the update or render method you should the set the x and y coordinates of your character like this:
player.x = direction.x*delta*speed;
player.y = direction.y*delta*speed;
Where delta is the Gdx.Graphics.getDeltaTime() and speed is the movementspeed of your player.
Hope it helps

Rotation in OpenGl ES to place objects then rotate the world

I am developing an augmented reality application for android and trying to use openGl to place cubes at locations in the world. My current method can be seen in the code below:
for(Marker ma: ARData.getMarkerlist().values()) {
Log.d("populating", "");
gl.glPushMatrix();
Location maLoc = new Location("loc");
maLoc.setLatitude(ma.lat);
maLoc.setLongitude(ma.lng);
maLoc.setAltitude(ma.alt);
float distance = currentLoc.distanceTo(maLoc);
float bearing = currentLoc.bearingTo(maLoc);
Log.d("distance", String.valueOf(distance));
Log.d("bearing", String.valueOf(bearing));
gl.glRotatef(bearing,0,0,1);
gl.glTranslatef(0,0,-distance);
ma.cube.draw(gl);
gl.glPopMatrix();
}
gl.glRotatef(y, 0, 1, 0);
gl.glRotatef(x, 1, 0, 0);`
Where y is yaw and x is the pitch. currently I am getting a single cube on the screen at a 45 degree angle someway in the distance. It looks like I am getting sensible bearing and distance values. Could it have something to do with the phones orientation? If you need more code let me know.
EDIT: I updated bearing rotation to gl.glRotatef(bearing,0,1,0); I am now getting my cubes mapped horizontally along the screen at different depths. Still no movement using heading and pitch but #Mirkules has identified some reasons why that might be.
EDIT 2: I am now attempting to place the cubes by rotating the matrix by the difference in angle between heading and bearing to a marker. However, all I get is a sort of jittering where the cubes appear to be rendered in a new position and then jump back to there old position. Code as above except for the following:
float angleDiff = bearing - y;
gl.glRotatef((angleDiff),0,1,0);
gl.glTranslatef(0,0,-distance);
bearing and y are both normalised to a 0 - 360 scale. Also, I moveed my "camera rotation" to above the code where I set the markers.
EDIT 3: I have heading working now using, float angleDiff = (bearing + y)/2;. However, I cant seem to get pitch working. I have attempted to use gl.glRotatef(-x,1,0,0); but that doesn't seem to work.
It's tricky to tell exactly what you're trying to do here, but there are a few things that stick out as potential problems.
Firstly, your final two rotations don't seem to actually apply to anything. If these are supposed to represent a movement of the world or camera (which mostly amounts to much the same thing) then they need to happen before drawing anything.
Then your rotations themselves perhaps won't entirely do what you intend.
Your cube is rotated around the Z axis. The usual convention in GL is for the camera to look down the Z axis, with the Y axis being considered 'up'. You can naturally interpret axes however you like, but a rotation around 'Z' would not typically be 'bearing', but 'roll'. 'Bearing' to me would be analogous to 'yaw'.
As you translate along the Z axis, I assume you are trying to position the object by rotating and translating, but obviously if the rotation is around the same axis as you translate along, it won't actually alter the position of the cube - it will always just be directly in front of the camera, spinning on its axis.
I'm not really clear on why you're trying to position the cube like that when it seems like you start off with a more specific location. You could probably directly construct a more appropriate matrix.
Finally, your camera/world rotation is two concatenated rotations around Y and X. You call these pitch and roll, but typically using euler angles for a camera rotation does not result in an intuitive result where terms like pitch and roll make complete sense. It is common to maintain an orientation and apply individual rotations to that in order to update it, rather than attempting to update several dependent rotations.
So yes, I would expect that this code, in the absence of other matrix operations, would likely result in drawing one or more cubes straight ahead which are simply rotated by some angle around the view direction.

Rotate player to face objective java slick

So I am currently working on learning slick. I was doing fine until I ran into a problem. I have been trying to find a answer for a good hour and could not. So I decided to post it on here.
My Problem: I have player on a 800 X 800 Grid. I am trying to make the player move to a certain point on the grid in a straight line. Now I can make him move on the X then turn and move on the Y, But I want to make him get there as fast as possible. So I figured if I could make a right triangle from the following points (Player pos, Target Pos, and the X,Y intercept see my image bellow).
My code:
Adj = (int) (TargetX-x); // Get The size of the Adjacent leg.
Opp = (int) (TargetY-y); // Get the size of the Opposite leg.
OppAdj = Opp/Adj; //Inverse tan is Opposite/Adjacent
TargetAngle = Math.abs(Math.atan(Opp/Adj)*100); //Keep the angle positive, and use inverse tan to get the missing angle.
Now what I thought this would do, is solve for the missing angle so I could rotate the player by that amount so the player can move in a straight line and hit the objective.
What this ends up doing though is is giving me a target angle of 73 degrees and a the Variable OppAdj ends up being 1.0.
What is wrong with my code?
Any help is appreciated!
Thanks,
Kyle
OppAdj = Opp/Adj;
That is the problem. You should do this:
OppAdj = (double)(Opp)/Adj;
That way it will give you a double for accuracy. By the way:
TargetAngle = Math.abs(Math.atan(Opp/Adj)*100);
Should be:
TargetAngle = Math.abs(Math.atan(OppAdj)*100);

Categories

Resources