void BallController::updateBalls(std::vector <Ball>& balls, Grid* grid, float deltaTime, int maxX, int maxY) { const float FRICTION = 0.01f; // Update our grabbed balls velocity if (m_grabbedBall != -1) { balls[m_grabbedBall].velocity = balls[m_grabbedBall].position - m_prevPos; } glm::vec2 gravity = getGravityAccel(); for (size_t i = 0; i < balls.size(); i++) { // get handle for less typing Ball& ball = balls[i]; // Update motion if its not grabbed if (i != m_grabbedBall) { ball.position += ball.velocity * deltaTime; // Apply friction glm::vec2 momentumVec = ball.velocity * ball.mass; if (momentumVec.x != 0 || momentumVec.y != 0) { if (FRICTION < glm::length(momentumVec)) { ball.velocity -= deltaTime * FRICTION * glm::normalize(momentumVec) / ball.mass; } else { ball.velocity = glm::vec2(0.0f); } } // Apply gravity ball.velocity += gravity * deltaTime; } // Check wall collision if (ball.position.x < ball.radius) { ball.position.x = ball.radius; if (ball.velocity.x < 0) ball.velocity.x *= -1; } else if (ball.position.x + ball.radius >= maxX) { ball.position.x = maxX - ball.radius - 1; if (ball.velocity.x > 0) ball.velocity.x *= -1; } if (ball.position.y < ball.radius) { ball.position.y = ball.radius; if (ball.velocity.y < 0) ball.velocity.y *= -1; } else if (ball.position.y + ball.radius >= maxY) { ball.position.y = maxY - ball.radius - 1; if (ball.velocity.y > 0) ball.velocity.y *= -1; } // Check to see if the ball moved Cell* newCell = grid->getCell(ball.position); if (newCell != ball.ownerCell) { grid->removeBallFromCell(&balls[i]); grid->addBall(&balls[i], newCell); } } // Updates all collisions using the spatial partitioning updateCollision(grid); //// Update our grabbed ball if (m_grabbedBall != -1) { // Update the velocity again, in case it got changed by collision balls[m_grabbedBall].velocity = balls[m_grabbedBall].position - m_prevPos; m_prevPos = balls[m_grabbedBall].position; } }
void BallController::updateBalls(std::vector<Ball>& balls, Grid* grid, float deltaTime, int maxX, int maxY) { const float FRICTION = 0.001f; if (_grabbedBall != -1) { balls[_grabbedBall].velocity = balls[_grabbedBall].position - _prePos; } glm::vec2 gravity = getGravityAccel(); for (int i = 0; i < balls.size(); i++) { Ball& ball = balls[i]; if (i != _grabbedBall) { ball.position += ball.velocity * deltaTime; glm::vec2 momentumVec = ball.velocity * ball.mass; if (momentumVec.x != 0 || momentumVec.y != 0) { if (FRICTION < glm::length(momentumVec)) { ball.velocity -= deltaTime * FRICTION * glm::normalize(momentumVec) / ball.mass; } else { ball.velocity = glm::vec2(0.0f); } } ball.velocity += gravity * deltaTime; } if (ball.position.x < ball.radius) { ball.position.x = ball.radius; if (ball.velocity.x < 0) { ball.velocity.x *= -1; } } else if (ball.position.x + ball.radius>= maxX) { ball.position.x = maxX - ball.radius - 1; if (ball.velocity.x > 0) { ball.velocity.x *= -1; } } if (ball.position.y < ball.radius) { ball.position.y = ball.radius; if (ball.velocity.y < 0) { ball.velocity.y *= -1; } } else if (ball.position.y + ball.radius >= maxY) { ball.position.y = maxY - ball.radius - 1; if (ball.velocity.y > 0) { ball.velocity.y *= -1; } } Cell* newCell = grid->getCell(ball.position); if (newCell != ball.ownerCell) { grid->removeBallFromCell(&balls[i]); grid->addBall(&balls[i], newCell); } } updateCollision(grid); if (_grabbedBall != -1) { balls[_grabbedBall].velocity = balls[_grabbedBall].position - _prePos; _prePos = balls[_grabbedBall].position; } }