Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/BulletCollision/GIMPACT/btContactProcessing.h @ 1983

Last change on this file since 1983 was 1963, checked in by rgrieder, 16 years ago

Added Bullet physics engine.

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1#ifndef BT_CONTACT_H_INCLUDED
2#define BT_CONTACT_H_INCLUDED
3
4/*! \file gim_contact.h
5\author Francisco Len Nßjera
6*/
7/*
8This source file is part of GIMPACT Library.
9
10For the latest info, see http://gimpact.sourceforge.net/
11
12Copyright (c) 2007 Francisco Leon Najera. C.C. 80087371.
13email: projectileman@yahoo.com
14
15
16This software is provided 'as-is', without any express or implied warranty.
17In no event will the authors be held liable for any damages arising from the use of this software.
18Permission is granted to anyone to use this software for any purpose,
19including commercial applications, and to alter it and redistribute it freely,
20subject to the following restrictions:
21
221. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
232. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
243. This notice may not be removed or altered from any source distribution.
25*/
26
27#include "LinearMath/btTransform.h"
28#include "LinearMath/btAlignedObjectArray.h"
29#include "btTriangleShapeEx.h"
30
31
32/*! \defgroup CONTACTS
33\brief
34Functions for managing and sorting contacts resulting from a collision query.
35*/
36//! @{
37
38/**
39Configuration var for applying interpolation of  contact normals
40*/
41#define NORMAL_CONTACT_AVERAGE 1
42
43#define CONTACT_DIFF_EPSILON 0.00001f
44
45/// Structure for collision results
46class BT_CONTACT
47{
48public:
49    btVector3 m_point;
50    btVector3 m_normal;
51    btScalar m_depth;//Positive value indicates interpenetration
52    btScalar m_distance;//Padding not for use
53    int m_feature1;//Face number
54    int m_feature2;//Face number
55public:
56    BT_CONTACT()
57    {
58    }
59
60    BT_CONTACT(const BT_CONTACT & contact):
61                                m_point(contact.m_point),
62                                m_normal(contact.m_normal),
63                                m_depth(contact.m_depth),
64                                m_feature1(contact.m_feature1),
65                                m_feature2(contact.m_feature2)
66    {
67    }
68
69    BT_CONTACT(const btVector3 &point,const btVector3 & normal,
70                                btScalar depth, int feature1, int feature2):
71                                m_point(point),
72                                m_normal(normal),
73                                m_depth(depth),
74                                m_feature1(feature1),
75                                m_feature2(feature2)
76    {
77    }
78
79        //! Calcs key for coord classification
80    SIMD_FORCE_INLINE unsigned int calc_key_contact() const
81    {
82        int _coords[] = {
83                (int)(m_point[0]*1000.0f+1.0f),
84                (int)(m_point[1]*1333.0f),
85                (int)(m_point[2]*2133.0f+3.0f)};
86                unsigned int _hash=0;
87                unsigned int *_uitmp = (unsigned int *)(&_coords[0]);
88                _hash = *_uitmp;
89                _uitmp++;
90                _hash += (*_uitmp)<<4;
91                _uitmp++;
92                _hash += (*_uitmp)<<8;
93                return _hash;
94    }
95
96    SIMD_FORCE_INLINE void interpolate_normals( btVector3 * normals,int normal_count)
97    {
98        btVector3 vec_sum(m_normal);
99                for(int i=0;i<normal_count;i++)
100                {
101                        vec_sum += normals[i];
102                }
103
104                btScalar vec_sum_len = vec_sum.length2();
105                if(vec_sum_len <CONTACT_DIFF_EPSILON) return;
106
107                //GIM_INV_SQRT(vec_sum_len,vec_sum_len); // 1/sqrt(vec_sum_len)
108
109                m_normal = vec_sum/btSqrt(vec_sum_len);
110    }
111
112};
113
114
115class btContactArray:public btAlignedObjectArray<BT_CONTACT>
116{
117public:
118        btContactArray()
119        {
120                reserve(64);
121        }
122
123        SIMD_FORCE_INLINE void push_contact(
124                const btVector3 &point,const btVector3 & normal,
125                btScalar depth, int feature1, int feature2)
126        {
127                push_back( BT_CONTACT(point,normal,depth,feature1,feature2) );
128        }
129
130        SIMD_FORCE_INLINE void push_triangle_contacts(
131                const BT_TRIANGLE_CONTACT & tricontact,
132                int feature1,int feature2)
133        {
134                for(int i = 0;i<tricontact.m_point_count ;i++ )
135                {
136                        push_contact(
137                                tricontact.m_points[i],
138                                tricontact.m_separating_normal,
139                                tricontact.m_penetration_depth,feature1,feature2);
140                }
141        }
142
143        void merge_contacts(const btContactArray & contacts, bool normal_contact_average = true);
144
145        void merge_contacts_unique(const btContactArray & contacts);
146};
147
148//! @}
149#endif // GIM_CONTACT_H_INCLUDED
Note: See TracBrowser for help on using the repository browser.