1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | // quote's physics integration quote_vel_y += dt * -250.0f; quote_x += dt * quote_vel_x; quote_y += dt * quote_vel_y; quote_circle.p = c2V(quote_x, quote_y); quote_sprite.x = quote_x; quote_sprite.y = quote_y; // quote's collision detection and collision resolution for (int i = 0; i < tile_count; ++i) { tile_t tile = tiles[i]; if (tile.shape_id == -1) continue; shape_t shape = shapes[tile.shape_id]; shape = get_transformed_tile_shape(tile, shape); c2Manifold m; c2Collide("e_circle, 0, C2_CIRCLE, &shape.u, 0, shape.type, &m); if (m.count) { // move quote out of colliding configuration float depth = -m.depths[0]; c2v n = m.n; quote_x += n.x * depth; quote_y += n.y * depth; // "slide along wall" // remove all velocity in the direction of the wall's normal // v -= n * dot(v, n) c2v v = c2V(quote_vel_x, quote_vel_y); v = c2Sub(v, c2Mulvs(n, c2Dot(v, n))); quote_vel_x = v.x; quote_vel_y = v.y; } } |
For a real game more work would need to be done to prevent Quote from floating off the ground at odd times. Logic to detect "on the ground" or not would need to be implemented. Some downwards raycasting, or maybe a Time of Impact solver, need to be used to make Qoute stick to the ground even when running over bumps, when is he "on ground". A bunch of other features would probably need to be implemented too, for a real game.