1 /*
2 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 
19 #ifndef B2_BODY_H
20 #define B2_BODY_H
21 
22 #include <Box2D/Common/b2Math.h>
23 #include <Box2D/Collision/Shapes/b2Shape.h>
24 #include <memory>
25 
26 class b2Fixture;
27 class b2Joint;
28 class b2Contact;
29 class b2Controller;
30 class b2World;
31 struct b2FixtureDef;
32 struct b2JointEdge;
33 struct b2ContactEdge;
34 
35 /// The body type.
36 /// static: zero mass, zero velocity, may be manually moved
37 /// kinematic: zero mass, non-zero velocity set by user, moved by solver
38 /// dynamic: positive mass, non-zero velocity determined by forces, moved by solver
39 enum b2BodyType
40 {
41 	b2_staticBody = 0,
42 	b2_kinematicBody,
43 	b2_dynamicBody
44 
45 	// TODO_ERIN
46 	//b2_bulletBody,
47 };
48 
49 /// A body definition holds all the data needed to construct a rigid body.
50 /// You can safely re-use body definitions. Shapes are added to a body after construction.
51 struct b2BodyDef
52 {
53 	/// This constructor sets the body definition default values.
b2BodyDefb2BodyDef54 	b2BodyDef()
55 	{
56 		userData = NULL;
57 		position.Set(0.0f, 0.0f);
58 		angle = 0.0f;
59 		linearVelocity.Set(0.0f, 0.0f);
60 		angularVelocity = 0.0f;
61 		linearDamping = 0.0f;
62 		angularDamping = 0.0f;
63 		allowSleep = true;
64 		awake = true;
65 		fixedRotation = false;
66 		bullet = false;
67 		type = b2_staticBody;
68 		active = true;
69 		gravityScale = 1.0f;
70 	}
71 
72 	/// The body type: static, kinematic, or dynamic.
73 	/// Note: if a dynamic body would have zero mass, the mass is set to one.
74 	b2BodyType type;
75 
76 	/// The world position of the body. Avoid creating bodies at the origin
77 	/// since this can lead to many overlapping shapes.
78 	b2Vec2 position;
79 
80 	/// The world angle of the body in radians.
81 	float32 angle;
82 
83 	/// The linear velocity of the body's origin in world co-ordinates.
84 	b2Vec2 linearVelocity;
85 
86 	/// The angular velocity of the body.
87 	float32 angularVelocity;
88 
89 	/// Linear damping is use to reduce the linear velocity. The damping parameter
90 	/// can be larger than 1.0f but the damping effect becomes sensitive to the
91 	/// time step when the damping parameter is large.
92 	float32 linearDamping;
93 
94 	/// Angular damping is use to reduce the angular velocity. The damping parameter
95 	/// can be larger than 1.0f but the damping effect becomes sensitive to the
96 	/// time step when the damping parameter is large.
97 	float32 angularDamping;
98 
99 	/// Set this flag to false if this body should never fall asleep. Note that
100 	/// this increases CPU usage.
101 	bool allowSleep;
102 
103 	/// Is this body initially awake or sleeping?
104 	bool awake;
105 
106 	/// Should this body be prevented from rotating? Useful for characters.
107 	bool fixedRotation;
108 
109 	/// Is this a fast moving body that should be prevented from tunneling through
110 	/// other moving bodies? Note that all bodies are prevented from tunneling through
111 	/// kinematic and static bodies. This setting is only considered on dynamic bodies.
112 	/// @warning You should use this flag sparingly since it increases processing time.
113 	bool bullet;
114 
115 	/// Does this body start out active?
116 	bool active;
117 
118 	/// Use this to store application specific body data.
119 	void* userData;
120 
121 	/// Scale the gravity applied to this body.
122 	float32 gravityScale;
123 };
124 
125 /// A rigid body. These are created via b2World::CreateBody.
126 class b2Body
127 {
128 public:
129 	/// Creates a fixture and attach it to this body. Use this function if you need
130 	/// to set some fixture parameters, like friction. Otherwise you can create the
131 	/// fixture directly from a shape.
132 	/// If the density is non-zero, this function automatically updates the mass of the body.
133 	/// Contacts are not created until the next time step.
134 	/// @param def the fixture definition.
135 	/// @warning This function is locked during callbacks.
136 	b2Fixture* CreateFixture(const b2FixtureDef* def);
137 
138 	/// Creates a fixture from a shape and attach it to this body.
139 	/// This is a convenience function. Use b2FixtureDef if you need to set parameters
140 	/// like friction, restitution, user data, or filtering.
141 	/// If the density is non-zero, this function automatically updates the mass of the body.
142 	/// @param shape the shape to be cloned.
143 	/// @param density the shape density (set to zero for static bodies).
144 	/// @warning This function is locked during callbacks.
145 	b2Fixture* CreateFixture(const b2Shape* shape, float32 density);
146 
147 	/// Destroy a fixture. This removes the fixture from the broad-phase and
148 	/// destroys all contacts associated with this fixture. This will
149 	/// automatically adjust the mass of the body if the body is dynamic and the
150 	/// fixture has positive density.
151 	/// All fixtures attached to a body are implicitly destroyed when the body is destroyed.
152 	/// @param fixture the fixture to be removed.
153 	/// @warning This function is locked during callbacks.
154 	void DestroyFixture(b2Fixture* fixture);
155 
156 	/// Set the position of the body's origin and rotation.
157 	/// Manipulating a body's transform may cause non-physical behavior.
158 	/// Note: contacts are updated on the next call to b2World::Step.
159 	/// @param position the world position of the body's local origin.
160 	/// @param angle the world rotation in radians.
161 	void SetTransform(const b2Vec2& position, float32 angle);
162 
163 	/// Get the body transform for the body's origin.
164 	/// @return the world transform of the body's origin.
165 	const b2Transform& GetTransform() const;
166 
167 	/// Get the world body origin position.
168 	/// @return the world position of the body's origin.
169 	const b2Vec2& GetPosition() const;
170 
171 	/// Get the angle in radians.
172 	/// @return the current world rotation angle in radians.
173 	float32 GetAngle() const;
174 
175 	/// Get the world position of the center of mass.
176 	const b2Vec2& GetWorldCenter() const;
177 
178 	/// Get the local position of the center of mass.
179 	const b2Vec2& GetLocalCenter() const;
180 
181 	/// Set the linear velocity of the center of mass.
182 	/// @param v the new linear velocity of the center of mass.
183 	void SetLinearVelocity(const b2Vec2& v);
184 
185 	/// Get the linear velocity of the center of mass.
186 	/// @return the linear velocity of the center of mass.
187 	const b2Vec2& GetLinearVelocity() const;
188 
189 	/// Set the angular velocity.
190 	/// @param omega the new angular velocity in radians/second.
191 	void SetAngularVelocity(float32 omega);
192 
193 	/// Get the angular velocity.
194 	/// @return the angular velocity in radians/second.
195 	float32 GetAngularVelocity() const;
196 
197 	/// Apply a force at a world point. If the force is not
198 	/// applied at the center of mass, it will generate a torque and
199 	/// affect the angular velocity. This wakes up the body.
200 	/// @param force the world force vector, usually in Newtons (N).
201 	/// @param point the world position of the point of application.
202 	/// @param wake also wake up the body
203 	void ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake);
204 
205 	/// Apply a force to the center of mass. This wakes up the body.
206 	/// @param force the world force vector, usually in Newtons (N).
207 	/// @param wake also wake up the body
208 	void ApplyForceToCenter(const b2Vec2& force, bool wake);
209 
210 	/// Apply a torque. This affects the angular velocity
211 	/// without affecting the linear velocity of the center of mass.
212 	/// @param torque about the z-axis (out of the screen), usually in N-m.
213 	/// @param wake also wake up the body
214 	void ApplyTorque(float32 torque, bool wake);
215 
216 	/// Apply an impulse at a point. This immediately modifies the velocity.
217 	/// It also modifies the angular velocity if the point of application
218 	/// is not at the center of mass. This wakes up the body.
219 	/// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
220 	/// @param point the world position of the point of application.
221 	/// @param wake also wake up the body
222 	void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake);
223 
224 	/// Apply an impulse to the center of mass. This immediately modifies the velocity.
225 	/// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
226 	/// @param wake also wake up the body
227 	void ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake);
228 
229 	/// Apply an angular impulse.
230 	/// @param impulse the angular impulse in units of kg*m*m/s
231 	/// @param wake also wake up the body
232 	void ApplyAngularImpulse(float32 impulse, bool wake);
233 
234 	/// Get the total mass of the body.
235 	/// @return the mass, usually in kilograms (kg).
236 	float32 GetMass() const;
237 
238 	/// Get the rotational inertia of the body about the local origin.
239 	/// @return the rotational inertia, usually in kg-m^2.
240 	float32 GetInertia() const;
241 
242 	/// Get the mass data of the body.
243 	/// @return a struct containing the mass, inertia and center of the body.
244 	void GetMassData(b2MassData* data) const;
245 
246 	/// Set the mass properties to override the mass properties of the fixtures.
247 	/// Note that this changes the center of mass position.
248 	/// Note that creating or destroying fixtures can also alter the mass.
249 	/// This function has no effect if the body isn't dynamic.
250 	/// @param massData the mass properties.
251 	void SetMassData(const b2MassData* data);
252 
253 	/// This resets the mass properties to the sum of the mass properties of the fixtures.
254 	/// This normally does not need to be called unless you called SetMassData to override
255 	/// the mass and you later want to reset the mass.
256 	void ResetMassData();
257 
258 	/// Get the world coordinates of a point given the local coordinates.
259 	/// @param localPoint a point on the body measured relative the the body's origin.
260 	/// @return the same point expressed in world coordinates.
261 	b2Vec2 GetWorldPoint(const b2Vec2& localPoint) const;
262 
263 	/// Get the world coordinates of a vector given the local coordinates.
264 	/// @param localVector a vector fixed in the body.
265 	/// @return the same vector expressed in world coordinates.
266 	b2Vec2 GetWorldVector(const b2Vec2& localVector) const;
267 
268 	/// Gets a local point relative to the body's origin given a world point.
269 	/// @param a point in world coordinates.
270 	/// @return the corresponding local point relative to the body's origin.
271 	b2Vec2 GetLocalPoint(const b2Vec2& worldPoint) const;
272 
273 	/// Gets a local vector given a world vector.
274 	/// @param a vector in world coordinates.
275 	/// @return the corresponding local vector.
276 	b2Vec2 GetLocalVector(const b2Vec2& worldVector) const;
277 
278 	/// Get the world linear velocity of a world point attached to this body.
279 	/// @param a point in world coordinates.
280 	/// @return the world velocity of a point.
281 	b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
282 
283 	/// Get the world velocity of a local point.
284 	/// @param a point in local coordinates.
285 	/// @return the world velocity of a point.
286 	b2Vec2 GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const;
287 
288 	/// Get the linear damping of the body.
289 	float32 GetLinearDamping() const;
290 
291 	/// Set the linear damping of the body.
292 	void SetLinearDamping(float32 linearDamping);
293 
294 	/// Get the angular damping of the body.
295 	float32 GetAngularDamping() const;
296 
297 	/// Set the angular damping of the body.
298 	void SetAngularDamping(float32 angularDamping);
299 
300 	/// Get the gravity scale of the body.
301 	float32 GetGravityScale() const;
302 
303 	/// Set the gravity scale of the body.
304 	void SetGravityScale(float32 scale);
305 
306 	/// Set the type of this body. This may alter the mass and velocity.
307 	void SetType(b2BodyType type);
308 
309 	/// Get the type of this body.
310 	b2BodyType GetType() const;
311 
312 	/// Should this body be treated like a bullet for continuous collision detection?
313 	void SetBullet(bool flag);
314 
315 	/// Is this body treated like a bullet for continuous collision detection?
316 	bool IsBullet() const;
317 
318 	/// You can disable sleeping on this body. If you disable sleeping, the
319 	/// body will be woken.
320 	void SetSleepingAllowed(bool flag);
321 
322 	/// Is this body allowed to sleep
323 	bool IsSleepingAllowed() const;
324 
325 	/// Set the sleep state of the body. A sleeping body has very
326 	/// low CPU cost.
327 	/// @param flag set to true to wake the body, false to put it to sleep.
328 	void SetAwake(bool flag);
329 
330 	/// Get the sleeping state of this body.
331 	/// @return true if the body is awake.
332 	bool IsAwake() const;
333 
334 	/// Set the active state of the body. An inactive body is not
335 	/// simulated and cannot be collided with or woken up.
336 	/// If you pass a flag of true, all fixtures will be added to the
337 	/// broad-phase.
338 	/// If you pass a flag of false, all fixtures will be removed from
339 	/// the broad-phase and all contacts will be destroyed.
340 	/// Fixtures and joints are otherwise unaffected. You may continue
341 	/// to create/destroy fixtures and joints on inactive bodies.
342 	/// Fixtures on an inactive body are implicitly inactive and will
343 	/// not participate in collisions, ray-casts, or queries.
344 	/// Joints connected to an inactive body are implicitly inactive.
345 	/// An inactive body is still owned by a b2World object and remains
346 	/// in the body list.
347 	void SetActive(bool flag);
348 
349 	/// Get the active state of the body.
350 	bool IsActive() const;
351 
352 	/// Set this body to have fixed rotation. This causes the mass
353 	/// to be reset.
354 	void SetFixedRotation(bool flag);
355 
356 	/// Does this body have fixed rotation?
357 	bool IsFixedRotation() const;
358 
359 	/// Get the list of all fixtures attached to this body.
360 	b2Fixture* GetFixtureList();
361 	const b2Fixture* GetFixtureList() const;
362 
363 	/// Get the list of all joints attached to this body.
364 	b2JointEdge* GetJointList();
365 	const b2JointEdge* GetJointList() const;
366 
367 	/// Get the list of all contacts attached to this body.
368 	/// @warning this list changes during the time step and you may
369 	/// miss some collisions if you don't use b2ContactListener.
370 	b2ContactEdge* GetContactList();
371 	const b2ContactEdge* GetContactList() const;
372 
373 	/// Get the next body in the world's body list.
374 	b2Body* GetNext();
375 	const b2Body* GetNext() const;
376 
377 	/// Get the user data pointer that was provided in the body definition.
378 	void* GetUserData() const;
379 
380 	/// Set the user data. Use this to store your application specific data.
381 	void SetUserData(void* data);
382 
383 	/// Get the parent world of this body.
384 	b2World* GetWorld();
385 	const b2World* GetWorld() const;
386 
387 	/// Dump this body to a log file
388 	void Dump();
389 
390 private:
391 
392 	friend class b2World;
393 	friend class b2Island;
394 	friend class b2ContactManager;
395 	friend class b2ContactSolver;
396 	friend class b2Contact;
397 
398 	friend class b2DistanceJoint;
399 	friend class b2FrictionJoint;
400 	friend class b2GearJoint;
401 	friend class b2MotorJoint;
402 	friend class b2MouseJoint;
403 	friend class b2PrismaticJoint;
404 	friend class b2PulleyJoint;
405 	friend class b2RevoluteJoint;
406 	friend class b2RopeJoint;
407 	friend class b2WeldJoint;
408 	friend class b2WheelJoint;
409 
410 	// m_flags
411 	enum
412 	{
413 		e_islandFlag		= 0x0001,
414 		e_awakeFlag			= 0x0002,
415 		e_autoSleepFlag		= 0x0004,
416 		e_bulletFlag		= 0x0008,
417 		e_fixedRotationFlag	= 0x0010,
418 		e_activeFlag		= 0x0020,
419 		e_toiFlag			= 0x0040
420 	};
421 
422 	b2Body(const b2BodyDef* bd, b2World* world);
423 	~b2Body();
424 
425 	void SynchronizeFixtures();
426 	void SynchronizeTransform();
427 
428 	// This is used to prevent connected bodies from colliding.
429 	// It may lie, depending on the collideConnected flag.
430 	bool ShouldCollide(const b2Body* other) const;
431 
432 	void Advance(float32 t);
433 
434 	b2BodyType m_type;
435 
436 	uint16 m_flags;
437 
438 	int32 m_islandIndex;
439 
440 	b2Transform m_xf;		// the body origin transform
441 	b2Sweep m_sweep;		// the swept motion for CCD
442 
443 	b2Vec2 m_linearVelocity;
444 	float32 m_angularVelocity;
445 
446 	b2Vec2 m_force;
447 	float32 m_torque;
448 
449 	b2World* m_world;
450 	b2Body* m_prev;
451 	b2Body* m_next;
452 
453 	b2Fixture* m_fixtureList;
454 	int32 m_fixtureCount;
455 
456 	b2JointEdge* m_jointList;
457 	b2ContactEdge* m_contactList;
458 
459 	float32 m_mass, m_invMass;
460 
461 	// Rotational inertia about the center of mass.
462 	float32 m_I, m_invI;
463 
464 	float32 m_linearDamping;
465 	float32 m_angularDamping;
466 	float32 m_gravityScale;
467 
468 	float32 m_sleepTime;
469 
470 	void* m_userData;
471 };
472 
GetType()473 inline b2BodyType b2Body::GetType() const
474 {
475 	return m_type;
476 }
477 
GetTransform()478 inline const b2Transform& b2Body::GetTransform() const
479 {
480 	return m_xf;
481 }
482 
GetPosition()483 inline const b2Vec2& b2Body::GetPosition() const
484 {
485 	return m_xf.p;
486 }
487 
GetAngle()488 inline float32 b2Body::GetAngle() const
489 {
490 	return m_sweep.a;
491 }
492 
GetWorldCenter()493 inline const b2Vec2& b2Body::GetWorldCenter() const
494 {
495 	return m_sweep.c;
496 }
497 
GetLocalCenter()498 inline const b2Vec2& b2Body::GetLocalCenter() const
499 {
500 	return m_sweep.localCenter;
501 }
502 
SetLinearVelocity(const b2Vec2 & v)503 inline void b2Body::SetLinearVelocity(const b2Vec2& v)
504 {
505 	if (m_type == b2_staticBody)
506 	{
507 		return;
508 	}
509 
510 	if (b2Dot(v,v) > 0.0f)
511 	{
512 		SetAwake(true);
513 	}
514 
515 	m_linearVelocity = v;
516 }
517 
GetLinearVelocity()518 inline const b2Vec2& b2Body::GetLinearVelocity() const
519 {
520 	return m_linearVelocity;
521 }
522 
SetAngularVelocity(float32 w)523 inline void b2Body::SetAngularVelocity(float32 w)
524 {
525 	if (m_type == b2_staticBody)
526 	{
527 		return;
528 	}
529 
530 	if (w * w > 0.0f)
531 	{
532 		SetAwake(true);
533 	}
534 
535 	m_angularVelocity = w;
536 }
537 
GetAngularVelocity()538 inline float32 b2Body::GetAngularVelocity() const
539 {
540 	return m_angularVelocity;
541 }
542 
GetMass()543 inline float32 b2Body::GetMass() const
544 {
545 	return m_mass;
546 }
547 
GetInertia()548 inline float32 b2Body::GetInertia() const
549 {
550 	return m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
551 }
552 
GetMassData(b2MassData * data)553 inline void b2Body::GetMassData(b2MassData* data) const
554 {
555 	data->mass = m_mass;
556 	data->I = m_I + m_mass * b2Dot(m_sweep.localCenter, m_sweep.localCenter);
557 	data->center = m_sweep.localCenter;
558 }
559 
GetWorldPoint(const b2Vec2 & localPoint)560 inline b2Vec2 b2Body::GetWorldPoint(const b2Vec2& localPoint) const
561 {
562 	return b2Mul(m_xf, localPoint);
563 }
564 
GetWorldVector(const b2Vec2 & localVector)565 inline b2Vec2 b2Body::GetWorldVector(const b2Vec2& localVector) const
566 {
567 	return b2Mul(m_xf.q, localVector);
568 }
569 
GetLocalPoint(const b2Vec2 & worldPoint)570 inline b2Vec2 b2Body::GetLocalPoint(const b2Vec2& worldPoint) const
571 {
572 	return b2MulT(m_xf, worldPoint);
573 }
574 
GetLocalVector(const b2Vec2 & worldVector)575 inline b2Vec2 b2Body::GetLocalVector(const b2Vec2& worldVector) const
576 {
577 	return b2MulT(m_xf.q, worldVector);
578 }
579 
GetLinearVelocityFromWorldPoint(const b2Vec2 & worldPoint)580 inline b2Vec2 b2Body::GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const
581 {
582 	return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_sweep.c);
583 }
584 
GetLinearVelocityFromLocalPoint(const b2Vec2 & localPoint)585 inline b2Vec2 b2Body::GetLinearVelocityFromLocalPoint(const b2Vec2& localPoint) const
586 {
587 	return GetLinearVelocityFromWorldPoint(GetWorldPoint(localPoint));
588 }
589 
GetLinearDamping()590 inline float32 b2Body::GetLinearDamping() const
591 {
592 	return m_linearDamping;
593 }
594 
SetLinearDamping(float32 linearDamping)595 inline void b2Body::SetLinearDamping(float32 linearDamping)
596 {
597 	m_linearDamping = linearDamping;
598 }
599 
GetAngularDamping()600 inline float32 b2Body::GetAngularDamping() const
601 {
602 	return m_angularDamping;
603 }
604 
SetAngularDamping(float32 angularDamping)605 inline void b2Body::SetAngularDamping(float32 angularDamping)
606 {
607 	m_angularDamping = angularDamping;
608 }
609 
GetGravityScale()610 inline float32 b2Body::GetGravityScale() const
611 {
612 	return m_gravityScale;
613 }
614 
SetGravityScale(float32 scale)615 inline void b2Body::SetGravityScale(float32 scale)
616 {
617 	m_gravityScale = scale;
618 }
619 
SetBullet(bool flag)620 inline void b2Body::SetBullet(bool flag)
621 {
622 	if (flag)
623 	{
624 		m_flags |= e_bulletFlag;
625 	}
626 	else
627 	{
628 		m_flags &= ~e_bulletFlag;
629 	}
630 }
631 
IsBullet()632 inline bool b2Body::IsBullet() const
633 {
634 	return (m_flags & e_bulletFlag) == e_bulletFlag;
635 }
636 
SetAwake(bool flag)637 inline void b2Body::SetAwake(bool flag)
638 {
639 	if (flag)
640 	{
641 		if ((m_flags & e_awakeFlag) == 0)
642 		{
643 			m_flags |= e_awakeFlag;
644 			m_sleepTime = 0.0f;
645 		}
646 	}
647 	else
648 	{
649 		m_flags &= ~e_awakeFlag;
650 		m_sleepTime = 0.0f;
651 		m_linearVelocity.SetZero();
652 		m_angularVelocity = 0.0f;
653 		m_force.SetZero();
654 		m_torque = 0.0f;
655 	}
656 }
657 
IsAwake()658 inline bool b2Body::IsAwake() const
659 {
660 	return (m_flags & e_awakeFlag) == e_awakeFlag;
661 }
662 
IsActive()663 inline bool b2Body::IsActive() const
664 {
665 	return (m_flags & e_activeFlag) == e_activeFlag;
666 }
667 
IsFixedRotation()668 inline bool b2Body::IsFixedRotation() const
669 {
670 	return (m_flags & e_fixedRotationFlag) == e_fixedRotationFlag;
671 }
672 
SetSleepingAllowed(bool flag)673 inline void b2Body::SetSleepingAllowed(bool flag)
674 {
675 	if (flag)
676 	{
677 		m_flags |= e_autoSleepFlag;
678 	}
679 	else
680 	{
681 		m_flags &= ~e_autoSleepFlag;
682 		SetAwake(true);
683 	}
684 }
685 
IsSleepingAllowed()686 inline bool b2Body::IsSleepingAllowed() const
687 {
688 	return (m_flags & e_autoSleepFlag) == e_autoSleepFlag;
689 }
690 
GetFixtureList()691 inline b2Fixture* b2Body::GetFixtureList()
692 {
693 	return m_fixtureList;
694 }
695 
GetFixtureList()696 inline const b2Fixture* b2Body::GetFixtureList() const
697 {
698 	return m_fixtureList;
699 }
700 
GetJointList()701 inline b2JointEdge* b2Body::GetJointList()
702 {
703 	return m_jointList;
704 }
705 
GetJointList()706 inline const b2JointEdge* b2Body::GetJointList() const
707 {
708 	return m_jointList;
709 }
710 
GetContactList()711 inline b2ContactEdge* b2Body::GetContactList()
712 {
713 	return m_contactList;
714 }
715 
GetContactList()716 inline const b2ContactEdge* b2Body::GetContactList() const
717 {
718 	return m_contactList;
719 }
720 
GetNext()721 inline b2Body* b2Body::GetNext()
722 {
723 	return m_next;
724 }
725 
GetNext()726 inline const b2Body* b2Body::GetNext() const
727 {
728 	return m_next;
729 }
730 
SetUserData(void * data)731 inline void b2Body::SetUserData(void* data)
732 {
733 	m_userData = data;
734 }
735 
GetUserData()736 inline void* b2Body::GetUserData() const
737 {
738 	return m_userData;
739 }
740 
ApplyForce(const b2Vec2 & force,const b2Vec2 & point,bool wake)741 inline void b2Body::ApplyForce(const b2Vec2& force, const b2Vec2& point, bool wake)
742 {
743 	if (m_type != b2_dynamicBody)
744 	{
745 		return;
746 	}
747 
748 	if (wake && (m_flags & e_awakeFlag) == 0)
749 	{
750 		SetAwake(true);
751 	}
752 
753 	// Don't accumulate a force if the body is sleeping.
754 	if (m_flags & e_awakeFlag)
755 	{
756 		m_force += force;
757 		m_torque += b2Cross(point - m_sweep.c, force);
758 	}
759 }
760 
ApplyForceToCenter(const b2Vec2 & force,bool wake)761 inline void b2Body::ApplyForceToCenter(const b2Vec2& force, bool wake)
762 {
763 	if (m_type != b2_dynamicBody)
764 	{
765 		return;
766 	}
767 
768 	if (wake && (m_flags & e_awakeFlag) == 0)
769 	{
770 		SetAwake(true);
771 	}
772 
773 	// Don't accumulate a force if the body is sleeping
774 	if (m_flags & e_awakeFlag)
775 	{
776 		m_force += force;
777 	}
778 }
779 
ApplyTorque(float32 torque,bool wake)780 inline void b2Body::ApplyTorque(float32 torque, bool wake)
781 {
782 	if (m_type != b2_dynamicBody)
783 	{
784 		return;
785 	}
786 
787 	if (wake && (m_flags & e_awakeFlag) == 0)
788 	{
789 		SetAwake(true);
790 	}
791 
792 	// Don't accumulate a force if the body is sleeping
793 	if (m_flags & e_awakeFlag)
794 	{
795 		m_torque += torque;
796 	}
797 }
798 
ApplyLinearImpulse(const b2Vec2 & impulse,const b2Vec2 & point,bool wake)799 inline void b2Body::ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point, bool wake)
800 {
801 	if (m_type != b2_dynamicBody)
802 	{
803 		return;
804 	}
805 
806 	if (wake && (m_flags & e_awakeFlag) == 0)
807 	{
808 		SetAwake(true);
809 	}
810 
811 	// Don't accumulate velocity if the body is sleeping
812 	if (m_flags & e_awakeFlag)
813 	{
814 		m_linearVelocity += m_invMass * impulse;
815 		m_angularVelocity += m_invI * b2Cross(point - m_sweep.c, impulse);
816 	}
817 }
818 
ApplyLinearImpulseToCenter(const b2Vec2 & impulse,bool wake)819 inline void b2Body::ApplyLinearImpulseToCenter(const b2Vec2& impulse, bool wake)
820 {
821 	if (m_type != b2_dynamicBody)
822 	{
823 		return;
824 	}
825 
826 	if (wake && (m_flags & e_awakeFlag) == 0)
827 	{
828 		SetAwake(true);
829 	}
830 
831 	// Don't accumulate velocity if the body is sleeping
832 	if (m_flags & e_awakeFlag)
833 	{
834 		m_linearVelocity += m_invMass * impulse;
835 	}
836 }
837 
ApplyAngularImpulse(float32 impulse,bool wake)838 inline void b2Body::ApplyAngularImpulse(float32 impulse, bool wake)
839 {
840 	if (m_type != b2_dynamicBody)
841 	{
842 		return;
843 	}
844 
845 	if (wake && (m_flags & e_awakeFlag) == 0)
846 	{
847 		SetAwake(true);
848 	}
849 
850 	// Don't accumulate velocity if the body is sleeping
851 	if (m_flags & e_awakeFlag)
852 	{
853 		m_angularVelocity += m_invI * impulse;
854 	}
855 }
856 
SynchronizeTransform()857 inline void b2Body::SynchronizeTransform()
858 {
859 	m_xf.q.Set(m_sweep.a);
860 	m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
861 }
862 
Advance(float32 alpha)863 inline void b2Body::Advance(float32 alpha)
864 {
865 	// Advance to the new safe time. This doesn't sync the broad-phase.
866 	m_sweep.Advance(alpha);
867 	m_sweep.c = m_sweep.c0;
868 	m_sweep.a = m_sweep.a0;
869 	m_xf.q.Set(m_sweep.a);
870 	m_xf.p = m_sweep.c - b2Mul(m_xf.q, m_sweep.localCenter);
871 }
872 
GetWorld()873 inline b2World* b2Body::GetWorld()
874 {
875 	return m_world;
876 }
877 
GetWorld()878 inline const b2World* b2Body::GetWorld() const
879 {
880 	return m_world;
881 }
882 
883 #endif
884