void Sphere::move() { coord += step; gravity(); if(collisionX()) flipX(); if(collisionY()) flipY(); }
void SideScroller::FixedUpdate() { for (size_t i = 0; i < entities.size(); i++) { // handle collisions // collide with something on bottom if (entities[i]->collidedBottom) { entities[i]->isJumping = false; entities[i]->velocity_y = 0.0f; } // collide with something on top if (entities[i]->collidedTop){ entities[i]->velocity_y = 0.0f; } // collide with something on left if (entities[i]->collidedLeft){ entities[i]->velocity_x = 0.0f; } // collide with something on right if (entities[i]->collidedRight){ entities[i]->velocity_x = 0.0f; } // reset entities[i]->collidedBottom = false; entities[i]->collidedTop = false; entities[i]->collidedLeft = false; entities[i]->collidedRight = false; // apply gravity if (!entities[i]->isStatic) { entities[i]->velocity_x += gravity_x * FIXED_TIMESTEP; entities[i]->velocity_y += gravity_y * FIXED_TIMESTEP; } // apply friction entities[i]->velocity_x = lerp(entities[i]->velocity_x, 0.0f, FIXED_TIMESTEP * entities[i]->friction_x); entities[i]->velocity_y = lerp(entities[i]->velocity_y, 0.0f, FIXED_TIMESTEP * entities[i]->friction_y); // apply acceleration entities[i]->velocity_x += entities[i]->acceleration_x * FIXED_TIMESTEP; entities[i]->velocity_y += entities[i]->acceleration_y * FIXED_TIMESTEP; // update y values entities[i]->y += entities[i]->velocity_y * FIXED_TIMESTEP; // handle collision Y collisionY(entities[i]); // update x values entities[i]->x += entities[i]->velocity_x * FIXED_TIMESTEP; // handle collision X collisionX(entities[i]); } }
ball_t moveBall(ball_t ballToMove) { //check if ball is running into any of the side walls if (collisionX(ballToMove.position.x)) { ballToMove.velocity.x = -1*ballToMove.velocity.x; } ballToMove.position.x += ballToMove.velocity.x; //check if the ball is running into the top or bottom and move the ball if (collisionY(ballToMove.position.y)) { ballToMove.velocity.y = -1*ballToMove.velocity.y; } ballToMove.position.y += ballToMove.velocity.y; return ballToMove; }
ball_t moveBall(ball_t ballToMove, paddle thePaddle) { //check if the ball has collided with the paddle if (collisionObjects(ballToMove, thePaddle)) { ballToMove.velocity.x = -1*ballToMove.velocity.x; } //check if ball is running into any of the side walls if (collisionX(ballToMove.position.x)) { ballToMove.velocity.x = -1*ballToMove.velocity.x; } //check if the ball is running into the top or bottom and move the ball if (collisionY(ballToMove.position.y)) { ballToMove.velocity.y = -1*ballToMove.velocity.y; } ballToMove.position.y += ballToMove.velocity.y; ballToMove.position.x += ballToMove.velocity.x; return ballToMove; }