如何在openGL中计算运动

编程入门 行业动态 更新时间:2024-10-09 11:16:25
本文介绍了如何在openGL中计算运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用JOGL计算我想要的运动时遇到了麻烦.书中的示例莫名其妙地使用了System.timeInMillis()的sin()和cos().

stack.translate(Math.sin(amt)*4.0f, Math.sin(amt) * 1.0f, Math.cos(amt)*2.0f);

这一切都很好,问题是我想知道他们从哪里得到这些数字,以及如何计算(其中包括)一个平稳下降的立方体.

int pos = 1; stack.translate(0.0, --pos, 0.0);

可以解决问题,但这是有限的.是否有任何资源说明如何使用这些神秘的sin()和cos()函数以非常特定的方式控制运动?

我正在调查相关问题.我所需要的只是某种参考,我什至不知道该怎么称呼进行这种研究.流体动力学? 3D三角图形运动? 3D牛顿物理学?

解决方案

方程不是神秘的……它只是椭圆的参数方程(扭曲的圆)……让我用它的推导来解释:

  • 2D xy平面圆:

    x=x0+r*cos(t) y=y0+r*sin(t)

    其中(x0,y0)是圆的中心,r是圆的半径,t=<0.0,2.0*Pi>是确定所需圆的点的角度参数.

  • 二维xy平面轴对齐的椭圆形

    x=x0+rx*cos(t) y=y0+ry*sin(t)

    我们仅在每个轴上使用不同的半径.所以rx,ry也是半轴.

  • 3D椭圆形

    如果将#2 椭圆旋转为 3D ,我们可以获得任何椭圆.为了简化此操作,我们可以沿一个轴旋转,将一个椭圆坐标分成两个...所以如果我重写您的方程式:

    x=sin(t)*4 y=sin(t) z=cos(t)*2

    表示z是椭圆的起始轴(角度0前一个轴x),而x,y轴是前一个y轴的旋转部分.椭圆以(0,0,0)为中心,并具有半轴2.0和sqrt(1^2+4^2).

  • 现在,如果我们将t的系统时间缩放为一定速度,则

    t = amt = 2.0*Pi*system_time/T

    T是您的运动周期.

    现在,当您使用绝对平移时,可以将对象沿椭圆移动到适当的位置.如果使用相对平移,则速度由该椭圆驱动,从而导致更复杂的轨迹.如果您想让真实的物理使用 Newton D'Alembert物理并通过更改加速度来驱动对象,那么这只是虚假的运动模拟. /p>

    如果您想使人为驱动对象,请在此处查看最后的链接:

    • 了解4x4均匀变换矩阵

    有关行星运动,请参见:

    • 是否可以在大小和质量方面进行逼真的n体太阳系模拟?

    因此要回答第二个问题,请使用 Newton D'Alembert 和矢量数学.我假设使用 3D .因此,让您的多维数据集具有位置速度和加速度.

    // 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

    其中,dt [s]是从上一次计算开始经过的时间,因此对于计时器而言,它是其间隔(以秒为单位).您可以使用任何单位,但是pos,vel,acc之间的所有单位都必须兼容.

    您可以添加摩擦,例如:

    acc+=k*vel*|vel|; // acc += k*vel^2

    其中,k是空气中的摩擦系数(在液体中为k*vel^3),远小于1.0.

    要驱动物体,可以使用驱动力...

    acc += F/m;

    其中F是驱动力的总和,而m是物体的质量.

    所有这些都可以针对角度(方向)完成,因为它具有相似性

    alpha -> pos omega -> vel epsilon -> acc

    并按alpha使用对象的绝对旋转.

    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?

    解决方案

    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:

    • Understanding 4x4 homogenous transform matrices

    For planetary motion see:

    • Is it possible to make realistic n-body solar system simulation in matter of size and mass?

    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.

    更多推荐

    如何在openGL中计算运动

    本文发布于:2023-11-28 08:01:34,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1641580.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何在   openGL

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!