Getting the velocity vector from position vectors

后端 未结 3 1504
日久生厌
日久生厌 2021-01-29 01:45

I looked at a bunch of similar questions, and I cannot seem to find one that particularly answers my question. I am coding a simple 3d game, and I am trying to allow the player

3条回答
  •  情深已故
    2021-01-29 02:20

    basic Newtonian/D'Alembert physics dictate:

    derivate(position)=velocity
    derivate(velocity)=acceleration
    

    and also backwards:

    integrate(acceleration)=velocity
    integrate(velocity)=position
    

    so for your engine you can use:

    rectangle summation instead of integration (numerical solution of integral). Define time constant dt [seconds] which is the interval between updates (timer or 1/fps). So the update code (must be periodically called every dt:

    vx+=ax*dt;
    vy+=ay*dt;
    vz+=az*dt;
     x+=vx*dt;
     y+=vy*dt;
     z+=vz*dt;
    

    where:

    • a{x,y,z} [m/s^2] is actual acceleration (in your case direction vector scaled to a=Force/mass)
    • v{x,y,z} [m/s] is actual velocity
    • x,y,z [m] is actual position

      1. These values have to be initialized a,v to zero and x,y,z to init position
      2. all objects/players... have their own variables
      3. full stop is done by v=0; a=0;
      4. driving of objects is done only by change of a
      5. in case of collision mirror v vector by collision normal

      and maybe multiply by some k<1.0 (0.95 for example) to account energy loss on impact

    You can add gravity or any other force field by adding g vector:

    vx+=ax*dt+gx*dt;
    vy+=ay*dt+gy*dt;
    vz+=az*dt+gz*dt;
    

    also You can add friction and anything else you need

    PS. the same goes for angles just use angle/omega/epsilon/I instead of x/a/v/m

    to be clear by angles I mean rotation (pitch,yaw,roll) around mass center

提交回复
热议问题