javascript - box2dweb 光线转换

标签 javascript box2d

我正在尝试获取我的 Angular 色和地面之间的距离,我发现了一些看起来应该做我想要的事情,但它是为另一个版本的 box2d 编写的。

原文:

float targetHeight = 3;
float springConstant = 100;

//make the ray at least as long as the target distance
b2Vec2 startOfRay = m_hovercarBody->GetPosition();
b2Vec2 endOfRay = m_hovercarBody->GetWorldPoint( b2Vec2(0,-5) );

overcarRayCastClosestCallback callback;
m_world->RayCast(&callback, startOfRay, endOfRay);

if ( callback.m_hit ) {
    float distanceAboveGround = (startOfRay - callback.m_point).Length();

    //dont do anything if too far above ground
    if ( distanceAboveGround < targetHeight ) {
        float distanceAwayFromTargetHeight = targetHeight - distanceAboveGround;
        m_hovercarBody->ApplyForce( b2Vec2(0,springConstant*distanceAwayFromTargetHeight),
        m_hovercarBody->GetWorldCenter() );
    }
}

我尝试将其更改为我认为应该的样子,但它甚至没有调用回调。

var targetHeight = 3;
var springConstant = 100;

//make the ray at least as long as the target distance
startOfRay = new b2Vec2(m_hovercarBody.GetPosition());
endOfRay = new b2Vec(m_hovercarBody.GetWorldPoint( b2Vec2(0,-5)));

function callback(raycast){
    if ( raycast.m_hit ) {
        var distanceAboveGround = (startOfRay - raycast.m_point).Length();

        //dont do anything if too far above ground
        if ( distanceAboveGround < targetHeight ) {
            var distanceAwayFromTargetHeight = targetHeight - distanceAboveGround;
            m_hovercarBody.ApplyForce( b2Vec2(0,springConstant*distanceAwayFromTargetHeight),
        m_hovercarBody.GetWorldCenter() );
        }
    }
}
m_world.RayCast(callback, startOfRay, endOfRay);

知道如何将其转换为与 box2dweb 一起使用吗?

谢谢

最佳答案

最初的代码可能是为坐标系工作方式不同的平台编写的。

在 Canvas 元素中,坐标系从左上角开始,这意味着 m_hovercarBody.GetWorldPoint( b2Vec2(0,-5)) 正在检查 Angular 色上方的点,而不是下面。

我不确定代码的其余部分,但尝试将其更改为 m_hovercarBody.GetWorldPoint( b2Vec2(0,5)) 并看看会发生什么。

编辑:

我认为实际上问题在于您构建回调的方式。查找 Raycast 函数的引用将 reveal more 。 (您使用的 Box2D 的 Javascript 版本是 Actionscript 版本的自动移植版本。鉴于两者具有相当相似的语法,您可以使用 reference for Flash 。)

你发布的原始代码似乎是C++,但我对其语法不太了解。似乎有某种类可以进行光线转换(overcarRayCastClosestCallback)。您可以查找该函数,或者尝试根据我发布的第一个链接构建您自己的回调函数。这将类似于:

function customRaycastCallback(fixture, normal, fraction) {
// you can, for instance, check if fixture belongs to the ground
// or something else, then handle things accordingly

    if( /* fixture belongs to ground */ ) {
        // you've got the fraction of the original length of the raycast!
        // you can use this to determine the distance
        // between the character and the ground
        return fraction;
    }
    else {
        // continue looking
        return 1;
    }
}

关于javascript - box2dweb 光线转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9251828/

相关文章:

javascript - 如何重建Math.random()的种子?

Javascript 滚动视差定位

javascript - 输入按钮 - Onclick 换色器

javascript - 显示 jquery 模态叠加视频

java - 创建 Box2d LibGDX 后我的 body 有点下降

java - 在 Libgdx 中设置相机旋转

javascript - jquery.js?v=1.9.1 :4421 Uncaught Error: Syntax error, 无法识别的表达式:[对象对象]

xna - Farseer 还是 Box2D?自上而下的射击物理实现

android - 关节体不跟随真实的 body

c++ - b2 Shape 等于 0xCDCDCDCD,在 Create Fixture 时抛出异常