0

In my app I am simply moving one of the sprites I created in SpriteBuilder like so:

[_sprite.physicsBody applyImpulse:ccp(95.0f, 0.0f)];

However, if I continuously call this, it will make the sprite go much much faster than I like. Is there a way to use clampf or something to make sure the velocity of the _sprite.physicsBody is in check?

I tried something like this but obviously wont work because it expects float and not CGPoints:

_sprite.physicsBody.velocity = clampf(_sprite.physicsBody.velocity, CGPointMake(-100.0f, 0.0f), CGPointMake(0.0f, 100.0f));

But is there something similar to this that will achieve what I want while I still use my applyImpulse/applyForce methods to move my physicsBody? Any help would be appreciated.

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • Applying an impulse in that case would be wrong, since you want a constant velocity. – duci9y Aug 09 '14 at 07:43
  • you need to clamp the point's x and y individually – CodeSmile Aug 09 '14 at 08:12
  • @duci9y What would it be then, applyForce? – SimplyKiwi Aug 09 '14 at 20:12
  • @LearnCocos2D That is not possible because the physicsBody's velocity .x and .y are not assignable. – SimplyKiwi Aug 09 '14 at 20:12
  • of course not, you have to assign the velocity to a cgpoint local variable, clamp it, and assign it back to the physicsbody. Cgpoint.x.y are not objc properties, thats why it won't work without the temp variable. – CodeSmile Aug 09 '14 at 22:19
  • Ahhh ok I see what you mean @LearnCocos2D. From the other answerer however, what is the proper way to set constant velocity instead of applyImpulse? – SimplyKiwi Aug 09 '14 at 23:02

0 Answers0