2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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.
19 #include "b2CircleContact.h"
20 #include "../b2Body.h"
21 #include "../b2WorldCallbacks.h"
22 #include "../../Common/b2BlockAllocator.h"
27 b2Contact* b2CircleContact::Create(b2Shape* shape1, b2Shape* shape2, b2BlockAllocator* allocator)
29 void* mem = allocator->Allocate(sizeof(b2CircleContact));
30 return new (mem) b2CircleContact(shape1, shape2);
33 void b2CircleContact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
35 ((b2CircleContact*)contact)->~b2CircleContact();
36 allocator->Free(contact, sizeof(b2CircleContact));
39 b2CircleContact::b2CircleContact(b2Shape* s1, b2Shape* s2)
42 b2Assert(m_shape1->GetType() == e_circleShape);
43 b2Assert(m_shape2->GetType() == e_circleShape);
44 m_manifold.pointCount = 0;
45 m_manifold.points[0].normalImpulse = 0.0f;
46 m_manifold.points[0].tangentImpulse = 0.0f;
49 void b2CircleContact::Evaluate(b2ContactListener* listener)
51 b2Body* b1 = m_shape1->GetBody();
52 b2Body* b2 = m_shape2->GetBody();
55 memcpy(&m0, &m_manifold, sizeof(b2Manifold));
57 b2CollideCircles(&m_manifold, (b2CircleShape*)m_shape1, b1->GetXForm(), (b2CircleShape*)m_shape2, b2->GetXForm());
62 cp.friction = m_friction;
63 cp.restitution = m_restitution;
65 if (m_manifold.pointCount > 0)
68 b2ManifoldPoint* mp = m_manifold.points + 0;
70 if (m0.pointCount == 0)
72 mp->normalImpulse = 0.0f;
73 mp->tangentImpulse = 0.0f;
77 cp.position = b1->GetWorldPoint(mp->localPoint1);
78 b2Vec2 v1 = b1->GetLinearVelocityFromLocalPoint(mp->localPoint1);
79 b2Vec2 v2 = b2->GetLinearVelocityFromLocalPoint(mp->localPoint2);
80 cp.velocity = v2 - v1;
81 cp.normal = m_manifold.normal;
82 cp.separation = mp->separation;
89 b2ManifoldPoint* mp0 = m0.points + 0;
90 mp->normalImpulse = mp0->normalImpulse;
91 mp->tangentImpulse = mp0->tangentImpulse;
95 cp.position = b1->GetWorldPoint(mp->localPoint1);
96 b2Vec2 v1 = b1->GetLinearVelocityFromLocalPoint(mp->localPoint1);
97 b2Vec2 v2 = b2->GetLinearVelocityFromLocalPoint(mp->localPoint2);
98 cp.velocity = v2 - v1;
99 cp.normal = m_manifold.normal;
100 cp.separation = mp->separation;
102 listener->Persist(&cp);
109 if (m0.pointCount > 0 && listener)
111 b2ManifoldPoint* mp0 = m0.points + 0;
112 cp.position = b1->GetWorldPoint(mp0->localPoint1);
113 b2Vec2 v1 = b1->GetLinearVelocityFromLocalPoint(mp0->localPoint1);
114 b2Vec2 v2 = b2->GetLinearVelocityFromLocalPoint(mp0->localPoint2);
115 cp.velocity = v2 - v1;
116 cp.normal = m0.normal;
117 cp.separation = mp0->separation;
119 listener->Remove(&cp);