zoukankan      html  css  js  c++  java
  • box2d 计算下一帧的位置/角度

    var dt:Number=1/30;
    var y0:Number=_body.GetPosition().y;
    var y:Number=y0+_body.GetLinearVelocity().y*dt+_world.GetGravity().y*dt*dt;
    trace(y0, "y:"+y);

     b2Island中的实现:

    var l_gravity:b2Vec2=b.m_customGravity?b.m_customGravity:gravity;
    //==============================added================================
    // Integrate velocities.
    //b.m_linearVelocity += step.dt * (gravity + b.m_invMass * b.m_force);
    b.m_linearVelocity.x += step.dt * (l_gravity.x + b.m_invMass * b.m_force.x);
    b.m_linearVelocity.y += step.dt * (l_gravity.y + b.m_invMass * b.m_force.y);
    b.m_angularVelocity += step.dt * b.m_invI * b.m_torque;
    
    // Apply damping.
    // ODE: dv/dt + c * v = 0
    // Solution: v(t) = v0 * exp(-c * t)
    // Time step: v(t + dt) = v0 * exp(-c * (t + dt)) = v0 * exp(-c * t) * exp(-c * dt) = v * exp(-c * dt)
    // v2 = exp(-c * dt) * v1
    // Taylor expansion:
    // v2 = (1.0f - c * dt) * v1
    b.m_linearVelocity.Multiply( b2Math.Clamp(1.0 - step.dt * b.m_linearDamping, 0.0, 1.0) );
    b.m_angularVelocity *= b2Math.Clamp(1.0 - step.dt * b.m_angularDamping, 0.0, 1.0);

     角度:

    var dt:Number=1/30;
    var ang:Number=_body.GetAngle();
    
    var angV:Number=_body.GetAngularVelocity();
    angV += dt * _body.m_invI * _body.m_torque;
    angV*=dt
    angV *= b2Math.Clamp(1.0 - dt * _body.m_angularDamping, 0.0, 1.0);
    
    var nextAng:Number=ang+angV;
    /*trace("ang:",ang);
    trace("angV:",angV);
    trace("nextAng:",nextAng+"
    ");*/

    LinearVelocity与像素的关系:

    不考虑摩擦,刚体质量为1,无重力的情况下
    linearVelocity*dt=distancePixel(像素距离)/ptm_ratio(像素到b2World单位)
  • 相关阅读:
    细说mysql索引
    SQL常见优化Sql查询性能的方法有哪些?
    JDK提供的几种线程池比较
    JVM 内部运行线程介绍
    线上服务CPU100%问题快速定位实战
    浅谈Java中的hashcode方法
    Spring 核心组件工作原理简析
    SpringMVC工作原理
    open MMT.distributions = null on transaction type: WIP Lot Split
    OSFM Tables
  • 原文地址:https://www.cnblogs.com/kingBook/p/6149903.html
Copyright © 2011-2022 走看看