Want more features on Pastebin? Sign Up, it's FREE!
Guest

whole code

By: Krisboss on Feb 13th, 2013  |  syntax: C++  |  size: 43.28 KB  |  views: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // Copyright (C) 2012  Daniele Giannetti
  3. //
  4. // This code is provided to evaluate some features of the Free binary-only
  5. // Havok Physics & Animation distribution. By no means we guarantee this small
  6. // project to be bug-free. This code implements a simple 3D physics-based game
  7. // using Havok Physics for real time physical simulation, and HKG to provide
  8. // some visual feedback to the user.
  9. ////////////////////////////////////////////////////////////////////////////////
  10. #include <stdlib.h>
  11. //#include <GL/freeglut.h>
  12. #include <gl/glut.h>
  13. //#include <game.h>
  14. #include <string>
  15.  
  16.  
  17.  
  18. #include <Common/Base/hkBase.h>
  19. #include <Common/Base/Memory/System/Util/hkMemoryInitUtil.h>
  20. #include <Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h>
  21. // Physics includes
  22. #include <Physics/Collide/hkpCollide.h>
  23. #include <Physics/Collide/Shape/Convex/Sphere/hkpSphereShape.h>
  24. #include <Physics/Collide/Shape/Convex/Box/hkpBoxShape.h>
  25. #include <Physics/Collide/Shape/Compound/Collection/List/hkpListShape.h>
  26. #include <Physics/Collide/Shape/Convex/ConvexTranslate/hkpConvexTranslateShape.h>
  27. #include <Physics/Collide/Shape/Misc/Bv/hkpBvShape.h>
  28. #include <Physics/Collide/Shape/Misc/PhantomCallback/hkpPhantomCallbackShape.h>
  29. #include <Physics/Collide/Dispatch/hkpAgentRegisterUtil.h>
  30. #include <Physics/Dynamics/hkpDynamics.h>
  31. #include <Physics/Dynamics/World/hkpWorld.h>
  32. #include <Physics/Dynamics/Entity/hkpRigidBody.h>
  33. #include <Physics/Dynamics/Constraint/ConstraintKit/hkpGenericConstraintData.h>
  34. #include <Physics/Dynamics/Constraint/ConstraintKit/hkpConstraintConstructionKit.h>
  35. #include <Physics/Dynamics/Constraint/hkpConstraintInstance.h>
  36. #include <Physics/Utilities/Dynamics/Inertia/hkpInertiaTensorComputer.h>
  37. #ifdef DEBUG
  38.         // Visual Debugger includes
  39.         #include <Common/Visualize/hkVisualDebugger.h>
  40.         #include <Physics/Utilities/VisualDebugger/hkpPhysicsContext.h>
  41. #endif
  42.  
  43.  
  44. #include <iostream>
  45.  
  46.         hkpWorld* m_world;
  47.  
  48.  
  49.  
  50.  
  51.  
  52. #include <Physics/Vehicle/hkpVehicleInstance.h>
  53. #include <Physics/Vehicle/Camera/hkp1dAngularFollowCam.h>
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. #include <Physics/Vehicle/DriverInput/Default/hkpVehicleDefaultAnalogDriverInput.h>
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88. #include <Physics/Vehicle/hkpVehicleInstance.h>
  89.  
  90. #include <Physics/Vehicle/AeroDynamics/Default/hkpVehicleDefaultAerodynamics.h>
  91. #include <Physics/Vehicle/DriverInput/Default/hkpVehicleDefaultAnalogDriverInput.h>
  92. #include <Physics/Vehicle/Brake/Default/hkpVehicleDefaultBrake.h>
  93. #include <Physics/Vehicle/Engine/Default/hkpVehicleDefaultEngine.h>
  94. #include <Physics/Vehicle/VelocityDamper/Default/hkpVehicleDefaultVelocityDamper.h>
  95. #include <Physics/Vehicle/Steering/Default/hkpVehicleDefaultSteering.h>
  96. #include <Physics/Vehicle/Suspension/Default/hkpVehicleDefaultSuspension.h>
  97. #include <Physics/Vehicle/Transmission/Default/hkpVehicleDefaultTransmission.h>
  98. #include <Physics/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
  99. #include <Physics/Vehicle/TyreMarks/hkpTyremarksInfo.h>
  100.  
  101.  
  102.         /// This class just holds ALL the vehicle parameters (hardcoded) for an untuned, four
  103.        
  104.  
  105.        
  106.  
  107.  
  108. #include <Physics/Vehicle/hkpVehicleInstance.h>
  109.  
  110. #include <Physics/Vehicle/AeroDynamics/Default/hkpVehicleDefaultAerodynamics.h>
  111. #include <Physics/Vehicle/DriverInput/Default/hkpVehicleDefaultAnalogDriverInput.h>
  112. #include <Physics/Vehicle/Brake/Default/hkpVehicleDefaultBrake.h>
  113. #include <Physics/Vehicle/Engine/Default/hkpVehicleDefaultEngine.h>
  114. #include <Physics/Vehicle/VelocityDamper/Default/hkpVehicleDefaultVelocityDamper.h>
  115. #include <Physics/Vehicle/Steering/Default/hkpVehicleDefaultSteering.h>
  116. #include <Physics/Vehicle/Suspension/Default/hkpVehicleDefaultSuspension.h>
  117. #include <Physics/Vehicle/Transmission/Default/hkpVehicleDefaultTransmission.h>
  118. #include <Physics/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
  119. #include <Physics/Vehicle/TyreMarks/hkpTyremarksInfo.h>
  120.  
  121.         /// This class just holds ALL the vehicle parameters (hardcoded) for an untuned, four
  122.         /// wheeled vehicle and facilitates construction via blueprints on a single call to
  123.         /// buildVehicle().
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. #include <Common\Base\Object\hkBaseObject.h>
  137. #include <Common\Base\Object\hkReferencedObject.h>
  138. #include <Common\Base\Types\Geometry\hkStridedVertices.h>
  139. #include <Physics\Collide\Shape\Convex\ConvexVertices\hkpConvexVerticesShape.h>
  140.  
  141.  
  142.  
  143.  
  144.  
  145. #ifndef FRICTION_MAP_VEHICLE_RAYCAST_COLLIDE_H
  146. #define FRICTION_MAP_VEHICLE_RAYCAST_COLLIDE_H
  147.  
  148. #include <Physics/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
  149. #include <Physics/Vehicle/hkpVehicleInstance.h>
  150. #include <Physics/Collide/Query/CastUtil/hkpWorldRayCastOutput.h>
  151.  
  152. extern const class hkClass FrictionMapVehicleRaycastWheelCollideClass;
  153.  
  154. #define VEHICLE_RAYCAST_ICY_FRICTION_PROPERTY 67341
  155.  
  156.         // This is the class which implements the calcSingleWheelGroundFriction, allowing you to override the
  157.         // ground friction calculation for raycast vehicles. Here, as a very simple example, we overwrite the friction to be
  158.         // 0.01 for all bodies (upon which we are driving) which have been tagged with a VEHICLE_RAYCAST_ICY_FRICTION_PROPERTY
  159.         // user property, otherwise we leave the friction as it is.
  160.         // In general of course, the friction value need not be constant over the body (or time), and any function can be used
  161.         // here, for example a lookup into your own full 2D 'friction map'.
  162.         // Other ways to store/retrieve friction values would be via:
  163.         //      hkpShape user data
  164.         //      A 'float' hkpProperty stored with a the rigid body
  165.         //      hkMeshMaterials for hkMeshShapes
  166.         //
  167.        
  168. class FrictionMapVehicleRaycastWheelCollide : public hkpVehicleRayCastWheelCollide
  169. {
  170.         public:
  171.                 HK_DECLARE_CLASS_ALLOCATOR(HK_MEMORY_CLASS_BASE);
  172.  
  173.         private:
  174.                 virtual void wheelCollideCallback( const hkpVehicleInstance* vehicle, hkUint8 wheelIndex, CollisionDetectionWheelOutput& cdInfo )
  175.                 {
  176.                         hkpRigidBody* rb = cdInfo.m_contactBody;
  177.                         if ( rb )
  178.                         {
  179.                                 if ( rb->hasProperty(VEHICLE_RAYCAST_ICY_FRICTION_PROPERTY) )
  180.                                 {
  181.                                         cdInfo.m_contactFriction = 0.01f;
  182.                                 }
  183.                         }
  184.                 }
  185.  
  186.         // Serialization.
  187.  
  188.         public:
  189.                 // By adding HK_DECLARE_REFLECTION, we enable objects of this class to be serialized.
  190.                 // However, the class does need to be registered with the type registry. The following code can be used:
  191.                 // hkBuiltinTypeRegistry::getInstance().addType( &FrictionMapVehicleRaycastWheelCollideTypeInfo, &FrictionMapVehicleRaycastWheelCollideClass );
  192.                 HK_DECLARE_REFLECTION();
  193.  
  194.                 FrictionMapVehicleRaycastWheelCollide () { }
  195.  
  196.                 FrictionMapVehicleRaycastWheelCollide ( hkFinishLoadedObjectFlag f ) : hkpVehicleRayCastWheelCollide( f ) { }
  197. };
  198.  
  199. #endif // FRICTION_MAP_VEHICLE_RAYCAST_COLLIDE_H
  200.  
  201.  
  202.  
  203. #ifndef HK_VEHICLE_SETUP_H
  204. #define HK_VEHICLE_SETUP_H
  205.  
  206. #include <Physics/Vehicle/hkpVehicleInstance.h>
  207.  
  208. #include <Physics/Vehicle/AeroDynamics/Default/hkpVehicleDefaultAerodynamics.h>
  209. #include <Physics/Vehicle/DriverInput/Default/hkpVehicleDefaultAnalogDriverInput.h>
  210. #include <Physics/Vehicle/Brake/Default/hkpVehicleDefaultBrake.h>
  211. #include <Physics/Vehicle/Engine/Default/hkpVehicleDefaultEngine.h>
  212. #include <Physics/Vehicle/VelocityDamper/Default/hkpVehicleDefaultVelocityDamper.h>
  213. #include <Physics/Vehicle/Steering/Default/hkpVehicleDefaultSteering.h>
  214. #include <Physics/Vehicle/Suspension/Default/hkpVehicleDefaultSuspension.h>
  215. #include <Physics/Vehicle/Transmission/Default/hkpVehicleDefaultTransmission.h>
  216. #include <Physics/Vehicle/WheelCollide/RayCast/hkpVehicleRayCastWheelCollide.h>
  217. #include <Physics/Vehicle/TyreMarks/hkpTyremarksInfo.h>
  218.  
  219.         /// This class just holds ALL the vehicle parameters (hardcoded) for an untuned, four
  220.         /// wheeled vehicle and facilitates construction via blueprints on a single call to
  221.         /// buildVehicle().
  222. class VehicleSetup : public hkReferencedObject
  223. {
  224.         public:
  225.  
  226.                 HK_DECLARE_CLASS_ALLOCATOR( HK_MEMORY_CLASS_VEHICLE );
  227.  
  228.                 VehicleSetup(){}
  229.  
  230.                 virtual void buildVehicle( const hkpWorld* world, hkpVehicleInstance& vehicle );
  231.  
  232.         public:
  233.  
  234.                 virtual void setupVehicleData( const hkpWorld* world, hkpVehicleData& data );
  235.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultAnalogDriverInput& driverInput );
  236.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultSteering& steering );
  237.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultEngine& engine );
  238.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultTransmission& transmission );
  239.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultBrake& brake );
  240.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultSuspension& suspension );
  241.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultAerodynamics& aerodynamics );
  242.                 virtual void setupComponent( const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper );
  243.  
  244.                 virtual void setupWheelCollide( const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide );
  245.                 virtual void setupTyremarks( const hkpVehicleData& data, hkpTyremarksInfo& tyremarkscontroller );
  246. };
  247.  
  248. #endif // HK_VEHICLE_SETUP_H
  249.  
  250.  
  251.  
  252.  
  253.  
  254. void VehicleSetup::buildVehicle( const hkpWorld* world, hkpVehicleInstance& vehicle )
  255. {
  256.         //
  257.         // All memory allocations are made here.
  258.         //
  259.  
  260.         vehicle.m_data                          = new hkpVehicleData;
  261.         vehicle.m_driverInput           = new hkpVehicleDefaultAnalogDriverInput;
  262.         vehicle.m_steering                      = new hkpVehicleDefaultSteering;
  263.         vehicle.m_engine                        = new hkpVehicleDefaultEngine;
  264.         vehicle.m_transmission          = new hkpVehicleDefaultTransmission;
  265.         vehicle.m_brake                         = new hkpVehicleDefaultBrake;
  266.         vehicle.m_suspension            = new hkpVehicleDefaultSuspension;
  267.         vehicle.m_aerodynamics          = new hkpVehicleDefaultAerodynamics;
  268.         vehicle.m_velocityDamper        = new hkpVehicleDefaultVelocityDamper;
  269.  
  270.         // For illustrative purposes we use a custom hkpVehicleRayCastWheelCollide
  271.         // which implements varying 'ground' friction in a very simple way.
  272.         vehicle.m_wheelCollide          = new FrictionMapVehicleRaycastWheelCollide;
  273.  
  274.         setupVehicleData( world, *vehicle.m_data );
  275.  
  276.         // initialise the tyremarks controller with 128 tyremark points.
  277.         vehicle.m_tyreMarks             = new hkpTyremarksInfo( *vehicle.m_data, 128 );
  278.  
  279.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultAnalogDriverInput* >(vehicle.m_driverInput) );
  280.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultSteering*>(vehicle.m_steering));
  281.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultEngine*>(vehicle.m_engine) );
  282.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultTransmission*>(vehicle.m_transmission) );
  283.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultBrake*>(vehicle.m_brake) );
  284.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultSuspension*>(vehicle.m_suspension) );
  285.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultAerodynamics*>(vehicle.m_aerodynamics) );
  286.         setupComponent( *vehicle.m_data, *static_cast< hkpVehicleDefaultVelocityDamper*>(vehicle.m_velocityDamper) );
  287.  
  288.         setupWheelCollide( world, vehicle, *static_cast< hkpVehicleRayCastWheelCollide*>(vehicle.m_wheelCollide) );
  289.  
  290.         setupTyremarks( *vehicle.m_data, *static_cast< hkpTyremarksInfo*>(vehicle.m_tyreMarks) );
  291.  
  292.         //
  293.         // Check that all components are present.
  294.         //
  295.         HK_ASSERT(0x0, vehicle.m_data );
  296.         HK_ASSERT(0x7708674a,  vehicle.m_driverInput );
  297.         HK_ASSERT(0x5a324a2d,  vehicle.m_steering );
  298.         HK_ASSERT(0x7bcb2aff,  vehicle.m_engine );
  299.         HK_ASSERT(0x29bddb50,  vehicle.m_transmission );
  300.         HK_ASSERT(0x2b0323a2,  vehicle.m_brake );
  301.         HK_ASSERT(0x7a7ade23,  vehicle.m_suspension );
  302.         HK_ASSERT(0x6ec4d0ed,  vehicle.m_aerodynamics );
  303.         HK_ASSERT(0x67161206,  vehicle.m_wheelCollide );
  304.         HK_ASSERT(0x295015f1,  vehicle.m_tyreMarks );
  305.  
  306.         //
  307.         // Set up any variables that store cached data.
  308.         //
  309.  
  310.  
  311.         // Give driver input default values so that the vehicle (if this input is a default for non
  312.         // player cars) will drive, even if it is in circles!
  313.  
  314.         // Accelerate.
  315.         vehicle.m_deviceStatus = new hkpVehicleDriverInputAnalogStatus;
  316.         hkpVehicleDriverInputAnalogStatus* deviceStatus = (hkpVehicleDriverInputAnalogStatus*)vehicle.m_deviceStatus;
  317.         deviceStatus->m_positionY = -0.4f;
  318.  
  319.         // Turn.
  320.         deviceStatus->m_positionX = 0.3f;
  321.  
  322.         // Defaults
  323.         deviceStatus->m_handbrakeButtonPressed = false;
  324.         deviceStatus->m_reverseButtonPressed = false;
  325.  
  326.  
  327.         //
  328.         // Don't forget to call init! (This function is necessary to set up derived data)
  329.         //
  330.         vehicle.init();
  331. }
  332.  
  333. void VehicleSetup::setupVehicleData( const hkpWorld* world, hkpVehicleData& data )
  334. {
  335.         data.m_gravity = world->getGravity();
  336.  
  337.         //
  338.         // The vehicleData contains information about the chassis.
  339.         //
  340.  
  341.         // The coordinates of the chassis system, used for steering the vehicle.
  342.         //                                                                              up                                      forward                         right
  343.         data.m_chassisOrientation.setCols( hkVector4(0, 1, 0), hkVector4(1, 0, 0), hkVector4(0, 0, 1));
  344.  
  345.         data.m_frictionEqualizer = 0.5f;
  346.  
  347.  
  348.         // Inertia tensor for each axis is calculated by using :
  349.         // (1 / chassis_mass) * (torque(axis)Factor / chassisUnitInertia)
  350.         data.m_torqueRollFactor = 0.625f;
  351.         data.m_torquePitchFactor = 0.5f;
  352.         data.m_torqueYawFactor = 0.35f;
  353.  
  354.         data.m_chassisUnitInertiaYaw = 1.0f;
  355.         data.m_chassisUnitInertiaRoll = 1.0f;
  356.         data.m_chassisUnitInertiaPitch = 1.0f;
  357.  
  358.         // Adds or removes torque around the yaw axis
  359.         // based on the current steering angle.  This will
  360.         // affect steering.
  361.         data.m_extraTorqueFactor = -0.5f;
  362.         data.m_maxVelocityForPositionalFriction = 0.0f;
  363.  
  364.         //
  365.         // Wheel specifications
  366.         //
  367.         data.m_numWheels = 4;
  368.  
  369.         data.m_wheelParams.setSize( data.m_numWheels );
  370.  
  371.         data.m_wheelParams[0].m_axle = 0;
  372.         data.m_wheelParams[1].m_axle = 0;
  373.         data.m_wheelParams[2].m_axle = 1;
  374.         data.m_wheelParams[3].m_axle = 1;
  375.  
  376.         data.m_wheelParams[0].m_friction = 1.5f;
  377.         data.m_wheelParams[1].m_friction = 1.5f;
  378.         data.m_wheelParams[2].m_friction = 1.5f;
  379.         data.m_wheelParams[3].m_friction = 1.5f;
  380.  
  381.         data.m_wheelParams[0].m_slipAngle = 0.0f;
  382.         data.m_wheelParams[1].m_slipAngle = 0.0f;
  383.         data.m_wheelParams[2].m_slipAngle = 0.0f;
  384.         data.m_wheelParams[3].m_slipAngle = 0.0f;
  385.  
  386.         for ( int i = 0 ; i < data.m_numWheels ; i++ )
  387.         {
  388.                 // This value is also used to calculate the m_primaryTransmissionRatio.
  389.                 data.m_wheelParams[i].m_radius = 0.4f;
  390.                 data.m_wheelParams[i].m_width = 0.2f;
  391.                 data.m_wheelParams[i].m_mass = 10.0f;
  392.  
  393.                 data.m_wheelParams[i].m_viscosityFriction = 0.25f;
  394.                 data.m_wheelParams[i].m_maxFriction = 2.0f * data.m_wheelParams[i].m_friction;
  395.                 data.m_wheelParams[i].m_forceFeedbackMultiplier = 0.1f;
  396.                 data.m_wheelParams[i].m_maxContactBodyAcceleration = hkReal(data.m_gravity.length3()) * 2;
  397.         }
  398. }
  399.  
  400.  
  401. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultAnalogDriverInput& driverInput )
  402. {
  403.         // We also use an analog "driver input" class to help converting user input to vehicle behavior.
  404.  
  405.         driverInput.m_slopeChangePointX = 0.8f;
  406.         driverInput.m_initialSlope = 0.7f;
  407.         driverInput.m_deadZone = 0.0f;
  408.         driverInput.m_autoReverse = true;
  409. }
  410.  
  411. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultSteering& steering )
  412. {  
  413.         steering.m_doesWheelSteer.setSize( data.m_numWheels );
  414.  
  415.         // degrees
  416.         steering.m_maxSteeringAngle = 35 * ( HK_REAL_PI / 180 );
  417.  
  418.         // [mph/h] The steering angle decreases linearly
  419.         // based on your overall max speed of the vehicle.
  420.         steering.m_maxSpeedFullSteeringAngle = 70.0f * (1.605f / 3.6f);
  421.         steering.m_doesWheelSteer[0] = true;
  422.         steering.m_doesWheelSteer[1] = true;
  423.         steering.m_doesWheelSteer[2] = false;
  424.         steering.m_doesWheelSteer[3] = false;
  425. }
  426.  
  427. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultEngine& engine )
  428. {
  429.         engine.m_maxTorque = 500.0f;
  430.  
  431.         engine.m_minRPM = 1000.0f;
  432.         engine.m_optRPM = 5500.0f;
  433.  
  434.         // This value is also used to calculate the m_primaryTransmissionRatio.
  435.         engine.m_maxRPM = 7500.0f;
  436.  
  437.         engine.m_torqueFactorAtMinRPM = 0.8f;
  438.         engine.m_torqueFactorAtMaxRPM = 0.8f;
  439.  
  440.         engine.m_resistanceFactorAtMinRPM = 0.05f;
  441.         engine.m_resistanceFactorAtOptRPM = 0.1f;
  442.         engine.m_resistanceFactorAtMaxRPM = 0.3f;
  443. }
  444.  
  445. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultTransmission& transmission )
  446. {
  447.         int numGears = 4;
  448.  
  449.         transmission.m_gearsRatio.setSize( numGears );
  450.         transmission.m_wheelsTorqueRatio.setSize( data.m_numWheels );
  451.  
  452.         transmission.m_downshiftRPM = 3500.0f;
  453.         transmission.m_upshiftRPM = 6500.0f;
  454.  
  455.         transmission.m_clutchDelayTime = 0.0f;
  456.         transmission.m_reverseGearRatio = 1.0f;
  457.         transmission.m_gearsRatio[0] = 2.0f;
  458.         transmission.m_gearsRatio[1] = 1.5f;
  459.         transmission.m_gearsRatio[2] = 1.0f;
  460.         transmission.m_gearsRatio[3] = 0.75f;
  461.         transmission.m_wheelsTorqueRatio[0] = 0.2f;
  462.         transmission.m_wheelsTorqueRatio[1] = 0.2f;
  463.         transmission.m_wheelsTorqueRatio[2] = 0.3f;
  464.         transmission.m_wheelsTorqueRatio[3] = 0.3f;
  465.  
  466.         const hkReal vehicleTopSpeed = 130.0f;  
  467.         const hkReal wheelRadius = 0.4f;
  468.         const hkReal maxEngineRpm = 7500.0f;
  469.         transmission.m_primaryTransmissionRatio = hkpVehicleDefaultTransmission::calculatePrimaryTransmissionRatio( vehicleTopSpeed,
  470.                                                                                                                                                                                                                                 wheelRadius,
  471.                                                                                                                                                                                                                                 maxEngineRpm,
  472.                                                                                                                                                                                                                                 transmission.m_gearsRatio[ numGears - 1 ] );
  473. }
  474.  
  475. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultBrake& brake )
  476. {
  477.         brake.m_wheelBrakingProperties.setSize( data.m_numWheels );
  478.  
  479.         const float bt = 1500.0f;
  480.         brake.m_wheelBrakingProperties[0].m_maxBreakingTorque = bt;
  481.         brake.m_wheelBrakingProperties[1].m_maxBreakingTorque = bt;
  482.         brake.m_wheelBrakingProperties[2].m_maxBreakingTorque = bt;
  483.         brake.m_wheelBrakingProperties[3].m_maxBreakingTorque = bt;
  484.  
  485.         // Handbrake is attached to rear wheels only.
  486.         brake.m_wheelBrakingProperties[0].m_isConnectedToHandbrake = false;
  487.         brake.m_wheelBrakingProperties[1].m_isConnectedToHandbrake = false;
  488.         brake.m_wheelBrakingProperties[2].m_isConnectedToHandbrake = true;
  489.         brake.m_wheelBrakingProperties[3].m_isConnectedToHandbrake = true;
  490.         brake.m_wheelBrakingProperties[0].m_minPedalInputToBlock = 0.9f;
  491.         brake.m_wheelBrakingProperties[1].m_minPedalInputToBlock = 0.9f;
  492.         brake.m_wheelBrakingProperties[2].m_minPedalInputToBlock = 0.9f;
  493.         brake.m_wheelBrakingProperties[3].m_minPedalInputToBlock = 0.9f;
  494.         brake.m_wheelsMinTimeToBlock = 1000.0f;
  495. }
  496.  
  497. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultSuspension& suspension )
  498. {
  499.         suspension.m_wheelParams.setSize( data.m_numWheels );
  500.         suspension.m_wheelSpringParams.setSize( data.m_numWheels );
  501.  
  502.         suspension.m_wheelParams[0].m_length = 0.35f;
  503.         suspension.m_wheelParams[1].m_length = 0.35f;
  504.         suspension.m_wheelParams[2].m_length = 0.35f;
  505.         suspension.m_wheelParams[3].m_length = 0.35f;
  506.  
  507.         const float str = 50.0f;
  508.         suspension.m_wheelSpringParams[0].m_strength = str;
  509.         suspension.m_wheelSpringParams[1].m_strength = str;
  510.         suspension.m_wheelSpringParams[2].m_strength = str;
  511.         suspension.m_wheelSpringParams[3].m_strength = str;
  512.  
  513.         const float wd = 3.0f;
  514.         suspension.m_wheelSpringParams[0].m_dampingCompression = wd;
  515.         suspension.m_wheelSpringParams[1].m_dampingCompression = wd;
  516.         suspension.m_wheelSpringParams[2].m_dampingCompression = wd;
  517.         suspension.m_wheelSpringParams[3].m_dampingCompression = wd;
  518.  
  519.         suspension.m_wheelSpringParams[0].m_dampingRelaxation = wd;
  520.         suspension.m_wheelSpringParams[1].m_dampingRelaxation = wd;
  521.         suspension.m_wheelSpringParams[2].m_dampingRelaxation = wd;
  522.         suspension.m_wheelSpringParams[3].m_dampingRelaxation = wd;
  523.        
  524.         //
  525.         // NB: The hardpoints MUST be positioned INSIDE the chassis.
  526.         //
  527.         {
  528.                 const hkReal hardPointFrontX = 1.3f;
  529.                 const hkReal hardPointBackX = -1.1f;
  530.                 const hkReal hardPointY = -0.05f;
  531.                 const hkReal hardPointZ = 1.1f;
  532.  
  533.                 suspension.m_wheelParams[0].m_hardpointChassisSpace.set ( hardPointFrontX, hardPointY, -hardPointZ);
  534.                 suspension.m_wheelParams[1].m_hardpointChassisSpace.set ( hardPointFrontX, hardPointY,  hardPointZ);
  535.                 suspension.m_wheelParams[2].m_hardpointChassisSpace.set ( hardPointBackX, hardPointY, -hardPointZ);
  536.                 suspension.m_wheelParams[3].m_hardpointChassisSpace.set ( hardPointBackX, hardPointY,  hardPointZ);
  537.         }
  538.  
  539.         const hkVector4 downDirection( 0.0f, -1.0f, 0.0f );
  540.         suspension.m_wheelParams[0].m_directionChassisSpace = downDirection;
  541.         suspension.m_wheelParams[1].m_directionChassisSpace = downDirection;
  542.         suspension.m_wheelParams[2].m_directionChassisSpace = downDirection;
  543.         suspension.m_wheelParams[3].m_directionChassisSpace = downDirection;
  544. }
  545.  
  546. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultAerodynamics& aerodynamics )
  547. {
  548.         aerodynamics.m_airDensity = 1.3f;
  549.         // In m^2.
  550.         aerodynamics.m_frontalArea = 1.0f;     
  551.  
  552.         aerodynamics.m_dragCoefficient = 0.7f;
  553.         aerodynamics.m_liftCoefficient = -0.3f;
  554.  
  555.         // Extra gavity applies in world space (independent of m_chassisCoordinateSystem).
  556.         aerodynamics.m_extraGravityws.set( 0.0f, -5.0f, 0.0f);
  557. }
  558.  
  559. void VehicleSetup::setupComponent( const hkpVehicleData& data, hkpVehicleDefaultVelocityDamper& velocityDamper )
  560. {
  561.         // Caution: setting negative damping values will add energy to system.
  562.         // Setting the value to 0 will not affect the angular velocity.
  563.  
  564.         // Damping the change of the chassis angular velocity when below m_collisionThreshold.
  565.         // This will affect turning radius and steering.
  566.         velocityDamper.m_normalSpinDamping    = 0.0f;
  567.  
  568.         // Positive numbers dampen the rotation of the chassis and
  569.         // reduce the reaction of the chassis in a collision.
  570.         velocityDamper.m_collisionSpinDamping = 4.0f;
  571.  
  572.         // The threshold in m/s at which the algorithm switches from
  573.         // using the normalSpinDamping to the collisionSpinDamping.    
  574.         velocityDamper.m_collisionThreshold   = 1.0f;
  575. }
  576.  
  577. void VehicleSetup::setupWheelCollide( const hkpWorld* world, const hkpVehicleInstance& vehicle, hkpVehicleRayCastWheelCollide& wheelCollide )
  578. {
  579.         // Set the wheels to have the same collision filter info as the chassis.
  580.         wheelCollide.m_wheelCollisionFilterInfo = vehicle.getChassis()->getCollisionFilterInfo();
  581. }
  582.  
  583. void VehicleSetup::setupTyremarks( const hkpVehicleData& data, hkpTyremarksInfo& tyreMarks )
  584. {
  585.         tyreMarks.m_minTyremarkEnergy = 100.0f;
  586.         tyreMarks.m_maxTyremarkEnergy  = 1000.0f;
  587. }
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599. hkpConvexVerticesShape* createCarChassisShape()
  600. {
  601.         hkReal xSize = 1.75f;
  602.         hkReal ySize = 0.25f;
  603.         hkReal zSize = 1.1f;
  604.  
  605.         hkReal xBumper = 1.9f;
  606.         hkReal yBumper = 0.15f;
  607.         hkReal zBumper = 1.0f;
  608.  
  609.         hkReal xRoofFront = 0.4f;
  610.         hkReal xRoofBack = -1.0f;
  611.         hkReal yRoof = ySize + 0.45f;
  612.         hkReal zRoof = 0.7f;
  613.  
  614.         hkReal xDoorFront = xRoofFront;
  615.         hkReal xDoorBack = xRoofBack;
  616.         hkReal yDoor = ySize;
  617.         hkReal zDoor = zSize + 0.1f;
  618.  
  619.         int numVertices = 22;
  620.  
  621.         // 16 = 4 (size of "each float group", 3 for x,y,z, 1 for padding) * 4 (size of float).
  622.         int stride = sizeof(hkReal) * 4;
  623.  
  624.         HK_ALIGN16(hkReal vertices[] )= {
  625.                 xSize, ySize, zSize, 0.0f,              // v0
  626.                 xSize, ySize, -zSize, 0.0f,             // v1
  627.                 xSize, -ySize, zSize, 0.0f,             // v2
  628.                 xSize, -ySize, -zSize, 0.0f,    // v3
  629.                 -xSize, -ySize, zSize, 0.0f,    // v4
  630.                 -xSize, -ySize, -zSize, 0.0f,   // v5
  631.  
  632.                 xBumper, yBumper, zBumper, 0.0f,        // v6
  633.                 xBumper, yBumper, -zBumper, 0.0f,       // v7
  634.                 -xBumper, yBumper, zBumper, 0.0f,       // v8
  635.                 -xBumper, yBumper, -zBumper, 0.0f,      // v9
  636.  
  637.                 xRoofFront, yRoof, zRoof, 0.0f,         // v10
  638.                 xRoofFront, yRoof, -zRoof, 0.0f,        // v11
  639.                 xRoofBack, yRoof, zRoof, 0.0f,          // v12
  640.                 xRoofBack, yRoof, -zRoof, 0.0f,         // v13
  641.  
  642.                 xDoorFront, yDoor, zDoor, 0.0f,         // v14
  643.                 xDoorFront, yDoor, -zDoor, 0.0f,        // v15
  644.                 xDoorFront, -yDoor, zDoor, 0.0f,        // v16
  645.                 xDoorFront, -yDoor, -zDoor, 0.0f,       // v17
  646.  
  647.                 xDoorBack, yDoor, zDoor, 0.0f,          // v18
  648.                 xDoorBack, yDoor, -zDoor, 0.0f,         // v19
  649.                 xDoorBack, -yDoor, zDoor, 0.0f,         // v20
  650.                 xDoorBack, -yDoor, -zDoor, 0.0f,        // v21
  651.         };
  652.        
  653.         //
  654.         // SHAPE CONSTRUCTION.
  655.         //
  656.        
  657.         hkStridedVertices               stridedVerts;
  658.         stridedVerts.m_numVertices      =       numVertices;
  659.         stridedVerts.m_striding         =       stride;
  660.         stridedVerts.m_vertices         =       vertices;
  661.        
  662.         return new hkpConvexVerticesShape(stridedVerts);
  663. }
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674.  
  675.  
  676.  
  677.  
  678.  
  679.  
  680.         hkpVehicleInstance* m_vehicle;
  681.         hkpConvexVerticesShape* chassisShape = createCarChassisShape();
  682.         //                      hkpSphereShape* chassisShape = new hkpSphereShape(0.15f);
  683. //      hkpRigidBody* chassis;
  684.         hkpRigidBody* chassisRigidBody;
  685.  
  686. void Pojazd()
  687. {
  688.  
  689.                 {
  690.                 hkpRigidBodyCinfo chassisInfo;
  691.  
  692.                 chassisInfo.m_mass = 750.0f;   
  693.                 chassisInfo.m_shape = chassisShape;
  694.                 chassisInfo.m_friction = 0.4f;
  695.  
  696.                 chassisInfo.m_motionType = hkpMotion::MOTION_BOX_INERTIA;
  697.                 chassisInfo.m_position.set(0.0f, 1.0f, 0.0f);
  698.                 hkpInertiaTensorComputer::setShapeVolumeMassProperties(chassisInfo.m_shape,
  699.                                                                                 chassisInfo.m_mass,
  700.                                                                                 chassisInfo);
  701.  
  702.                 chassisRigidBody = new hkpRigidBody(chassisInfo);
  703.  
  704.                 // No longer need reference to shape as the hkpRigidBody holds one.
  705.                 chassisShape->removeReference();
  706.  
  707.                 m_world->addEntity(chassisRigidBody);
  708.         }
  709.  
  710.                 //      createVehicle( chassisRigidBody );
  711.        
  712.  
  713.  
  714.         m_vehicle = new hkpVehicleInstance( chassisRigidBody );
  715.  
  716.  
  717.         VehicleSetup setup;
  718.         setup.buildVehicle( m_world, *m_vehicle );
  719.  
  720.         m_vehicle->addToWorld( m_world );
  721.  
  722.         m_world->addAction(m_vehicle);
  723.  
  724.  
  725.         chassisRigidBody->removeReference();
  726.  
  727. }
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.                        
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792. //z game.h
  793. const int WIDTH = 800; // default width of the window
  794. const int HEIGHT = 600; // default height of the window
  795.  
  796. const float FRAME_PERIOD = 0.01667f; // ~60 fps
  797.  
  798. bool fullscreen = false;
  799.  
  800. int xOrigin = -1;
  801. int yOrigin = -1;
  802. float katY=0.0,deltakatY = 0.0;
  803.  
  804. float kat=0.0,deltakat = 0.0, ratio;
  805. float x=0.0f,y=1.0f,z=3.0f;
  806. float lx=0.0f,ly=0.0f,lz=0.0f;
  807. int deltaRuch = 0;
  808.  
  809.  
  810. // ---------------------------- More Constants ------------------------------ //
  811.  
  812. const float TORQUE_MULTIPLIER = 0.20f; // multiplier used for ball movement
  813.  
  814. const float VIEW_ANGLE = 50.0f; // field of view in the Y direction
  815. const float NEAR_CLIPPING = 0.05f; // near clipping distance (5cm)
  816. const float FAR_CLIPPING = 1000.0f; // far clipping distance (10m)
  817.  
  818. //const float ANGLE_LIMIT = 70.0f * HK_REAL_PI/180.0f; // 7 degrees limit
  819.  
  820. // ------------------------- Global GameData object ------------------------- //
  821.  
  822. // GameData structure
  823. /* This structure represent all the data associated with a game instance,
  824.  * this includes persistent data for operation of the Havok engine,
  825.  * variables used for user input, and game status variables.
  826.  */
  827.  
  828.         // Havok persistent objects
  829.        
  830.  
  831.         hkpRigidBody* m_sphereBody;
  832.         hkpRigidBody* m_sphereBody2;
  833.         hkpRigidBody* m_sphereBody3;
  834.         hkpRigidBody* m_ground;
  835.         //hkpRigidBody* m_planeBody;
  836.         #ifdef DEBUG
  837.                 hkVisualDebugger* m_vdb;
  838.                 hkpPhysicsContext* m_physicsContext;
  839.         #endif
  840.  
  841.         // Sphere movement variables
  842.         hkReal m_forward, m_strife; // variables for WASD movement
  843.        
  844.  
  845.         // Function to reset everything in the GameData object
  846.         void reset()
  847.         {
  848.                 // reset everything
  849.                 m_sphereBody->setPositionAndRotation(hkVector4(0.0f, 0.6f, 0.0f), hkQuaternion::getIdentity());
  850.                 m_sphereBody->setLinearVelocity(hkVector4(0.0f, 0.0f, 0.0f));
  851.                 m_sphereBody->setAngularVelocity(hkVector4(0.0f, 0.0f, 0.0f));
  852.                 m_sphereBody2->setPositionAndRotation(hkVector4(0.3f, 1.0f, 0.0f), hkQuaternion::getIdentity());
  853.                 m_sphereBody2->setLinearVelocity(hkVector4(0.0f, 0.0f, 0.0f));
  854.                 m_sphereBody2->setAngularVelocity(hkVector4(0.0f, 0.0f, 0.0f));
  855.                 m_sphereBody3->setPositionAndRotation(hkVector4(-0.3f, 1.0f, 0.0f), hkQuaternion::getIdentity());
  856.                 m_sphereBody3->setLinearVelocity(hkVector4(0.0f, 0.0f, 0.0f));
  857.                 m_sphereBody3->setAngularVelocity(hkVector4(0.0f, 0.0f, 0.0f));
  858.                 m_forward = 0.0f;
  859.                 m_strife = 0.0f;
  860.         }
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867. // -------------------------- Graphics Utilities ---------------------------- //
  868.  
  869.  
  870.  
  871.         void mouseMove(int x, int y) {
  872.  
  873.  
  874.                 deltakat = (x - xOrigin) * 0.0001f;
  875.         deltakatY = (-y + yOrigin) * 0.0001f;
  876.  
  877. }
  878.  
  879. void mouseButton(int button, int state, int x, int y) {
  880.  
  881.         // only start motion if the left button is pressed
  882.         if (button == GLUT_LEFT_BUTTON) {
  883.  
  884.                 // when the button is released
  885.                 if (state == GLUT_UP) {
  886.                         kat += deltakat;
  887.                         xOrigin = -1;
  888.                         yOrigin = -1;
  889.                         deltakat = 0;
  890.             deltakatY = 0;
  891.                 }
  892.                 else  {// state = GLUT_DOWN
  893.                         xOrigin = x;
  894.                         yOrigin = y;
  895.                 }
  896.         }
  897. }
  898.  
  899.  
  900.  
  901.  
  902.  
  903.  
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912. void zorientujMnie (float ang) {
  913.      
  914.      lx = sin(ang);
  915.      //ly = sin(ang);
  916.      lz = -cos(ang);
  917.      glLoadIdentity();
  918.      gluLookAt(x, y, z, x+lx, y+ly, z+lz,
  919.                         0.0f, 1.0f, 0.0f);    
  920.      }
  921.      
  922.      
  923. void zorientujMnieY (float ang) {
  924.      
  925.      //lx = sin(ang);
  926.      ly = tan(ang);
  927.      //lz = -cos(ang);
  928.      glLoadIdentity();
  929.      gluLookAt(x, y, z, x+lx, y+ly, z+lz,
  930.                         0.0f, 1.0f, 0.0f);    
  931.      }
  932.      
  933.      
  934.      
  935. void PlaskiRuch(int i) {
  936.      x = x + i*(lx)*0.1;
  937.      z = z + i*(lz)*0.1;
  938.      glLoadIdentity();
  939.      gluLookAt(x, y, z,
  940.                   x + lx, y + ly, z + lz,
  941.                   0.0f,1.0f,0.0f);
  942.      }
  943.  
  944.  
  945.  
  946.  
  947.  
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954.  
  955.  
  956.  
  957.  
  958.  
  959.  
  960. // Initialize OpenGL
  961. static void initGraphics()
  962. {
  963.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  964.         glCullFace(GL_BACK);
  965.         glEnable(GL_CULL_FACE);
  966.         glEnable(GL_LIGHTING);
  967.         glShadeModel(GL_SMOOTH);
  968.         glEnable(GL_DEPTH_TEST);
  969.         glEnable (GL_BLEND);
  970.         glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  971.         const GLfloat modelAmbient[] = {0.0f, 0.0f, 0.0f, 1.0f};
  972.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmbient);
  973.         const GLfloat lightAmbient[] = {0.4f, 0.4f, 0.4f, 0.0f};
  974.         glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
  975.         const GLfloat lightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
  976.         glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
  977.         const GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f, 1.0f};
  978.         glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
  979.         glEnable(GL_LIGHT0);
  980.  
  981.         const GLfloat lightPosition[] = {0.0f, 1.0f, 0.0f, 1.0f};
  982.         glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  983. }
  984.  
  985. // Setup OpenGL for scene rendering
  986. static void beginSceneRendering()
  987. {
  988.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  989.         glMatrixMode(GL_PROJECTION);
  990.         glLoadIdentity();
  991.         gluPerspective(
  992.                 VIEW_ANGLE,
  993.                 static_cast<double>(glutGet(GLUT_WINDOW_WIDTH))/static_cast<double>(glutGet(GLUT_WINDOW_HEIGHT)), // aspect ratio
  994.                 NEAR_CLIPPING,
  995.                 FAR_CLIPPING);
  996.         glMatrixMode(GL_MODELVIEW);
  997.         glLoadIdentity();
  998.                      gluLookAt(x, y, z, x+lx, y+ly, z+lz,
  999.                         0.0f, 1.0f, 0.0f);
  1000.         //gluLookAt(x=x+lx, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  1001.  
  1002.  
  1003.  
  1004. if (deltaRuch)
  1005.    {PlaskiRuch(deltaRuch);}
  1006. if (deltakat) {
  1007.    kat += deltakat;
  1008.    zorientujMnie(kat);
  1009. }
  1010.  if (deltakatY) {
  1011.    katY += deltakatY;
  1012.    zorientujMnieY(katY);
  1013. }  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021. }
  1022.  
  1023. // Draw a sphere (after beginSceneRendering())
  1024. static void drawSphere(const hkVector4& position, const hkQuaternion& quaternion)
  1025. {
  1026.         glPushMatrix();
  1027.                 const GLfloat diffuseColor[] = {0.6f, 0.6f, 0.6f, 1.0f};
  1028.                 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, diffuseColor);
  1029.                 const GLfloat specularColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
  1030.                 glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
  1031.                 glMaterialf(GL_FRONT, GL_SHININESS, 60.0f);
  1032.                 glTranslatef(position.getComponent(0), position.getComponent(1), position.getComponent(2));
  1033.  
  1034.  
  1035.                                 if(quaternion.hasValidAxis())
  1036.                 {
  1037.                 hkReal angle = quaternion.getAngle() * 180.0f / HK_REAL_PI;
  1038.                         hkVector4 axis;
  1039.                         quaternion.getAxis(axis);
  1040.                         glRotatef(angle, axis.getSimdAt(0), axis.getSimdAt(1), axis.getSimdAt(2));
  1041.                 }
  1042.  
  1043.  
  1044.  
  1045.                 glutSolidSphere(0.07f, 50, 50);
  1046.         glPopMatrix();
  1047.        
  1048. }
  1049.  
  1050. static void drawSphere2(const hkVector4& position, const hkQuaternion& quaternion)
  1051. {
  1052.         glPushMatrix();
  1053.                 const GLfloat diffuseColor[] = {1.0f, 0.0f, 0.0f, 1.0f};
  1054.                 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, diffuseColor);
  1055.                 const GLfloat specularColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
  1056.                 glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
  1057.                 glMaterialf(GL_FRONT, GL_SHININESS, 60.0f);
  1058.  
  1059.  
  1060.  
  1061.  
  1062.                 glTranslatef(position.getComponent(0), position.getComponent(1), position.getComponent(2));
  1063.  
  1064.                 if(quaternion.hasValidAxis())
  1065.                 {
  1066.                 hkReal angle = quaternion.getAngle() * 180.0f / HK_REAL_PI;
  1067.                         hkVector4 axis;
  1068.                         quaternion.getAxis(axis);
  1069.                         glRotatef(angle, axis.getSimdAt(0), axis.getSimdAt(1), axis.getSimdAt(2));
  1070.                 }
  1071.  
  1072.  
  1073.  
  1074.  
  1075.                 glutSolidSphere(0.10f, 10, 10);
  1076.         glPopMatrix();
  1077. }
  1078.  
  1079. static void drawSphere3(const hkVector4& position, const hkQuaternion& quaternion)
  1080. {
  1081.         glPushMatrix();
  1082.                 const GLfloat diffuseColor[] = {0.0f, 1.0f, 0.0f, 1.0f};
  1083.                 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, diffuseColor);
  1084.                 const GLfloat specularColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
  1085.                 glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
  1086.                 glMaterialf(GL_FRONT, GL_SHININESS, 60.0f);
  1087.                 glTranslatef(position.getComponent(0), position.getComponent(1), position.getComponent(2));
  1088.  
  1089.  
  1090.  
  1091.  
  1092.                                 if(quaternion.hasValidAxis())
  1093.                 {
  1094.                 hkReal angle = quaternion.getAngle() * 180.0f / HK_REAL_PI;
  1095.                         hkVector4 axis;
  1096.                         quaternion.getAxis(axis);
  1097.                         glRotatef(angle, axis.getSimdAt(0), axis.getSimdAt(1), axis.getSimdAt(2));
  1098.                 }
  1099.  
  1100.  
  1101.  
  1102.  
  1103.                 glutSolidSphere(0.15f, 50, 50);
  1104.         glPopMatrix();
  1105. }
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113. static void drawPlane(const hkVector4& position)
  1114. {
  1115.         glPushMatrix();
  1116.                 const GLfloat specularColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
  1117.                 glMaterialfv(GL_FRONT, GL_SPECULAR, specularColor);
  1118.                
  1119.                                 const GLfloat diffuseColor[] = {0.0f, 0.0f, 1.0f, 1.0f};
  1120.                                 glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, diffuseColor);
  1121.                                
  1122.                                 glTranslatef(position.getComponent(0), position.getComponent(1), position.getComponent(2));
  1123.                                 glScalef(2.0f, 4.00f, 2.0f);
  1124.                                 glutSolidCube(1.0f);
  1125.  
  1126.         glPopMatrix();
  1127. }
  1128.  
  1129.  
  1130.  
  1131.  
  1132. // Draw text on screen (after the scene has been rendered)
  1133. static void drawText(const char* string)
  1134. {
  1135.         glDisable(GL_DEPTH_TEST);
  1136.         glMatrixMode(GL_PROJECTION);
  1137.         glLoadIdentity();
  1138.     glOrtho( 0.0, glutGet( GLUT_WINDOW_WIDTH ),
  1139.                  0.0, glutGet( GLUT_WINDOW_HEIGHT ),
  1140.                  -1.0, 1.0 );
  1141.         glMatrixMode(GL_MODELVIEW);
  1142.         glLoadIdentity();
  1143.         glRasterPos2i( 10, glutGet( GLUT_WINDOW_HEIGHT ) - 30 );
  1144.         glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
  1145.         //glutBitmapString(GLUT_BITMAP_HELVETICA_18, reinterpret_cast<const unsigned char*>(string));
  1146.         glEnable(GL_DEPTH_TEST);
  1147. }
  1148.  
  1149. // Deinitialize OpenGL (no-op)
  1150. static void deinitGraphics()
  1151. {}
  1152.  
  1153. // --------------------------- Error Reporting ------------------------------ //
  1154.  
  1155. // Havok error reporting function
  1156. static inline void HK_CALL errorReport(const char* msg, void* userContext)
  1157. {
  1158.         std::cerr << msg << std::endl;
  1159. }
  1160.  
  1161.  
  1162. // Initialization function
  1163. void OnInit() {
  1164.         // Initialize Havok
  1165.         // The frameinfo buffer must NOT be zero if physics is being used (it is the solver buffer)
  1166. #ifdef DEBUG
  1167.         hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initChecking( hkMallocAllocator::m_defaultMallocAllocator, hkMemorySystem::FrameInfo(1024*1024) );
  1168. #else
  1169.         hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault( hkMallocAllocator::m_defaultMallocAllocator, hkMemorySystem::FrameInfo(1024*1024) );
  1170. #endif
  1171.  
  1172.         hkBaseSystem::init( memoryRouter, errorReport );
  1173.         {
  1174.                 // Create the simulation world
  1175.                 {
  1176.                         hkpWorldCinfo worldInfo;
  1177.                 worldInfo.m_collisionTolerance = 0.1f;
  1178.                         worldInfo.m_gravity.set(0.0f,-9.8f,0.0f);
  1179.                         worldInfo.setBroadPhaseWorldSize(10000.0f);
  1180.                 worldInfo.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM);
  1181.                         m_world = new hkpWorld(worldInfo);
  1182.                                
  1183.                         // This is needed to detect collisions
  1184.                         hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() );
  1185.                 }
  1186. #ifdef DEBUG
  1187.                 // Connect to the visual debugger
  1188.                 {
  1189.                         m_physicsContext = new hkpPhysicsContext();
  1190.                         m_physicsContext->addWorld( m_world ); // add all worlds as you have
  1191.                         hkpPhysicsContext::registerAllPhysicsProcesses();
  1192.                         hkArray<hkProcessContext*> contexts;
  1193.                         contexts.pushBack( m_physicsContext );  
  1194.                         m_vdb = new hkVisualDebugger( contexts );
  1195.                         m_vdb->serve();
  1196.                 }
  1197. #endif
  1198.                 // Create a sphere
  1199.                 {
  1200.                         hkpSphereShape* sphere = new hkpSphereShape(0.07f);
  1201.                         // convex radius for spheres is exactly the sphere radius
  1202.                         hkpRigidBodyCinfo rigidBodyInfo;
  1203.                         rigidBodyInfo.m_shape = sphere;
  1204.                         rigidBodyInfo.m_motionType = hkpMotion::MOTION_DYNAMIC;
  1205.                         hkpInertiaTensorComputer::setShapeVolumeMassProperties(
  1206.                                 sphere, 1.0f, rigidBodyInfo);
  1207.                         rigidBodyInfo.m_position.set(0.0f, 0.6f, 0.0f);
  1208.                         rigidBodyInfo.m_friction = 1.0f;
  1209.                         rigidBodyInfo.m_restitution = 0.2f;
  1210.  
  1211.                         m_sphereBody = new hkpRigidBody(rigidBodyInfo);
  1212.                         sphere->removeReference();
  1213.                
  1214.                         m_world->addEntity(m_sphereBody);
  1215.                 }
  1216.  
  1217.  
  1218.                 // Create a sphere2
  1219.                 {
  1220.                         hkpSphereShape* sphere2 = new hkpSphereShape(0.10f);
  1221.                         // convex radius for spheres is exactly the sphere radius
  1222.                         hkpRigidBodyCinfo rigidBodyInfo2;
  1223.                         rigidBodyInfo2.m_shape = sphere2;
  1224.                         rigidBodyInfo2.m_motionType = hkpMotion::MOTION_DYNAMIC;
  1225.                         hkpInertiaTensorComputer::setShapeVolumeMassProperties(
  1226.                                 sphere2, 0.1f, rigidBodyInfo2);
  1227.                         rigidBodyInfo2.m_position.set(0.3f, 1.0f, 0.0f);
  1228.                         rigidBodyInfo2.m_friction = 0.1f;
  1229.                         rigidBodyInfo2.m_restitution = 0.9f;
  1230.  
  1231.                         m_sphereBody2 = new hkpRigidBody(rigidBodyInfo2);
  1232.                         sphere2->removeReference();
  1233.                
  1234.                         m_world->addEntity(m_sphereBody2);
  1235.                 }
  1236.  
  1237.  
  1238.                                 // Create a sphere3
  1239.                 {
  1240.                         hkpSphereShape* sphere3 = new hkpSphereShape(0.15f);
  1241.                         // convex radius for spheres is exactly the sphere radius
  1242.                         hkpRigidBodyCinfo rigidBodyInfo3;
  1243.                         rigidBodyInfo3.m_shape = sphere3;
  1244.                         rigidBodyInfo3.m_motionType = hkpMotion::MOTION_DYNAMIC;
  1245.                         hkpInertiaTensorComputer::setShapeVolumeMassProperties(
  1246.                                 sphere3, 2.0f, rigidBodyInfo3);
  1247.                         rigidBodyInfo3.m_position.set(-0.3f, 1.0f, 0.0f);
  1248.                         rigidBodyInfo3.m_friction = 0.1f;
  1249.                         rigidBodyInfo3.m_restitution = 0.1f;
  1250.  
  1251.                         m_sphereBody3 = new hkpRigidBody(rigidBodyInfo3);
  1252.                         sphere3->removeReference();
  1253.                
  1254.                         m_world->addEntity(m_sphereBody3);
  1255.                 }
  1256.  
  1257.  
  1258.  
  1259.  
  1260.                 //ziemia
  1261.  
  1262.  
  1263.                         {
  1264.                 hkVector4 groundRadii( 1.0f, 2.0f, 1.0f );
  1265.                 hkpConvexShape* shape = new hkpBoxShape( groundRadii , 0 );
  1266.  
  1267.                 hkpRigidBodyCinfo ci;
  1268.  
  1269.                 ci.m_shape = shape;
  1270.                 ci.m_motionType = hkpMotion::MOTION_FIXED;
  1271.                 ci.m_position = hkVector4( 0.0f, -2.0f, 0.0f );
  1272.                 ci.m_qualityType = HK_COLLIDABLE_QUALITY_FIXED;
  1273.                 m_ground = new hkpRigidBody( ci );
  1274.                 m_world->addEntity(m_ground)->removeReference();
  1275.                 shape->removeReference();
  1276.         }
  1277.  
  1278.         //hkVector4 groundPos( 0.0f, 0.0f, 0.0f );
  1279.         //hkVector4 posy = groundPos;
  1280.  
  1281.  
  1282.                         Pojazd();
  1283.                
  1284.  
  1285.  
  1286.         }
  1287.  
  1288.         // initialize graphics
  1289.         initGraphics();
  1290. }
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298. // ------------------------ Frame Rendering Function ------------------------ //
  1299.  
  1300. // Render the whole game scene
  1301. static void renderScene()
  1302. {
  1303.         beginSceneRendering();
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.                 drawSphere(m_sphereBody->getPosition(),m_sphereBody->getRotation());
  1314.                 drawSphere2(m_sphereBody2->getPosition(),m_sphereBody2->getRotation());
  1315.                 drawSphere3(m_sphereBody3->getPosition(),m_sphereBody3->getRotation());
  1316.                 //drawPlane(m_planeBody->getRotation());
  1317.                 drawPlane(m_ground->getPosition());
  1318.         //endSceneRendering();
  1319. }
  1320.  
  1321. // Frame rendering function
  1322. void OnFrame() {
  1323.  
  1324.  
  1325.                
  1326.                
  1327.                         {
  1328.                                
  1329.                                 m_sphereBody->applyTorque(FRAME_PERIOD, hkVector4(-m_forward, 0.0f, -m_strife));
  1330.  
  1331.                                 m_world->stepDeltaTime(FRAME_PERIOD);
  1332.                                 #ifdef DEBUG
  1333.                                         m_vdb->step(FRAME_PERIOD);
  1334.                                 #endif
  1335.  
  1336.                                 renderScene();
  1337.  
  1338.                                 const hkVector4& spherePos = m_sphereBody->getPosition();
  1339.                                 const hkVector4& spherePos2 = m_sphereBody2->getPosition();
  1340.                         }
  1341.  
  1342. }
  1343.  
  1344. // ----------------------- Deinitialization Function ------------------------ //
  1345.  
  1346. void OnExit() {
  1347.         deinitGraphics();
  1348.  
  1349.         // delete havok entities
  1350.         {
  1351.                 //m_planeBody->removeReference();
  1352.                 m_sphereBody->removeReference();
  1353.                 m_sphereBody2->removeReference();
  1354.                 m_sphereBody3->removeReference();
  1355.                 m_ground->removeReference();
  1356. #ifdef DEBUG
  1357.                 m_vdb->removeReference();
  1358.                 m_physicsContext->removeReference();
  1359. #endif
  1360.                 m_world->removeReference();
  1361.         }
  1362.         hkBaseSystem::quit();
  1363.         hkMemoryInitUtil::quit();
  1364. }
  1365.  
  1366. // ------------------- Standard Key Management Function --------------------- //
  1367.  
  1368. // Key manager function
  1369. void KeyManager(unsigned char key, int x, int y) {
  1370.        
  1371.         switch(key) {
  1372.                 case 'w': // move forward
  1373.                 case 'W':
  1374.                         m_forward = TORQUE_MULTIPLIER;
  1375.                         break;
  1376.                 case 's': // move backwards
  1377.                 case 'S':
  1378.                         m_forward = -TORQUE_MULTIPLIER;
  1379.                         break;
  1380.                 case 'd': // strife right
  1381.                 case 'D':
  1382.                         m_strife = TORQUE_MULTIPLIER;
  1383.                         break;
  1384.                 case 'a': // strife left
  1385.                 case 'A':
  1386.                         m_strife = -TORQUE_MULTIPLIER;
  1387.                         break;
  1388.                 case 'c':
  1389.                 case 'C': // tabulation: toggle fullscreen
  1390.                         if(!fullscreen) {
  1391.                                 glutFullScreen();
  1392.                                 fullscreen = true;
  1393.                         } else {
  1394.                                 glutPositionWindow(10,10);
  1395.                                 glutReshapeWindow(WIDTH,HEIGHT);
  1396.                                 fullscreen = false;
  1397.                         }
  1398.                         break;
  1399.                 case 'r':
  1400.                 case 'R':       //restart
  1401.                         reset();
  1402.  
  1403.                         break;
  1404.         case 'o':
  1405.         case 'O': deltaRuch = 1;break;
  1406.         case 'L':
  1407.  
  1408.         case 'l':
  1409.         deltaRuch = -1;break;
  1410.         case 'k':
  1411.         case 'K':deltakat = -0.01f;break;
  1412.         case ';':
  1413.         case ':':
  1414.         deltakat = 0.01f;break;
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.                 }
  1422.  
  1423.  
  1424.  
  1425.              if (key == 27)
  1426.         exit(0);
  1427.        
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.         return;
  1434. }
  1435.  
  1436. // ------------------------ Key Up Manager Function ------------------------- //
  1437.  
  1438. // Key up manager function
  1439. void KeyUpManager(unsigned char key, int x, int y) {
  1440.         switch(key) {
  1441.         case 'w':
  1442.         case 'W':
  1443.         case 's':
  1444.         case 'S':
  1445.                 m_forward = 0.0f;
  1446.                 break;
  1447.         case 'd':
  1448.         case 'D':
  1449.         case 'a':
  1450.         case 'A':
  1451.                 m_strife = 0.0f;
  1452.                 break;
  1453.  
  1454.         case 'O':
  1455.         case 'L':
  1456.         case 'o':
  1457.         case 'l':
  1458.         deltaRuch = 0;break;
  1459.         case 'k':
  1460.         case 'K':
  1461.         case ';':
  1462.         case ':':
  1463.         deltakat = 0.0f;break;
  1464.        
  1465.  
  1466.         }
  1467.  
  1468.  
  1469.  
  1470.  
  1471. }
  1472.  
  1473. // ----------------------- Timer Management Function ------------------------ //
  1474.  
  1475. // Timer manager
  1476. void TimerManager(int) {
  1477.         // post a new display operation
  1478.         glutPostRedisplay();
  1479.                
  1480.         glutTimerFunc(static_cast<unsigned>(FRAME_PERIOD*1000.0f), TimerManager, 0);
  1481. }
  1482.  
  1483. // ------------------- Window Reshape Management Function ------------------- //
  1484.  
  1485. // Window resize manager
  1486. void ResizeManager(int width, int height) {
  1487.         glViewport(0, 0, width, height);
  1488.  
  1489.  
  1490.  
  1491.  
  1492. }
  1493.  
  1494. #include <Common/Base/keycode.cxx>
  1495.  
  1496. // we're not using any product apart from Havok Physics.
  1497. #undef HK_FEATURE_PRODUCT_AI
  1498. #undef HK_FEATURE_PRODUCT_ANIMATION
  1499. #undef HK_FEATURE_PRODUCT_CLOTH
  1500. #undef HK_FEATURE_PRODUCT_DESTRUCTION
  1501. #undef HK_FEATURE_PRODUCT_BEHAVIOR
  1502.  
  1503. // Also we're not using any serialization/versioning so we don't need any of these.
  1504.  
  1505. #define HK_EXCLUDE_FEATURE_SerializeDeprecatedPre700
  1506. #define HK_EXCLUDE_FEATURE_RegisterVersionPatches
  1507. #define HK_EXCLUDE_FEATURE_MemoryTracker
  1508.  
  1509. #include <Common/Base/Config/hkProductFeatures.cxx>
  1510.  
  1511.  
  1512.  
  1513. void idle()
  1514. {
  1515.  //glutPostRedisplay();
  1516.  
  1517. }
  1518.  
  1519.  
  1520.  
  1521.  
  1522. // Drawing function
  1523. /* Uses the OnFrame() function to execute all scene-specific
  1524.  * actions, and also perform the swap buffers operation.
  1525.  */
  1526. void DrawGLScene(void) {
  1527.     OnFrame();
  1528.     glutSwapBuffers();
  1529. }
  1530.  
  1531. // Main function
  1532. int main(int argc, char **argv) {
  1533.        
  1534.     //int win;
  1535.     // FreeGLUT init
  1536.     glutInit(&argc,argv);
  1537.     glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|
  1538.         GLUT_MULTISAMPLE|GLUT_DEPTH);
  1539.     glutInitWindowSize(WIDTH,HEIGHT);
  1540.     glutInitWindowPosition(10,10);
  1541.     glutCreateWindow("HavokOGL");
  1542.     // initialize the scene
  1543.     OnInit();
  1544.         glutIgnoreKeyRepeat(1);
  1545.     glutDisplayFunc(DrawGLScene);
  1546.         glutIdleFunc(idle);
  1547.     glutReshapeFunc(ResizeManager);
  1548.     glutKeyboardFunc(KeyManager);
  1549.     glutKeyboardUpFunc(KeyUpManager);
  1550.         glutMouseFunc(mouseButton);
  1551.         glutMotionFunc(mouseMove);
  1552.  
  1553.    // glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
  1554.    //     GLUT_ACTION_CONTINUE_EXECUTION);
  1555.     glutTimerFunc(static_cast<unsigned>(FRAME_PERIOD*1000.0f), TimerManager, 0);
  1556.     glutMainLoop();
  1557.     OnExit();
  1558.     return 0;
  1559. }
clone this paste RAW Paste Data