//-------------------------------------------------------------------------------------------------- void q3Box::Render( const q3Transform& tx, bool awake, q3Render* render ) const { q3Transform world = q3Mul( tx, local ); q3Vec3 vertices[ 8 ] = { q3Vec3( -e.x, -e.y, -e.z ), q3Vec3( -e.x, -e.y, e.z ), q3Vec3( -e.x, e.y, -e.z ), q3Vec3( -e.x, e.y, e.z ), q3Vec3( e.x, -e.y, -e.z ), q3Vec3( e.x, -e.y, e.z ), q3Vec3( e.x, e.y, -e.z ), q3Vec3( e.x, e.y, e.z ) }; for ( i32 i = 0; i < 36; i += 3 ) { q3Vec3 a = q3Mul( world, vertices[ kBoxIndices[ i ] ] ); q3Vec3 b = q3Mul( world, vertices[ kBoxIndices[ i + 1 ] ] ); q3Vec3 c = q3Mul( world, vertices[ kBoxIndices[ i + 2 ] ] ); q3Vec3 n = q3Normalize( q3Cross( b - a, c - a ) ); //render->SetPenColor( 0.2f, 0.4f, 0.7f, 0.5f ); //render->SetPenPosition( a.x, a.y, a.z ); //render->Line( b.x, b.y, b.z ); //render->Line( c.x, c.y, c.z ); //render->Line( a.x, a.y, a.z ); render->SetTriNormal( n.x, n.y, n.z ); render->Triangle( a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z ); } }
//-------------------------------------------------------------------------------------------------- void q3Box::ComputeAABB( const q3Transform& tx, q3AABB* aabb ) const { q3Transform world = q3Mul( tx, local ); q3Vec3 v[ 8 ] = { q3Vec3( -e.x, -e.y, -e.z ), q3Vec3( -e.x, -e.y, e.z ), q3Vec3( -e.x, e.y, -e.z ), q3Vec3( -e.x, e.y, e.z ), q3Vec3( e.x, -e.y, -e.z ), q3Vec3( e.x, -e.y, e.z ), q3Vec3( e.x, e.y, -e.z ), q3Vec3( e.x, e.y, e.z ) }; for ( i32 i = 0; i < 8; ++i ) v[ i ] = q3Mul( world, v[ i ] ); q3Vec3 min( Q3_R32_MAX, Q3_R32_MAX, Q3_R32_MAX ); q3Vec3 max( -Q3_R32_MAX, -Q3_R32_MAX, -Q3_R32_MAX ); for ( i32 i = 0; i < 8; ++i ) { min = q3Min( min, v[ i ] ); max = q3Max( max, v[ i ] ); } aabb->min = min; aabb->max = max; }
bool GrabberRayCastCallBack::ReportShape(q3Box * b){ if (b->body->IsStatic()){ //std::cout << "Not grabbing static object" << std::endl; return false; } else{ // Save the body mGrabber.mObject = b->body; /* std::cout << "Grabbing object which is:" << "Static? " << mGrabber.mObject->IsStatic() << std::endl << "Kinematic? " << mGrabber.mObject->IsKinematic() << std::endl << "Dynamic? " << mGrabber.mObject->IsDynamic() << std::endl; */ // Retrieve the point of contact q3Vec3 worldPoint = mData.start + (mData.dir * mData.toi); // Save the point of contact in model space mGrabber.mPoint = b->body->GetLocalPoint(worldPoint); mGrabber.mIntegralError = q3Vec3( 0, 0, 0 ); // TODO could for example return false if you grab a bullet or something else not grabbable return true; } }
BulletModel::BulletModel(glm::vec3 dir) : Lib3dsModel(), mAge(0) { SetPhysicsType(Model::Dynamic); SetScaling(glm::vec3(0.5, 0.5, 0.5)); mDirection = glm::normalize(dir); float verticalAngle = -glm::asin(mDirection.y); // approximately good float horizontalAngle = -((-3.14159 / 2.0) + glm::atan(mDirection.z / mDirection.x)); // ATAN2 if (mDirection.x < 0){ if (mDirection.z >= 0){ horizontalAngle -= 3.14159; } else{ horizontalAngle += 3.14159; } } //std::cout << "Bullet model vAngle: " << verticalAngle << " hAngle " << horizontalAngle << " x: " << direction.x << " z: " << direction.z << std::endl; q3Quaternion rot_vert( q3Vec3(1,0,0), verticalAngle); q3Quaternion rot_horiz(q3Vec3(0,1,0), horizontalAngle); mRotation = rot_horiz * rot_vert; mScaling = glm::vec3(0.01, 0.01, 0.01); mMeshes.insert(13); mFileName = "../Assets/Objects/bombs.3ds"; LoadModel(); //mMaxAge = 90; // 90 second exipr date }
void Demo( ) { scene.SetIterations( 20 ); scene.SetAllowSleep( true ); scene.RemoveAllBodies( ); //seed = 23; //printf( "seed: %d\n", seed ); srand( seed++ ); // Create the floor q3BodyDef bodyDef; //bodyDef.axis.Set( q3RandomFloat( -1.0f, 1.0f ), q3RandomFloat( -1.0f, 1.0f ), q3RandomFloat( -1.0f, 1.0f ) ); //bodyDef.angle = q3PI * q3RandomFloat( -1.0f, 1.0f ); q3Body* body = scene.CreateBody( bodyDef ); q3BoxDef boxDef; boxDef.SetRestitution( 0 ); q3Transform tx; q3Identity( tx ); boxDef.Set( tx, q3Vec3( 50.0f, 1.0f, 50.0f ) ); body->AddBox( boxDef ); // Create boxes for ( i32 i = 0; i < 10; ++i ) { bodyDef.position.Set( 0.0f, 1.2f * (i + 1), -0.0f ); //bodyDef.axis.Set( 0.0f, 1.0f, 0.0f ); //bodyDef.angle = q3PI * q3RandomFloat( -1.0f, 1.0f ); //bodyDef.angularVelocity.Set( 3.0f, 3.0f, 3.0f ); //bodyDef.linearVelocity.Set( 2.0f, 0.0f, 0.0f ); bodyDef.bodyType = eDynamicBody; body = scene.CreateBody( bodyDef ); boxDef.Set( tx, q3Vec3( 1.0f, 1.0f, 1.0f ) ); body->AddBox( boxDef ); } }
void Grabber::ApplyForce(q3Vec3 & lookAt, q3Vec3 & from){ assert(HasObject()); #if GRABBER_USE_FORCE q3Vec3 objectPoint = mObject->GetWorldPoint(mPoint); q3Vec3 force = (from + (lookAt * mCallback.mData.t)) - objectPoint; // Also integrate the error over time mIntegralError += force * 0.1; r32 distance = q3Distance({0,0,0},force); q3Vec3 velocity = mObject->GetLinearVelocity(); if (q3Dot(velocity, force) < 0.0f){ // If the object is moving away from us, damp its velocity mObject->SetLinearVelocity(velocity * 0.95f); } // Always pull it very hard toward us force *= distance * distance; force += mIntegralError; mObject->ApplyForceAtWorldPoint(force, objectPoint); #else // THIS BUGS WITH STATIC OBJECTS auto tx = mObject->GetTransform(); tx.position = (from + (lookAt * mCallback.mData.t)); mObject->SetTransform(tx.position); auto v = mObject->GetLinearVelocity(); mObject->SetLinearVelocity(q3Vec3( 0, 0, 0 )); #endif }
//-------------------------------------------------------------------------------------------------- const q3Vec3 operator*( r32 f, const q3Vec3& rhs ) { return q3Vec3( rhs.x * f, rhs.y * f, rhs.z * f ); }
//-------------------------------------------------------------------------------------------------- const q3Vec3 q3Vec3::operator/( r32 f ) const { return q3Vec3( x / f, y / f, z / f ); }
//-------------------------------------------------------------------------------------------------- const q3Vec3 q3Vec3::operator*( r32 f ) const { return q3Vec3( x * f, y * f, z * f ); }
//-------------------------------------------------------------------------------------------------- const q3Vec3 q3Vec3::operator-( const q3Vec3& rhs ) const { return q3Vec3( x - rhs.x, y - rhs.y, z - rhs.z ); }
//-------------------------------------------------------------------------------------------------- const q3Vec3 q3Vec3::operator+( const q3Vec3& rhs ) const { return q3Vec3( x + rhs.x, y + rhs.y, z + rhs.z ); }
//-------------------------------------------------------------------------------------------------- q3Vec3 q3Vec3::operator-( void ) const { return q3Vec3( -x, -y, -z ); }