开发者

Body moves too fast after ApplyLinearImpulse()

开发者 https://www.devze.com 2023-01-21 14:08 出处:网络
I have top-down game for iPhone (no gravity) and I\'m using Cocos2d and Box2d. I try to move a bullet by this code:

I have top-down game for iPhone (no gravity) and I'm using Cocos2d and Box2d.

I try to move a bullet by this code:

// 'targetPosition' is a point of touch

b2Vec2 touchInWorld = b2Vec2(targetPosition.x/PTM_RATIO, targetPosition.y/PTM_RATIO); 

b2Vec2 direction = b2Vec2(touchInWorld.x - ballBodyDef.position.x, touchInWorld.y - ballBodyDef.position.y);

b2Vec2 force = b2Vec2(direction.x, direction.y);

force.Normalize();

ballBody->ApplyLinearImpulse(force, ballBodyDef.position);

The problem is that the ball moves waaaay to fast if the sprite is small (10x10 px).

If the sprite is 50x50, then the speed is smaller and looks okay.

It's driving me crazy because I can't control the speed at all.

And not only that, if I don't put force.Normalize() the the speed is differ开发者_如何学运维ent depending on the direction of touch ...

Everything was working great when I was using just Cocos2d and animations. I tried using Box2d to implement collisions but it seems such a HUGE amount of effort that I am considering doing the physics myself :(


First thing , i am not sure but why are you dividing by PTM_Ratios here, i don think you need that, may be you are handling it some other way.

Second, 10x10 travels faster than 50x50 because you are setting SetMassFromShapes, so assuming you are creating a 50/PTM_RATIOSx50/PTM_RATIO body for 50x50 sprite, the body has greater mass than 10x10 body. so it travels slow.

Third, you are using Distance for Impulse so definatly it you are not going to Normalize you are going to get a Proportional Impulse to your distance

and for the Hackish Solution , if nothing else works for you just Decrease the PTM_RATIO!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号