first commit
[blok] / Box2D / Source / Dynamics / Contacts / b2CircleContact.cpp
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
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 #include "b2CircleContact.h"
20 #include "../b2Body.h"
21 #include "../b2WorldCallbacks.h"
22 #include "../../Common/b2BlockAllocator.h"
23
24 #include <new>
25 #include <string.h>
26
27 b2Contact* b2CircleContact::Create(b2Shape* shape1, b2Shape* shape2, b2BlockAllocator* allocator)
28 {
29         void* mem = allocator->Allocate(sizeof(b2CircleContact));
30         return new (mem) b2CircleContact(shape1, shape2);
31 }
32
33 void b2CircleContact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
34 {
35         ((b2CircleContact*)contact)->~b2CircleContact();
36         allocator->Free(contact, sizeof(b2CircleContact));
37 }
38
39 b2CircleContact::b2CircleContact(b2Shape* s1, b2Shape* s2)
40 : b2Contact(s1, s2)
41 {
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;
47 }
48
49 void b2CircleContact::Evaluate(b2ContactListener* listener)
50 {
51         b2Body* b1 = m_shape1->GetBody();
52         b2Body* b2 = m_shape2->GetBody();
53
54         b2Manifold m0;
55         memcpy(&m0, &m_manifold, sizeof(b2Manifold));
56
57         b2CollideCircles(&m_manifold, (b2CircleShape*)m_shape1, b1->GetXForm(), (b2CircleShape*)m_shape2, b2->GetXForm());
58
59         b2ContactPoint cp;
60         cp.shape1 = m_shape1;
61         cp.shape2 = m_shape2;
62         cp.friction = m_friction;
63         cp.restitution = m_restitution;
64
65         if (m_manifold.pointCount > 0)
66         {
67                 m_manifoldCount = 1;
68                 b2ManifoldPoint* mp = m_manifold.points + 0;
69
70                 if (m0.pointCount == 0)
71                 {
72                         mp->normalImpulse = 0.0f;
73                         mp->tangentImpulse = 0.0f;
74
75                         if (listener)
76                         {
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;
83                                 cp.id = mp->id;
84                                 listener->Add(&cp);
85                         }
86                 }
87                 else
88                 {
89                         b2ManifoldPoint* mp0 = m0.points + 0;
90                         mp->normalImpulse = mp0->normalImpulse;
91                         mp->tangentImpulse = mp0->tangentImpulse;
92
93                         if (listener)
94                         {
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;
101                                 cp.id = mp->id;
102                                 listener->Persist(&cp);
103                         }
104                 }
105         }
106         else
107         {
108                 m_manifoldCount = 0;
109                 if (m0.pointCount > 0 && listener)
110                 {
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;
118                         cp.id = mp0->id;
119                         listener->Remove(&cp);
120                 }
121         }
122 }