2

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?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
MadMax
  • 605
  • 1
  • 6
  • 19
  • 1
    They're used for rotations. Google "transform+computer graphics". – Robinson Sep 08 '17 at 02:09
  • 2
    How you calculate movement depends on what kind of movement you want to simulate/achieve. And if it should be in realtime or as accurate as possible. Should the moving object interact with other object, should it be influenced by air, ... . Your question to vague to be answered. SO is a place to ask question about specific problems you have with your code. – t.niese Sep 08 '17 at 04:43

1 Answers1

2

the equation is not mysterious ... it is just an parametric equation of ellipse (distorted circle)... let me explain by its derivation:

  1. 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.

  2. 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 .

  3. 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.

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • bingo thanks spektre. I implemented your second method: – MadMax Sep 08 '17 at 18:07
  • vel+=acc*dt; pos+=vel*dt; cube.translate(pos) and it worked great my pyramid is revolving just like you would expect. How would I calculate a falling object ? Or is that what this one was supposed to do and I did something wrong. – MadMax Sep 08 '17 at 18:08
  • @madmax the code is already for falling in `y-` direction if your view is turned and without perspective then maybe it is falling in view direction so you do not see it ... in such case move the `-9.81` in `acc` to different axis. Take a look at this [How to implement a constraint solver for 2-D geometry?](https://stackoverflow.com/a/40827518/2521214) it is a bit more complicated working example of such physics using fields to compute geometry problem ... Also check your units for all the variables they should be in the same system like SI ... or scaled properly – Spektre Sep 08 '17 at 18:26