We want the box2d engine to bounce us around and change our direction but we want to know just how fast we should be moving and try to nudge ourselves just enough, but in the current direction, in order to achieve that. Why speed? Why not a b2Vec? We don’t want a steady direction, just a steady rate of movement. We’re going to subclass BodyNode for this and add a little to it: CruiseControl : BodyNodeįloat (nonatomic) float got a BodyNode subclass to which we have added a speed member. Now let’s focus on our CruiseControl object. BodyNode : CCNodeĬCSprite* (readonly, nonatomic) b2Body* (readonly, nonatomic) CCSprite* more to it, but that’s enough to get us going here. In my case, I have a CCNode subclass which has member pointers to both the b2World and CCSprite objects. I will leave the pointer management concerns to you based on your own implementation. Likewise, you can also create a CCSprite subclass which has a b2Body* member and thus the two could refer to one another. b2Body objects do have a UserData attribute which is a void* and which can therefore store a pointer to, for example, a CCSprite. But I will pull out the pertinent bits and provide some illumination on what I’m doing.īefore we get started, remember that box2d is a physics simulator only. Here’s the actual velocity.” Instead, what we want to do is tell the box2d engine, “See that body over there? I want you to add this new impulse to it and factor that in along with everything else.” This lets the box2d engine take the entire model and any ongoing interactions into account rather than dropping everything and running with the new values.īecause I’m taking code out of my game Centripetal, I don’t have a full project with a demo set up to show you what I’m talking about. The problem is that doing so essentially tells the box2d engine “Hey, ignore whatever you *think* should be happening to that body. What we specifically do not want to do is simply call SetLinearVelocity(). How do you do that? That’s what we’re going to take a look out now.Ĭonceptually, what we want to do is measure our current speed on each update cycle and then fire an impulse of the appropriate size and direction in order to nudge us up to (or down to) speed. You might control the vector direction in some other method, but the speed is intended to remain constant. In this world gravity, friction (including rotational friction), inertia and momentum are all simulated.īut what of the concept of cruise control? You know, you set a speed and then the target continues to try to match that speed. It assumes you have a basic understanding of how to set up a box2d project with cocos2d.Īs you are no doubt aware, the box2d physics engine is a wonderful tool for creating a virtual world filled with objects that interact in a way analagous to the real world. But then Unity computes a new velocity using yours as initial velocity but still applying all other forces.This brief tutorial will show you how to set up dynamic objects with a constant speed in box2d. You've modified the velocity parameter in FixedUpdate, which means that you've set v0 explicitly. The key for the behavior you're observing is in the step 3. So when this acceleration is applied to the current velocity in step 3 results in the desired velocity finally applied to the rigidbody. Using AddForce means that the acceleration computed in step 2 includes the velocity change you've specified and assumes the effects of the other forces. But then Unity computes a new velocity v using yours as initial velocity but still applying all other forces. The position is computed out of the velocity: pos = pos0 + v*dt.The velocity is updated using the current velocity (v0) and the acceleration: v = v0 + a*dt.The acceleration is computed out of all forces affecting the rigidbody (gravity, collisions, AddForce, etc).Simplifying things a lot, this is what happens in each physics simulation step: That's because the Rigidbody is also computing and applying its own velocity on top of yours.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |