Пример #1
0
	void Ball::createVertices(Vector3 color, float size)
	{
		// Divide circle
		// vertex 0 is center
		// create indices to be a triangle fan
		_size = size;
		int vertexAmount = 12;
		_vertexAmount = vertexAmount + 1;
		_verticesArray = new Vertex[_vertexAmount];


		_verticesArray[0].setPos( 0.0f, 0.0f, 0.0f);
		_verticesArray[0].setColor(color);


		float x;
		float y;
		float nx;
		float ny;
		float cs;
		float sn;

		float cAngle;

		float pi = M_PI;
		float angle = (pi * 2.0f) / (float)(vertexAmount);
		cAngle = 0.0f;
		Vector3 vertexPos(0.0f, size, 0.0f);


		for(int i = 1; i < _vertexAmount; i++)
		{
			cs = cos(cAngle);
			sn = sin(cAngle);
			x = vertexPos.x;
			y = vertexPos.y;
			nx = x * cs - y * sn;
			ny = x * sn + y * cs;
			_verticesArray[i].setPos(nx, ny, 0.0f);
			_verticesArray[i].setColor(color.x, color.y, color.z);
			cAngle += angle;
		}

		for(int i = 0; i < _vertexAmount; i++)
		{

			LOG("Ball Vertex %d at %f : %f", i, _verticesArray[i]._x, _verticesArray[i]._y);
			//LOG("Ball Vertex %d is %f : %f : %f", i,_verticesArray[i]._r, _verticesArray[i]._g, _verticesArray[i]._b);
		}


		_dataArray = new float[7 * _vertexAmount];
		for(int i = 0; i < _vertexAmount; i++)
		{
			for(int d = 0; d < 7; d++)
			{
				_dataArray[i*7 +0] = _verticesArray[i]._x;
				_dataArray[i*7 +1] = _verticesArray[i]._y;
				_dataArray[i*7 +2] = _verticesArray[i]._z;
				_dataArray[i*7 +3] = _verticesArray[i]._r;
				_dataArray[i*7 +4] = _verticesArray[i]._g;
				_dataArray[i*7 +5] = _verticesArray[i]._b;
				_dataArray[i*7 +6] = _verticesArray[i]._a;

			}

		}

		for(int i = 0; i < _vertexAmount; i++)
			{
				for(int d = 0; d < 7; d++)
				{
						LOG("Ball pushing vertex data to dataArray %d:%d = %f", i,d, _dataArray[d]);
				}
			}

		// Create indices
		// 1 triangle from each vertex
		_indicesAmount = 3 * vertexAmount;
		_indicesArray = new unsigned short[_indicesAmount];
		int triangles = vertexAmount;
		unsigned short indice = 1;

		for(int i = 0; i < triangles; i++)
		{
			_indicesArray[i * 3] = 0; // Middlepoint
			_indicesArray[(i*3)+1] = indice;
			indice++;
			if(indice > vertexAmount)
			{
				indice = 1;
			}
			_indicesArray[(i*3)+2] = indice;
			// No increase, share the vertex
		}

		LOG("Ball has %d indices", _indicesAmount);
		for(int i = 0; i < _indicesAmount; i++)
		{
			LOG("Ball indice at %d", _indicesArray[i]);
		}

	}
Пример #2
0
unsigned jfCollisionDetector_x86::boxAndHalfSpace(const jfCollisionBox& box,
													const jfCollisionPlane& plane,
													jfCollisionData* data
													) const
{
        /*
	// Make sure we have contacts
    if (! data->hasMoreContacts())
	{
		return 0;
	}
*/
	jfIntersectionTester_x86 intersectionTester;

    // Check for intersection
    if (! intersectionTester.boxAndHalfSpace(box, plane))
    {
        return 0;
    }

    // We have an intersection, so find the intersection points. We can make
    // do with only checking vertices. If the box is resting on a plane
    // or on an edge, it will be reported as four or two contact points.

    // Go through each combination of + and - for each half-size
/*    static jfReal mults[8][3] = {{1,1,1},
                                    {-1,1,1},
                                    {1,-1,1},
                                    {-1,-1,1},
                                    {1,1,-1},
                                    {-1,1,-1},
                                    {1,-1,-1},
                                    {-1,-1,-1}};
                                    */
    static jfReal mults[8][3] = { {-1,-1,-1},
                                    {-1,-1,1},
                                    {-1,1,-1},
                                    {-1,1,1},
                                    {1,-1,-1},
                                    {1,-1,1},
                                    {1,1,-1},
                                    {1,1,1} };

    jfMatrix4_x86 boxTransformMatrix;
    jfVector3_x86 boxHalfSize;
    jfVector3_x86 planeDirection;

    //For efficiency cache these values
    box.getHalfSize(&boxHalfSize);
    plane.getDirection(&planeDirection);
    box.getTransformMatrix(&boxTransformMatrix);

    unsigned contactsUsed = 0;
    for (unsigned i = 0; i < 8; i++) {
		// Calculate the position of each vertex
        jfVector3_x86 vertexPos(mults[i][0],
                                mults[i][1],
                                mults[i][2]);

        vertexPos.componentProductUpdate(boxHalfSize);
		boxTransformMatrix.transform(vertexPos, &vertexPos);

        // Calculate the distance from the plane
        jfReal vertexDistance = vertexPos.dotProduct(planeDirection);

        // Compare this to the plane's distance
        if (vertexDistance <= (plane.getOffset() + data->getTolerance()))
        {
            jfContact_x86 contact;
            // Create the contact data.
            // The contact point is halfway between the vertex and the
            // plane - we multiply the direction by half the separation
            // distance and add the vertex location.
            jfVector3_x86 contactPoint = planeDirection;
			contactPoint *= (vertexDistance-plane.getOffset());
			contactPoint += vertexPos;
            contact.setContactPoint(contactPoint);
            contact.setContactNormal(planeDirection);
            contact.setPenetration(plane.getOffset() - vertexDistance);
            cout<<"vertexDistance is : "<<vertexDistance<<endl;
            // Write the appropriate data
            contact.setBodyData(box.getBody(), NULL, data->getFriction(), data->getRestitution());

            // Move onto the next contact
			data->addContact(contact);
            contactsUsed++;
/*
            if (! data->hasMoreContacts())
			{
				return contactsUsed;
			}*/
		}
    }
    return contactsUsed;
}
Пример #3
0
unsigned CollisionDetector::boxAndHalfSpace(
    const CollisionBox &box,
    const CollisionPlane &plane,
    CollisionData *data
    )
{
    // Make sure we have contacts
    if (data->contactsLeft <= 0) return 0;

    // Check for intersection
    if (!IntersectionTests::boxAndHalfSpace(box, plane))
    {
        return 0;
    }

    // We have an intersection, so find the intersection points. We can make
    // do with only checking vertices. If the box is resting on a plane
    // or on an edge, it will be reported as four or two contact points.

    // Go through each combination of + and - for each half-size
    static real mults[8][3] = {{1,1,1},{-1,1,1},{1,-1,1},{-1,-1,1},
                               {1,1,-1},{-1,1,-1},{1,-1,-1},{-1,-1,-1}};

    Contact* contact = data->contacts;
    unsigned contactsUsed = 0;
    for (unsigned i = 0; i < 8; i++) {

        // Calculate the position of each vertex
        Vector3 vertexPos(mults[i][0], mults[i][1], mults[i][2]);
        vertexPos.componentProductUpdate(box.halfSize);
        vertexPos = box.transform.transform(vertexPos);

        // Calculate the distance from the plane
        real vertexDistance = vertexPos * plane.direction;

        // Compare this to the plane's distance
        if (vertexDistance <= plane.offset)
        {
            // Create the contact data.

            // The contact point is halfway between the vertex and the
            // plane - we multiply the direction by half the separation
            // distance and add the vertex location.
            contact->contactPoint  = plane.direction;
            contact->contactPoint *= (vertexDistance-plane.offset);
            contact->contactPoint += vertexPos;
            contact->contactNormal = plane.direction;
            contact->penetration = plane.offset - vertexDistance;

            // Write the appropriate data
            contact->setBodyData(box.body, NULL,
                data->friction, data->restitution);

            // Move onto the next contact
            contact++;
            contactsUsed++;
            if (contactsUsed == (unsigned)data->contactsLeft) return contactsUsed;
        }
    }

	data->addContacts(contactsUsed);
    return contactsUsed;
}