Getting the velocity vector from position vectors

后端 未结 3 1404
日久生厌
日久生厌 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:18

    A very simple implementation could be like this:

    velocity(t+delta) = velocity(t) + delta * acceleration(t)
    acceleration(t) = force(t) / mass of the object
    

    velocity, acceleration and force are vectors. t, delta and mass scalars.

    This only works reasonably well for small and equally spaced deltas. What you are essentially trying to achieve with this is a simulation of bodies using classical mechanics.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-29 02:24

    An Impulse is technically F∆t for a constant F. Here we might want to assume a∆t instead because mass is irrelevant. If you want to animate an impulse you have to decide what the change in velocity should be and how long it needs to take. It gets complicated real fast.

    To be honest an impulse isn't the correct thing to do. Instead it would be preferable to set a constant pick_up_velocity (people don't tend to pick things up using an impulse), and refresh the position each time the object rises up velocity.y, until it reaches the correct level:

    while(entPos.y < holdingEntPos.y)
    {
        entPos.y += pickupVel.y;
       //some sort of short delay
    }
    

    And as for floating in front of the player's eyes, set an EyeMovementEvent of some sort that also sends the correct change in position to any entity the player is holding.

    And if I missed something and that's what you are already doing, remember that when humans apply an impulse, it is generally really high acceleration for a really short time, much less than a frame. You wouldn't see it in-game anyways.

    0 讨论(0)
提交回复
热议问题