Free-flight camera with vectors - how to rotate properly? - java

I have this camera that is set up with vecmath.lookatMatrix(eye, center, up).
The movement works fine, forwards, backwards, right, left, these work fine.
What does not seem to work fine is the rotation.
I am not really good at math, so I assume I may be missing some logic here, but I thought the rotation would work like this:
On rotation around the Y-axis I add/sub a value to the X value of the center vector.
On rotation around the X-axis I add/sub a value to the Y value of the center vector.
For example here is rotation to the right: center = center.add(vecmath.vector(turnSpeed, 0, 0))
This actually works, but with some strange behaviour. It looks like the higher the x/y of the center vector value gets, the slower the rotation. I guess it's because through the addition/substraction to the center vector it moves too far away or something similar, I would really like to know what is actually happening.
Actually while writing this, I just realized this can't work like this, because once I have moved around and rotated a bit, and for example I'm in "mid air", the rotation would be wrong....
I really hope someone can help me here.

Rotating a vector for OpenGL should be done using matrices. Linear movement can be executed by simply adding vectors together, but for rotation it is not enough just to change one of the coordinates... if that was the case, how'd you get from (X,0,0) direction to (0,X,0)?
Here is another tutorial, which is C++, but there are Java samples too.
There is a bit of math behind all this - you seem to be familiar with vectors, and probably have a 'feel' of them, which helps.
EDIT - if you are to use matrices in OpenGL properly, you'll need to familiarize yourself with the MVP concepts. You have something to display (the model) which is placed somewhere in your world (view) at which you are looking through a camera (projection).

A working solution to working with a free-flight camera with eye, center, up vectors was posted here:
Free Flight Camera - strange rotation around X-axis

Related

Android, Java: Stretch Bitmap or Path to arbitrary polygon

