Reputation: 615
I am having trouble calculating the movements I want using JOGL. The examples in the book inexplicably use the sin()
and cos()
of System.timeInMillis()
.
stack.translate(Math.sin(amt)*4.0f, Math.sin(amt) * 1.0f, Math.cos(amt)*2.0f);
this all works very fine the problem is I want to know where they get these numbers from and how to calculate, among other things, a smoothly falling cube.
int pos = 1;
stack.translate(0.0, --pos, 0.0);
does the trick but it's limited. Are there any resources that instruct on how to use these mysterious sin()
and cos()
functions to control movement in a very specific way?
I'm looking into the related questions now. All I need is a reference of some kind, I really don't even know what to call this kind of thing to do research on; Fluid dynamics? 3D trigonometric graphic motion? 3D Newtonian physics?
Upvotes: 2
Views: 1283
Reputation: 51923
the equation is not mysterious ... it is just an parametric equation of ellipse (distorted circle)... let me explain by its derivation:
2D xy plane circle:
x=x0+r*cos(t)
y=y0+r*sin(t)
where (x0,y0)
is circle center, r
is circle radius and t=<0.0,2.0*Pi>
is angle parameter determining which point of the circle you want.
2D xy plane axis aligned ellipse
x=x0+rx*cos(t)
y=y0+ry*sin(t)
we just use different radius per axis. So rx,ry
are also the semi-axises .
3D ellipse
if we rotate our #2 ellipse into 3D we could get any ellipse. To make this easy we can just rotate along one axis which will divide one ellipse coordinate into two ... so if I rewrite to your equation:
x=sin(t)*4
y=sin(t)
z=cos(t)*2
means that z
is start axis of ellipse (angle 0
former axis x
) and x,y
axises are rotated parts of former y
axis. The ellipse is centered around (0,0,0)
and has semi axises 2.0
and sqrt(1^2+4^2)
.
Now if we change t
with system time scaled to some speed then
t = amt = 2.0*Pi*system_time/T
Where T
is your movement period.
Now when you use absolute translate then you move your object into position along the ellipse. If you use relative translation then the speed is driven by this ellipse resulting in more complex trajectory. This is just fake motion simulation if you want real physics use Newton D'Alembert physics and drive your object by changing acceleration.
If you want to make human driven objects take a look at last links here:
For planetary motion see:
So to answer your second question use Newton D'Alembert and vector math. I assuming 3D. So let your cube has position speed and acceleration.
// init do this just once
pos=(0,0,0); // [m] start position
vel=(0,0,0); // [m/s] start velocity
acc=(0,-9.81,0); // [m/s^2] acceleration on object (gravity in -y direction)
// on some timer or before render ...
vel+=acc*dt;
pos+=vel*dt;
cube.translate(pos); // absolute translation of your cube
where dt [s]
is time elapsed from last computation so in case of timer it is its interval in seconds. You can use any units but all units must be compatible between pos,vel,acc
.
You can add frictions like:
acc+=k*vel*|vel|; // acc += k*vel^2
where k
is the friction coefficient in air (in liquid it would k*vel^3
) much less than 1.0
.
To drive your object you can use driving forces ...
acc += F/m;
where F
is sum of driving forces and m
is mass of your object.
All of this can be done also for angle (orientation) as it has similarities
alpha -> pos
omega -> vel
epsilon -> acc
and use absolute rotation of your object by alpha
.
Upvotes: 2