In my libGDX project I want to move a player around the screen with a touchpad. That already works.
Now I want to rotate the "face" of the player in the direction he is moving.
I have tried it with this little piece of code, but I doesn´t really work
double degree;
degree = Math.tan(touchpad.getKnobPercentX()/touchpad.getKnobPercentY());
playerSprite.setRotation((float) degree);
I hope u can help me (I have bee searching on Google for about 1 hour before I asked this question, so don't tell me to Google it;))
You might want to use the atan(y/x) function or even better the atan2(y,x) function to get the angle. Check the documentation where you get results in radians and expect inputs in degrees.
tan transforms an angle (in radians) to the tangent value, the slope of the angle.
phi=atan(y/x) is the arc tan, centuries ago written as arg(y/x=tan(phi)). It gives the same result for opposite angles, so you would have to correct for the right quadrant.
phi=atan2(y,x) already performs that correction.
Related
So I'm making a TD game where I can place a gatling gun, and depending on which sector of a surrounding circle the mouse is in, the sprite and bullet path will change.
My difficulty is with creating an algorithm which will tell me which sector my mouse is in.
My circle has 16 sectors, and a radius of 300. Each arc has a length of 117.81.
Extending from (300,300), I have an exact list of all the coordinates of the lines, so I am able to currently draw the sector like this:
Circle
I'm using a mouse listener to detect the coordinates of my mouse whenever my mouse moves, so I have a "currentPoint" to check within which sector it's in.
Based on this information, can anyone think of an easy way to simply return an integer of which sector the mouse is currently inside? Preferably somewhat efficiently.
These are the two ways I'm thinking about how it would look:
Two_Ideas
And I did look at this StackOverflow which seemed like a similar problem: Efficiently find points inside a circle sector
And I implemented it with Java, but it doesn't seem to translate without having Vectors and I'm a bit too confused about the math to make it work.
Been trying to figure this out for a while, I would love any help with an implementation of any kind, (don't mind adding Trig calculations), along with any help understanding the problem.
Thank you!!
To get sector, you need to get angle relative to point center.
Pseudocode (I am not sure how math functions and rounding look in Java):
double angle = math.atan2(mouse.y-center.y, mouse.x-center.x);
angle = angle - math.pi / 16.0;
while (angle < 0) {
angle = angle + 2*math.pi;
}
sector = math.floor(angle * 8.0 / math.pi);
I made correction by half-sector becouse your first sector is centered around OX axis.
I am trying to bounce a projectile off of a wall in Slick. When the wall is vertical or horizontal, I can just invert the x or y velocity of the projectile, but when this is not the case I am struggling. I found another answer to a similar question in C# and have tried to implement it, but the result is just that the ball goes off at 180 degrees every time. What I have is:
Vector2f norm = wall.getNormal();
float comp = (vect.dot(norm)/norm.dot(norm));
Vector2f u = new Vector2f(norm.getX()*comp,norm.getY()*comp);
Vector2f w = new Vector2f(vect.getX()-u.getX(),u.getX());
vect.set(w.getX()-u.getX(),w.getY()-u.getY());
Where wall is a Vector2f that I am trying to bounce off of (converted from a line to a vector, but not sure if this was necessary) and vect is the trajectory of my projectile.
Any advice would be really helpful. I would love if someone could explain an answer to me rather than just giving me the code, because I think I may need to implement something slightly different later.
Thanks.
The answer here is going to wind up being mostly a question of math. I briefly go over the math/physics involved and trust that you should be able to implement the math/physics in code yourself.
How projectiles are supposed to bounce off of walls
In classical physics, or at least in a reasonably representative model of classical physics, when a projectile collides with a wall, the magnitude of the angle at which it leaves the wall relative to the 'normal' vector of the wall is equal to the magnitude at which it approaches the wall relative to the 'normal' vector. In other words, draw a line perpendicular to the wall the object is colliding with. The angle between this line and the vector of your projectile moving towards the wall will be the same as the angle of the vector of your projectile moving away from the wall.
This is most commonly discussed for light rays, but it works the same way for simple projectiles.
http://hyperphysics.phy-astr.gsu.edu/hbase/phyopt/fermat.html
How to calculate these angles
So let's say you have a vector for the normal of the wall, and a vector for the projectile. For 2-dimensional vectors, this is actually quite easy. You should have a couple of decent options here. One way might be to get the angles of the two vectors and subtract them to find the difference.
Now we know the magnitude of the angle, but we now need to create a vector with the same angle but on the other side of the normal vector. Your vector library conveniently allows you to adjust your vector by a given angle. You should be able to take the projectile's current velocity vector and adjust in the correct direction it by twice the angle between it and the normal-to-wall vector and get the desired velocity vector post-reflection. The trick I will leave you to figure out is a good way to determine if you need to adjust your vector in the clockwise or counter-clockwise direction.
Some things to keep in mind about Vector2f
The getNormal() method returns a unit vector in the same direction as the vector
If you want a vector normal to a certain vector, you must use the getPerpendicular() method
A vector may be rotated/adjusted by a certain angle using the sub(double theta) method
If you would like to retrieve the angle of a vector, you may do so using the getTheta() method
I have been trying for quite some time now and I cant seem to find anything about 3rd person cameras. I just want to make a simple 3rd person camera but it gets difficult because I'm in 3d. So far I can do the camera on 1 plane but after that its messed up. I'm using libgdx with ModelInstances if anyone knows libgdx.
I don't need you to hand me the code for it, I just need help on how to write this out with the information I have. I know the velocity in x y and z, the rotation in x y z of the object, and its position in x y z. I can make the camera look at the object so all I have to do is position it behind and above it no matter what the orientation is for the object. Any links or explanations are greatly appreciated :)
Solution:
Xoppa posted in the comments a link to his chaseCam that extends perspective cam and It is amazing. Since it wasn't an answer I could mark it as accepted but it did what I needed. Heres the link chaseCamera.java All credit to Xoppa for the answer, just making it easier to find.
I would do on each frame after updating the player's position:
camera.up.set(0,1,0); //Not sure if this is necessary.
//Making sure up is always up after
//last frame's lookAt() call.
camera.postion.set(player.position)
.add(-UNITS_BACK, UNITS_UP, 0)
.rotate(Vector3.UP, player.angle);
camera.lookAt(player.position);
camera.update();
where player.angle is the number of degrees counter-clockwise the player is facing from the X-axis.
That's a simple starting point. You'll probably want to smooth out the movement by limiting the speed at which the camera can move or giving it a second order interpolated movement to this target position and direction.
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.
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);