// Update function for this object type void RedSquareUpdate ( REDSQUARE *self ) { COLLISION_FLAG flag = CheckHotspotCollision( self->rect_ ); char buffer[100] = { 0 }; // Apply acceleration for keystrokes // Documentation for physics: http://cecilsunkure.blogspot.com/2012/02/basic-2d-vector-physics.html // Documentation for collisions: http://cecilsunkure.blogspot.com/2012/07/collision-basic-2d-collision-detection.html // http://cecilsunkure.blogspot.com/2012/04/binary-collision-maps-platformer.html if(IsKeyPressed( VK_RIGHT )) { self->vel_.x_ += self->accel_ * GetDT( ); } if(IsKeyPressed( VK_LEFT )) { self->vel_.x_ += -self->accel_ * GetDT( ); } if(IsKeyPressed( VK_UP )) { self->vel_.y_ += -self->accel_ * GetDT( ); } if(IsKeyPressed( VK_DOWN )) { self->vel_.y_ += self->accel_ * GetDT( ); } // Clamp velocity within a max/min range VelocityClamp( &self->vel_, .02f ); self->rect_.center_.x_ += self->vel_.x_ * GetDT( ); self->rect_.center_.y_ += self->vel_.y_ * GetDT( ); sprintf( buffer, "%.3f, %.3f", self->vel_.x_, self->vel_.y_ ); WriteStringToScreen( buffer, 30, 30 ); if(flag & COLLISION_BOTTOM) { SnapToCell( &self->rect_.center_.y_ ); self->vel_.y_ = 0.f; } if(flag & COLLISION_TOP) { SnapToCell( &self->rect_.center_.y_ ); self->vel_.y_ = 0.f; } if(flag & COLLISION_LEFT) { SnapToCell( &self->rect_.center_.x_ ); self->vel_.x_ = 0.f; } if(flag & COLLISION_RIGHT) { SnapToCell( &self->rect_.center_.x_ ); self->vel_.x_ = 0.f; } }
void Level1_Draw(void) { float f; fprintf(output, "\n\n\nLevel1:Draw\n"); f = i + 1.73f; fprintf(output, "%f got snapped to: ", f); SnapToCell(&f); fprintf(output, "%f\n", f); if(i == 1) fprintf(output, "Flag for 3.4f, 1.7f, 1.0f, 1.0f is: %i //You should get 1\n", Flag); else if(i == 2) fprintf(output, "Flag for 1.7f, 2.2f, 1.0f, 1.0f is: %i //You should get 11\n", Flag); else if(i == 3) fprintf(output, "Flag for 1.2f, 3.8f, 1.0f, 1.0f is: %i //You should get 15\n", Flag); }
int main(void) { int Flag; float f; // Test here if you can read "Export.txt" ImportMapDataFromFile("Exported.txt"); //Printing the information read by ImportMapDataFromFile // You should get (With the prodvided export file): // Width: 5 // Height: 5 // Map Data: // 1 1 1 1 1 // 1 0 0 0 1 // 1 4 2 0 1 // 1 1 1 3 1 // 1 1 1 1 1 PrintRetrievedInformation(); // Testing "GetCellValue" printf("\n\nTesting GetCellValue\n"); printf("Cell (0,0) = %i\n", GetCellValue(0, 0)); //You should get 1 printf("Cell (1,1) = %i\n", GetCellValue(1, 1)); //You should get 1 printf("Cell (1,2) = %i\n", GetCellValue(1, 2)); //You should get 0 printf("Cell (1,2) = %i\n", GetCellValue(1, 2)); //You should get 0 printf("Cell (3,4) = %i\n", GetCellValue(3, 4)); //You should get 1 printf("Cell (-1,1) = %i\n", GetCellValue(-1, 1)); //You should get 0 printf("Cell (1,-1) = %i\n", GetCellValue(1, -1)); //You should get 0 printf("Cell (5,1) = %i\n", GetCellValue(5, 1)); //You should get 0 printf("Cell (1,5) = %i\n", GetCellValue(1, 5)); //You should get 0 // Testing "SnapToCell" printf("\n\nTesting SnapToCell\n"); f = 0.2f; SnapToCell(&f); printf("0.2 got snapped to: %f\n", f); //You should get 0.5 f = 2.3f; SnapToCell(&f); printf("2.3 got snapped to: %f\n", f); //You should get 2.5 f = 1.7f; SnapToCell(&f); printf("1.7 got snapped to: %f\n", f); //You should get 1.5 f = 5.4f; SnapToCell(&f); printf("5.4 got snapped to: %f\n", f); //You should get 5.5 f = 4.9f; SnapToCell(&f); printf("4.9 got snapped to: %f\n", f); //You should get 4.5 //Testing "CheckInstanceBinaryMapCollision" printf("\n\nTesting CheckInstanceBinaryMapCollision\n"); Flag = 0; Flag = CheckInstanceBinaryMapCollision(1.7f, 2.2f, 1.0f, 1.0f); printf("Flag for 1.7f, 2.2f, 1.0f, 1.0f is: %i\n", Flag); //You should get 11 Flag = 0; Flag = CheckInstanceBinaryMapCollision(3.4f, 1.7f, 1.0f, 1.0f); printf("Flag for 3.4f, 1.7f, 1.0f, 1.0f is: %i\n", Flag); //You should get 1 Flag = 0; Flag = CheckInstanceBinaryMapCollision(1.2f, 3.8f, 1.0f, 1.0f); printf("Flag for 1.2f, 3.8f, 1.0f, 1.0f is: %i\n", Flag); //You should get 15 FreeMapData(); return 1; }