float vf_Count_Percentage(float *init, float *real, float *finish) { float vr[3], dr, vf[3], df; Make_Vector(init, finish, vf); df = Vector_Length(vf); Make_Vector(init, real, vr); dr = Vector_Length(vr); if (df == 0) return 100; return ((100.0f * dr) / df); }
// // A spring dampening function // for the camera // void SpringDamp( Vector currPos, Vector trgPos, // Target Position Vector prevTrgPos, // Previous Target Position Vector result, float springConst, // Hooke's Constant float dampConst, // Damp Constant float springLen) { Vector disp; // Displacement Vector velocity; // Velocity Vector mx; Vector z; float len; float dot; float forceMag; // Force Magnitude // Calculate Spring Force Vector_Minus(currPos, trgPos, disp); Vector_Minus(prevTrgPos, trgPos, velocity); len = Vector_Length(disp); // get dot product dot = DotProduct(disp, velocity); forceMag = springConst * (springLen - len) + dampConst * (dot / len); Vector_Normalize(disp, z); //disp *= forceMag; Vector_Multiply(z, mx, forceMag); printf("%0.2f %0.2f\n", mx[0], currPos[0]); Vector_Add(currPos, mx, result); //result[0] = currPos[0]; //result[1] = currPos[1]; //result[2] = currPos[2]; } // end of the function
// // Vector_Normalize // void Vector_Normalize(Vector a, Vector res) { float len; len = Vector_Length(a); if (len > 0.0f) len = 1.0f / len; else len = 0.0f; res[0] = a[0] * len; res[1] = a[1] * len; res[2] = a[2] * len; } // end of the function
int main() { sf::RenderWindow window(sf::VideoMode(1024, 768), "Breakout"); sf::RectangleShape background = sf::RectangleShape(sf::Vector2<float>(GAME_WIDTH, GAME_HEIGHT)); sf::RectangleShape paddle = sf::RectangleShape(sf::Vector2<float>(PADDLE_WIDTH, PADDLE_HEIGHT)); sf::CircleShape ball = sf::CircleShape(BALL_RADIUS); sf::RectangleShape blocks[MAX_BLOCKS]; bool blockVisible[MAX_BLOCKS]; sf::Vector2<float> ball_velocity = sf::Vector2f(); background.setPosition(GAME_LEFT, GAME_TOP); background.setFillColor(sf::Color::Blue); paddle.setPosition((GAME_WIDTH / 2 - PADDLE_WIDTH / 2), PADDLE_VPOS); paddle.setFillColor(sf::Color::Green); for(int i = 0; i < MAX_BLOCKS; i++) { blocks[i].setFillColor(sf::Color::Yellow); blocks[i].setSize(sf::Vector2f(BLOCK_SIZE, BLOCK_SIZE)); blockVisible[i] = 1; } srand(time(0)); double randNum; double angle; double collisionAngle; sf::Vector2f projection; sf::Vector2<int> moveVector; sf::Time dt = sf::Time::Zero; sf::Time t = sf::Time::Zero; sf::Clock clock; std::stringstream ss (std::stringstream::in | std::stringstream::out); sf::String ups = sf::String(); sf::Font font; if(!font.loadFromFile("arial.ttf")) { return 1; } sf::Text upsText = sf::Text(); upsText.setFont(font); //sf::Font font; //if(!font.loadFromFile()) bool ball_on_paddle = 1; while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); // mouse movement if(event.type == sf::Event::MouseMoved) { paddle.setPosition(event.mouseMove.x - PADDLE_WIDTH / 2, paddle.getPosition().y); } if(event.type == sf::Event::MouseButtonPressed && ball_on_paddle) { randNum = (double)rand() / RAND_MAX; angle = randNum * M_PI_2 + M_PI_4; ball_velocity = sf::Vector2<float>(BALL_SPEED * cos(angle),-BALL_SPEED * sin(angle)); ball_on_paddle = 0; } } // handle collisions with wall if(ball.getPosition().x < GAME_LEFT) { ball_velocity.x = abs(ball_velocity.x); } if(ball.getPosition().x > GAME_RIGHT - 2 * BALL_RADIUS) { ball_velocity.x = -abs(ball_velocity.x); } if(ball.getPosition().y < GAME_TOP) { ball_velocity.y = abs(ball_velocity.y); } if(ball.getPosition().y > GAME_BOTTOM - 2 * BALL_RADIUS) { ball_on_paddle = 1; } // collisions with paddle // TODO make this suck less. sf::Vector2f impulse = collide(ball, paddle); if(impulse.x != 0 || impulse.y != 0) { collisionAngle = acos((double)ball_velocity.y/(double)Vector_Length(ball_velocity)) + acos((double)impulse.y/(double)Vector_Length(impulse)) + M_PI; projection = Vector_Length(ball_velocity) * (float)cos(collisionAngle) * impulse; ball_velocity = ball_velocity + 2.0f * projection; //std::cout << impulse.x << ", " << impulse.y << std::endl; } dt = clock.restart(); t += dt; ss.str(""); ss.clear(); ss << (1/dt.asSeconds()); ups = ss.str(); upsText.setString(ups); if(ball_on_paddle) { Reset_Ball(&ball, paddle); } else { ball.move(ball_velocity * dt.asSeconds()); } if(t.asMilliseconds() > MS_PER_FRAME) { // draw window.clear(); window.draw(background); window.draw(paddle); window.draw(ball); window.draw(upsText); window.display(); t = t.Zero; } } return 0; }