import graphics3d.Graphics3DFramework; import Graphics3D; import Math; // For using the abs function // Variables for storing IDs of models and polygons int axis, box, pointPolygon; // Variables for storing IDs of vectors int directionVector, pointVector, interVector, normalVector; // Variables related to animation and motion double y = 3.0; // Height of the point double v = -0.0; // Falling speed double a = -1.0; // Gravitational acceleration double dt = 0.05; // Time interval for animation // Function called at the start of the program void onStart ( int rendererID ) { // Optional: Set window size and background color setWindowSize( 800, 600 ); setBackgroundColor( 0, 0, 0, 255 ); // Place axis model in the world coordinate system axis = newAxisModel( 3.0, 3.0, 3.0 ); mountModel( axis, rendererID ); // Place a point polygon to observe the motion pointPolygon = newPointPolygon( 0.0, 0.0, 0.0, 0.1 ); mountPolygon( pointPolygon, rendererID ); // Place a box model in the world coordinate system box = newBoxModel( 1.0, 1.0, 1.0 ); mountModel( box, rendererID ); // Prepare the direction vector for collision ray directionVector = newVector( 0.0, -1.0, 0.0 ); // Prepare the origin vector for collision ray pointVector = newVector( 0.0, 3.0, 0.0 ); // Prepare vector for storing intersection result interVector = newVector(); // Prepare vector for storing surface normal normalVector = newVector(); } // Function called repeatedly per frame (dozens of times per second) void onUpdate (int renderer) { // Calculate the falling position of the point v += a * dt; y += v * dt; // Update the origin of the collision ray and the point's position setVector( pointVector, 0.0, y, 0.0 ); setPolygonVector( pointPolygon, 0.0, y, 0.0 ); // Perform collision detection; returns whether an intersection exists bool hit = getModelIntersection( box, directionVector, pointVector, interVector, normalVector ); // If intersection exists and distance is less than 0.2, bounce back if( hit ){ // abs returns the absolute value if( abs( getVectorY( interVector ) - y ) < 0.2 ){ v = abs( v ); } } }