Hello, im having another problem, with math this time.
Im having the following problem:
I want to rotate certain parts of an model at creation to give an random look.
However this involves the rotation of 3d points, and im not really sure how to handle this.
Rotation in one way was easy, but 3D seems very complicated (i dont really get the explanations on the internet).
Basicly im looking for an code representation of gl.glRotatef(rotate, 0f, 0f, 1f), so i can calculate these points at creation.
Does anyone have some retard proof explanation / sample or simply some resource to get an implementation from?
Im trying to solve most math problems myself, but im just clueless about 3d rotations.
Yeha, but how does this multiplication work?
I see 9 variables within the blocks, and im not really sure how i should use them in combination with eachother.
How it works is that you take all the rotation matrices and and put the angle you want to rotate. this will result in a final rotation matrix, then multiply that matrix with your 3D point matrix(vector). Make sure the order is correct.
A better description of your problem would be handy. Using matrices when you don’t know them might be like attempting to use a pile-driver to drive a nail. Maybe you just need a hammer.
Im drawing some terrain with self drawn trees (dont laugh):
I already added randomness with color, size and texture coords, but i also want to add some rotation randomness.
Im building the tree with some segments, wich i would like to be able to rotate in 3 axis.
Exterragated effect:
I could just call glRotatef(rotate, 0f, 0f, 1f) before every segment, but that would not be efficient, and then i wont be able to keep groups of trees in a single vbo, since then i would need to render every segment alone.
OK. So basically you want an animation hierarchy, which in this special case you want to add some procedurally generated random rotations at each joint to make identical models look slightly different. In addition to rotations scaling effects (among others) would be handy. Building or finding a library for animation hierarchies is the way I would go. Additionally with this is place you could augment your system to support things like wind based effects. Ultimately quaternions fit the problem much better than matrices.
Exactly, so my problem is now building this library wih rotation / scaling.
I was also thinking about wind effects and maybe falling trees, so were on one line
Scaling is really easy, but im having no clue about rotations.
1 dimensional rotation is simple:
//Calculates 4 points of rectangle
public static Vector2f[] rotate(Vector4f src, float rotation){
Vector2f[] dst = new Vector2f[4];
float sin = sinf(rotation);
float cos = cosf(rotation);
float wsin = sin * (src.z / 2);
float wcos = cos * (src.z / 2);
float hsin = sin * (src.w / 2);
float hcos = cos * (src.w / 2);
//x' = x*cos(t) - y*sin(t)
//y' = x*sin(t) + y*cos(t)
dst[0] = new Vector2f(src.x + wcos - hsin, src.y + hcos + wsin);
dst[1] = new Vector2f(src.x - wcos - hsin, src.y + hcos - wsin);
dst[2] = new Vector2f(src.x + wcos + hsin, src.y - hcos + wsin);
dst[3] = new Vector2f(src.x - wcos + hsin, src.y - hcos - wsin);
return dst;
}
but now the problem is creating an functon like this:
public static Vector3f rotate(Vector3f src, Vector3f point, Vector3f rotation){
//Rotate point around src with some math magic
}
quaternions and matrices sounds good, and looks like it is an solution, bu i have no clue about how to use them.
I have tryd to understand them on wikipedia or math tutorials, but im really clueless how to use them inside an function.
Basicly im just trying to find out how create this function (in java and / or glsl(wind effect) ).
If there is some precreated code somewhere, its fine to, reverse enginering the solution would still give me the information i need.
Apparently I can’t even explain complex numbers in a manner that’s understandable, but I’ll start a wiki page for quaternions…I guess I’m a glutton for wasting time.
As he was originally* looking for axis-aligned rotations around the origin… I think this (untested) code solves his problem quite well. (* before feature creep started)
public class Rotation {
private float cos, sin; // this is secretly a 'complex number', woohoo!
public Rotation() {
cos = 1.0f;
sin = 0.0f;
}
public void setAngle(double radians) {
cos = (float) Math.cos(radians);
sin = (float) Math.sin(radians);
}
public void rotateAroundX(Vec3 src, Vec3 dst) {
float y = src.y, z = src.z;
dst.x = src.x;
dst.y = cos * y - sin * z;
dst.z = sin * y + cos * z;
}
public void rotateAroundY(Vec3 src, Vec3 dst) {
float x = src.x, z = src.z;
dst.x = cos * x - sin * z;
dst.y = src.y;
dst.z = sin * x + cos * z;
}
public void rotateAroundZ(Vec3 src, Vec3 dst) {
float x = src.x, y = src.y;
dst.x = cos * x - sin * y;
dst.y = sin * x + cos * y;
dst.z = src.z;
}
}
Awsome!
I did not know it would be this simple (using 1d rotation on 3 axis).
Perfect, this saves me a lot of headache with i would have with those matrix calculations.
Now i can continue my tree coding
Also many thanks to you Roquen an Agro to help with my noobish problem.