As the title implies I need an algorithm, code or a library that would help me to stretch a Bitmap (or a Path in Android) to an arbitrary polygon. Polygon is given with a list of x, y coordinates. Actually I need to transform/stretch a Path object in Android which is also given by x, y coordinates. I mentioned Bitmap because it is more likely that someone had similar problem and I assume that both will be transformed my a Matrix
I tried to use Matrix.setPolyToPoly(...) but it doesn't seem to help since it is transforming to square like area (only 4 points) not to an arbitrary polygon.
For better illustration what I need please check out image bellow. It is not exact transformation but something close. Note that whole image is stretched to star shaped polygon, it is not a mask and not a trim, just pixel transition.
I saw your question a few days ago, then yesterday I ran across this:
Canvas#drawBitmapMesh | Android Developers
It's kind of hard to grasp, but the way I understand it you start with an imaginary elastic grid over your bitmap. The way you want to warp the bitmap can be expressed by moving the x,y points of the grid to alternate locations.
Here's an article with a diagram and here's an article with some sample code.
Obviously, the hard part now is to take your frame polygon and use it to generate the warped vertices in the mesh. That may take some fancy mathematics. But I thought this would be a step in the right direction.
This is what I was envisioning: I'm looking at the star polygon and I'm picturing a circle as the starting point (not the square). The star could be seen as taking the circle and stretching points on it toward and away from the center. Whichever way it was stretched would create some vectors, from zero at the center to strongest at the stretch point.
For a Path, you could then just apply the vectors to the points in the path, but the lines would also need to be bent so this would be some pretty convoluted math with Bezier curves (convoluted at least for me, I'm not any sort of mathematician).
But if you drew the Path onto a Bitmap you might be in a better position. You could just alter the mesh vertices using the different vectors then use Canvas.drawBitmapMesh() to render the final result.

Libgdx rotate ModelInstance

I am developing my first libgdx 3D game. Until now i can move arround in a maze-like (hardcoded) world, collision detection works. Also i have some enemies with working A* Pathfinding.
I also loded my first (pretty ugly) Blender model, using FBX-Conv to get a .g3db file. For some reason the model lise on the floor instead of standing. Maybe i had some wrong settings when i exported it as .fbx.
For that i tryed to rotate() him arround the z-Axis by 90 degrees by calling:
modelInstance.transform.rotate(Vector3.Z, 90) in the show() method of my Screen, after loading the Model and instantiating my ModelInstance (at a given position). For some reason it did not rotate. Then i put the rotate method in the render(delta), thinking, that it would now rotate 90 degrees every render loop. But instead it was standing still, like it should.
Okay, but now i want the modelInstance to rotate to where it actually looks, meaning it should rotate, depending on my enemies Vector3 direction.
I am allready setting his position with modelInstance.transform.setTotranslation(enemie.getPosition()) which works perfect. So i thought i can also use modelInstance.transform.setToRotation(Vector3 v1, Vector3 vs), with v1 = enemie.getPosition() and v2 = enemie.getPosition().add(enemie.getDirection). Note, that the position Vector is not used directly, as it would change its values inside the add() method.
Doing this, i don't see the object anymore, meaning also its position is wrong.
Why is this happening?
And how can i rotate my modelInstance by using the direction vector?
Thanks a lot.
I solved this with #Xoppas help. The problem was:
i used setToTranslation to move my Model to a given position, but this als resets the rotation
I missunderstood the setToRotation(Vector3, Vector3) method.
So the solution was to to the setToTranslation first, and then use setToRotation(Vector3 direction, Vector3 face), where direction is the direction, in which my Model is looking and face is the face, which should look in this direction, in my case the Vector3.X.
Hope it helps someone else.
Worse case scenario, you could modify the transformation matrix using:
ModelInstance.transform.rotate()

min3D framework. Object rotation & movement

I am trying to code a cessna flying around the world using the accelerometer with the min3D framework for android but the rotation is a bit weird.
I'm using this to apply the accelerometer rotation to the object:
cessna.rotation().x = rotX;
cessna.rotation().z = rotZ;
This works fine. I haven't figured out yet how to move in the direction of rotation (I think I have to use trigonometry).
I rotated the object with
cessna.rotation().y++;
just to test what will happen. At 180° the rotation around the x axis is mirrored. So the nose of the plane turns down instead of up.
I think I rotate the Objects around the world axis and not around the local axis from the object. How can I do this? I didn't find any documentation about the min3D framework in the internet :/ .
Thank you if you can help me.
(sorry for the bad English)
If you want to rotate around object local axis. Do this
(in pseudo code - you'll need to find similar functions in min3d)
Translate(object.pos.x,object.pos.y,object.pos.z);
object.rotation().x+=radians(45);// or whatever
if that doesn't work, try wrapping the above two lines in
pushMatrix()
...
popMatrix()
Or similar functions in min 3d to save and then restore the current camera rotation translation matrix.
Have you considered, Processing, which also comes with an Android 'output'?

What is the best way to rotate cube in Opengl

I have a Rubik's cube displayed using OpenGl in a Java Eclipse application and I want to "rotate" this cube in response to mouse events.
I started with a "naive" (isn't it ?) solution as described here: OpenGL - moving camera with mouse. With such solution in addition to the problem described (my problem is maybe the same ???) when I rotate 90 degrees according X-Axis to get 'upside front' i am not to rotate anymore according initial Y-Axis to get the new front on the right. Because of the first 90 degrees rotaion I will have now to rotate according to Z to get expected behavior.
May be using gluLookAt utility method is easiest than using modeling transformation in this case ?
Would arcball make you happy ? (it should)
(I don't usually link to NeHe, but this one is independent from openGL)

how can i do to a 2D image and spin it around it's X, Y, or Z axis as if it were a 3D image.in java?

I want to achieve the effect of a 2D image I have but a little inclined, for example a plane, I want the image can be rotated about its axis Y. .. anyone can help me with some idea of how to do ..**
Basically you need a little linear geometry/algebra, and/or a package to do them for you.
From the geometry point of view, you think of the image as if it's on a plane in space; you're looking at it as if it were back-projected on your monitor. If the picture is exactly parallel to that screen, and the same size, each point is mapped to a pixel on the screen. Otherwise you have to go through a computation that makes that mapping, which involves a trig function for the angles in the x,y,z directions between that plane and the plane of the screen. The linear algebra comes in because the easy way to handle this computation is as a series of multiplications of 4×4 matrices.
Now, you could program all that yourself, and for what you're thinking of it wouldn't be all that difficult. See any good computer graphics text, like Shirley, or Foley and van Damm.
As far as a package, there's good 3D graphics in Java. Even better, there are good tutorials:
The Java3D tutorial at Sun.
the stuff at j3d.org
a whole list of them at java3d.org
In what context ? Using a 3D API like OpenGL trough JOGL seems to me like the simplest way to achieve this. Otherwise, if the angle is variable, you'll need some form of software renderer.

Categories