box2dweb 光线投射
我正在尝试获取我的角色和地面之间的距离,我发现了一些看起来应该做我想要的事情,但它是为 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 一起使用吗?
谢谢
I am trying to get the distance between my character and the ground, I have found something that looks like it should do what I want but it has been written for another version of box2d.
Original:
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() );
}
}
I have tried to change it to what I think it should be but it doesn't even call the callback.
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);
Any idea how to convert it to work with box2dweb?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最初的代码可能是为坐标系工作方式不同的平台编写的。
在 Canvas 元素中,坐标系从左上角开始,这意味着 m_hovercarBody.GetWorldPoint( b2Vec2(0,-5)) 正在检查角色上方的点,而不是下方的点。
我不确定代码的其余部分,但尝试将其更改为 m_hovercarBody.GetWorldPoint( b2Vec2(0,5)) 并看看会发生什么。
编辑:
我认为实际上问题在于您构建回调的方式。查找 Raycast 函数的参考将 揭示更多。
(您使用的 Box2D 的 Javascript 版本是 Actionscript 版本的自动移植版本。鉴于两者具有相当相似的语法,您可以使用 Flash参考。)
你发布的原始代码似乎是C++,但我不太了解它的语法。似乎有某种类可以进行光线投射(
overcarRayCastClosestCallback
)。您可以查找该函数,或者尝试根据我发布的第一个链接构建您自己的回调函数。大概是这样的:It might be that the original bit of code was written for a platform where the coordinate system works differently.
In a Canvas element, the coordinate system starts from the top left corner, meaning that
m_hovercarBody.GetWorldPoint( b2Vec2(0,-5))
is checking for a point above the character, rather than below.I'm not sure about the rest of the code but try changing that to
m_hovercarBody.GetWorldPoint( b2Vec2(0,5))
and see what happens.EDIT:
I think actually the problem is with the way you've structured your callback. Looking up the reference for the Raycast function would reveal more.
(The Javascript version of Box2D you're using is an automatic port of the Actionscript one. Given the two have fairly similar syntax, you can use the reference for Flash.)
The original code you posted seems to be C++, but I don't know much about its syntax. It seems there's some sort of class that does the raycasting (
overcarRayCastClosestCallback
). You can either look for that, or try and build your own callback function according to the first link I posted. It would be something along the lines of:尝试在您的浏览器上运行它。当我在 Box2Dweb 中学习传感器和 RAYCAST 时,我编写了这个代码。希望有帮助。
Try running this on your browser. I coded this when I was learning sensor and RAYCAST in Box2Dweb. Hope it helps.
这是我在 Box2dWeb 中工作的 Raycast:
Here's a Raycast in Box2dWeb that I got working: