似乎无法让ApplyForce()以有意义的方式工作
我在 IOS/cocos2D/Box2D 游戏中使用 b2Body->ApplyForce() 时遇到问题。我有一个球对象,我正在施加力来模拟高尔夫球杆的击球。用户设置射门的角度和力量,然后点击射门按钮来射门。我使用的代码如下:
CGPoint ballPosition = game.theBall.position;
b2Vec2 force;
force.x = ((game.power*POWER_REDUCTION) * cos(game.angle))/PTM_RATIO; // cocos2D angle
force.y = ((game.power*POWER_REDUCTION) * sin(game.angle))/PTM_RATIO;
CCLOG(@"Force: %f, %f", force.x, force.y);
CCLOG(@"Angle: %.4f", game.angle);
b2Vec2 point = body->GetWorldPoint(b2Vec2(0.0f, 0.0f));
body->ApplyForce(force, point);
POWER_REDUCTION 只是我在测试时使用的定义常量,因此我可以影响射击的力量并拨入正确的数字。在我的 Update 方法中,我重新定位与其表示的 Box2D 主体关联的精灵:
GolfBallObject *ballSprite = [self getChildByTag:GOLF_BALL_SPRITE_TAG];
if (!ballSprite)
{
CCLOG(@"in PlayLayer->update: [self getChildByTag:GOLF_BALL_SPRITE_TAG] returned nil");
}
b2Vec2 p = theBall->GetWorldPoint(b2Vec2(0.0f, 0.0f));
CGPoint newPosition = ccp(p.x*PTM_RATIO, p.y*PTM_RATIO);
ballSprite.position = newPosition;
game.theBall.position = newPosition;
CCLOG(@"In Flight - ball position: %.2f, %.2f", game.theBall.position.x, game.theBall.position.y);
theBall 是与我的球精灵关联的 b2Body。
无论我如何调整施加的力,球都会遵循相同的飞行:
Shooting - ball position: 120.00, 455.00
Force: -0.004102, 0.004030
Angle: 14.9314
In Flight - ball position: -1441.75, -731.84
In Flight - ball position: -3015.97, -1961.38
In Flight - ball position: -4591.16, -3192.20
另一个:
Shooting - ball position: 120.00, 455.00
Force: -0.410169, 0.402972
Angle: 14.9314
In Flight - ball position: -1441.75, -731.84
In Flight - ball position: -3015.97, -1961.38
In Flight - ball position: -4591.16, -3192.20
这是我改变角度的地方:
Shooting - ball position: 120.00, 455.00
Force: -0.561694, 0.122985
Angle: 78.3243
In Flight - ball position: -1441.75, -731.84
In Flight - ball position: -3015.97, -1961.38
In Flight - ball position: -4591.16, -3192.20
我以前从未使用过 Box2D,所以我确信我错过了一些可能是基本的东西,但这种行为似乎完全不合理。角度和力似乎都不相关。你能指出我做错了什么吗?
谢谢!
-迪克
I’m having a problem utilizing b2Body->ApplyForce() in an IOS/cocos2D/Box2D game. I have a ball object, and I’m applying force to simulate a golf club strike on it. The user sets the angle and power of the shot, then taps a shoot button to shoot the ball. The code I’m using follows:
CGPoint ballPosition = game.theBall.position;
b2Vec2 force;
force.x = ((game.power*POWER_REDUCTION) * cos(game.angle))/PTM_RATIO; // cocos2D angle
force.y = ((game.power*POWER_REDUCTION) * sin(game.angle))/PTM_RATIO;
CCLOG(@"Force: %f, %f", force.x, force.y);
CCLOG(@"Angle: %.4f", game.angle);
b2Vec2 point = body->GetWorldPoint(b2Vec2(0.0f, 0.0f));
body->ApplyForce(force, point);
POWER_REDUCTION is just a defined constant I’m using while testing so I can affect the power of the shot and dial in the right numbers. In my Update method, I’m re-positioning the sprite associated with the Box2D body it represents:
GolfBallObject *ballSprite = [self getChildByTag:GOLF_BALL_SPRITE_TAG];
if (!ballSprite)
{
CCLOG(@"in PlayLayer->update: [self getChildByTag:GOLF_BALL_SPRITE_TAG] returned nil");
}
b2Vec2 p = theBall->GetWorldPoint(b2Vec2(0.0f, 0.0f));
CGPoint newPosition = ccp(p.x*PTM_RATIO, p.y*PTM_RATIO);
ballSprite.position = newPosition;
game.theBall.position = newPosition;
CCLOG(@"In Flight - ball position: %.2f, %.2f", game.theBall.position.x, game.theBall.position.y);
theBall is the b2Body tied to my ball sprite.
No matter how I adjust the force applied, the ball follows the same flight:
Shooting - ball position: 120.00, 455.00
Force: -0.004102, 0.004030
Angle: 14.9314
In Flight - ball position: -1441.75, -731.84
In Flight - ball position: -3015.97, -1961.38
In Flight - ball position: -4591.16, -3192.20
another:
Shooting - ball position: 120.00, 455.00
Force: -0.410169, 0.402972
Angle: 14.9314
In Flight - ball position: -1441.75, -731.84
In Flight - ball position: -3015.97, -1961.38
In Flight - ball position: -4591.16, -3192.20
Here’s one where I change the angle:
Shooting - ball position: 120.00, 455.00
Force: -0.561694, 0.122985
Angle: 78.3243
In Flight - ball position: -1441.75, -731.84
In Flight - ball position: -3015.97, -1961.38
In Flight - ball position: -4591.16, -3192.20
I’ve never used Box2D before, so I’m sure there is something I’m missing that’s probably elementary, but this behavior seems totally irrational. Both the angle and the force seem to be non-relevant. Can you please point out what I’m doing wrong?
Thanks!
-Dick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你真的想为此使用施加脉冲...施加力是为了平稳、连续的运动,而不是高尔夫球杆的突然击打。另外,尝试改变撞击点。如果你从顶部垂直向下击打篮球,它总是会向上弹出。如果你从侧面敲击它,它就会向侧面移动。
I think you really want to use apply implulse for thiS...apply force is for a smooth, continuous motion, not a sudden strike of a golf club. Also, try changing the point of impact. If you strike a basketball straight downward from the top it will alway pop upwards. If you strike it from the side, it will move sideways.