1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
#include <iostream>
#include <osg/Geometry>
#include <osg/ShapeDrawable>
#include <osg/PolygonMode>
#include <osgDB/ReadFile>
#include <osg/PositionAttitudeTransform>
#include <osg/io_utils>
#include "movableObject.h"
#include "jgt.h"
using namespace std;
MovableObject:: MovableObject(osg::Group* mod,
osg::PositionAttitudeTransform * pat)
: model(mod),
pat(pat),
speed(5.0),
x_facing(0.0), //Set the initial facings to be 0 (default)
y_facing(0.0),
z_facing(0.0) {
copyTriangles();
}
void MovableObject::setDirtyTrue() {
dirty = true;
computeLocalToWorldMatrix();
}
void MovableObject::computeLocalToWorldMatrix()
{
worldTransMatrix = osg::Matrix::rotate(pat->getAttitude()) * osg::Matrix::translate(pat->getPosition());
}
//**********************************************
//je recupére dans cette fonction la geometrie
//*********************************************
void MovableObject::copyTriangles(const osg::Geometry* g) //comme argument une geometrie
{
const GLfloat* v = static_cast<const GLfloat*>(g->getVertexArray()->getDataPointer());
unsigned int offset = vertices.size();
//cout<<"offset"<<offset<<endl;
unsigned int numVertices = g->getVertexArray()->getNumElements();
unsigned int numIndices;
for (unsigned int i=0; i<numVertices; ++i)
{
vertices.push_back(osg::Vec3(v[i*3], v[i*3+1], v[i*3+2]));
}
const osg::PrimitiveSet* ps;
unsigned int v0,v1,v2;
for (unsigned int i=0; i<g->getNumPrimitiveSets(); ++i)
{
ps = g->getPrimitiveSet(i);
if (ps->getMode() == GL_TRIANGLE_STRIP)
{
v0 = ps->index(0);
v1 = ps->index(1);
numIndices = ps->getNumIndices();
for (unsigned int j=2; j<numIndices; ++j) {
v2 = ps->index(j);
faces.push_back(osg::Vec3s(v0+offset, v1+offset, v2+offset));//je constitu la face dun triangle grace o 3points vetex
v0 = v1;
v1 = v2;
}
} else if (ps->getMode() == GL_TRIANGLES) {
numIndices = ps->getNumIndices();
for (unsigned int j=0; j<numIndices; j+=3) {
v0 = ps->index(j);
v1 = ps->index(j+1);
v2 = ps->index(j+2);
faces.push_back(osg::Vec3s(v0+offset, v1+offset, v2+offset));
}
} else {
// Woops, only supporting triangles and triangle strips
throw std::string("Not a triangle in copyTriangles");
}
}
}
//************************************************************************
void MovableObject::copyTriangles() {
const osg::Geode* g;
const osg::Geometry* d;
vertices.clear();
faces.clear();
for (unsigned int i=0; i<model->getNumChildren(); ++i)
{
g = dynamic_cast<const osg::Geode*>(model->getChild(i));
if (!g) continue;
for (unsigned int j=0; j<g->getNumDrawables(); ++j) {
d = dynamic_cast<const osg::Geometry*>(g->getDrawable(j));
if (d)
{
copyTriangles(d);
}
}
}
}
void MovableObject::updateVertices() {
if (dirty) {
copyTriangles();//j'ai recupéré la geometrie
for (unsigned int i = 0; i < vertices.size(); ++i)
{
vertices[i] = vertices[i] * worldTransMatrix;
}
dirty = false;
}
}
//la collision
bool MovableObject::collidesWith(MovableObject* obj)
{
float* v0;
float* v1;
float* v2;
float* u0;
float* u1;
float* u2;
//englobe l'objet dans une sphere ,je voudrais que mon objet soit diviser en plusieurs petite sphere commet je fais ?
const osg::BoundingSphere& bs1 = pat->getBound();//l'objet qui bouge
const osg::BoundingSphere& bs2 = obj->getPat().getBound();
if (!bs1.intersects(bs2))return false;
setDirtyTrue();
unsigned int numFaces, objNumFaces;
// This whole process could be optimized for more complicated objects by
// checking bounding boxes of individual geodes
// Has not been tested for objects w/ more than 1 geode and drawable
// Do the more precise triangle/triangle collision detection
numFaces = faces.size();
objNumFaces = obj->faces.size();
updateVertices();
obj->updateVertices();
for (unsigned int i=0; i<numFaces; ++i)
{
v0 = vertices[faces[i][0]].ptr();
v1 = vertices[faces[i][1]].ptr();
v2 = vertices[faces[i][2]].ptr();
for (unsigned int j=0; j<objNumFaces; j++)
//U=> c'est les vertex de l'objet statique
{
u0 = obj->vertices[obj->faces[j][0]].ptr();
u1 = obj->vertices[obj->faces[j][1]].ptr();
u2 = obj->vertices[obj->faces[j][2]].ptr();
if ( NoDivTriTriIsect( v0, v1, v2,u0, u1, u2))
{
i=objNumFaces;
j=objNumFaces;
return true;
}
}
}
return false;
}
//les touche du clavier |
Partager