public COLLIDE checkCollision(Vector2 pos, Vector2 vel) { Vector2 ptA, ptB; float dist; var result = COLLIDE.NONE; if (isBroken) { return(result); } // Check for collision with each edge Vector2 vec; // Bottom ptA.x = x; ptA.y = y + height; ptB.x = x + width; ptB.y = y + height; if (vel.y < 0 && pos.y > ptA.y) { if (pos.x > ptA.x && pos.x < ptB.x) { dist = pos.y - ptA.y; if (dist < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.DOWN; } } else { vec = pos.x <= ptA.x ? pos - ptA : pos - ptB; if (MathX.Fabs(vec.y) > MathX.Fabs(vec.x) && vec.LengthFast < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.DOWN; } } } if (result != COLLIDE.NONE) { return(result); } // Top ptA.y = y; ptB.y = y; if (vel.y > 0 && pos.y < ptA.y) { if (pos.x > ptA.x && pos.x < ptB.x) { dist = ptA.y - pos.y; if (dist < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.UP; } } else { vec = pos.x <= ptA.x ? pos - ptA : pos - ptB; if (MathX.Fabs(vec.y) > MathX.Fabs(vec.x) && vec.LengthFast < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.UP; } } } if (result != COLLIDE.NONE) { return(result); } // Left side ptA.x = x; ptA.y = y; ptB.x = x; ptB.y = y + height; if (vel.x > 0 && pos.x < ptA.x) { if (pos.y > ptA.y && pos.y < ptB.y) { dist = ptA.x - pos.x; if (dist < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.LEFT; } } else { vec = pos.y <= ptA.y ? pos - ptA : pos - ptB; if (MathX.Fabs(vec.x) >= MathX.Fabs(vec.y) && vec.LengthFast < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.LEFT; } } } if (result != COLLIDE.NONE) { return(result); } // Right side ptA.x = x + width; ptB.x = x + width; if (vel.x < 0 && pos.x > ptA.x) { if (pos.y > ptA.y && pos.y < ptB.y) { dist = pos.x - ptA.x; if (dist < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.LEFT; } } else { vec = pos.y <= ptA.y ? pos - ptA : pos - ptB; if (MathX.Fabs(vec.x) >= MathX.Fabs(vec.y) && vec.LengthFast < GameBustOutWindow.BALL_RADIUS) { result = COLLIDE.LEFT; } } } return(result); }
public override float Evaluate(float[] state, float[] newState, float t0, float t1) { int i, n; float max, error; var delta = t1 - t0; for (n = 0; n < 4; n++) { var halfDelta = delta * 0.5F; var fourthDelta = delta * 0.25F; float sixthDelta; // first step of first half delta derive(t0, userData, state, d1); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + fourthDelta * d1[i]; } // second step of first half delta derive(t0 + fourthDelta, userData, tmpState, d2); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + fourthDelta * d2[i]; } // third step of first half delta derive(t0 + fourthDelta, userData, tmpState, d3); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + halfDelta * d3[i]; } // fourth step of first half delta derive(t0 + halfDelta, userData, tmpState, d4); sixthDelta = halfDelta * (1.0F / 6.0F); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + sixthDelta * (d1[i] + 2.0F * (d2[i] + d3[i]) + d4[i]); } // first step of second half delta derive(t0 + halfDelta, userData, tmpState, d1half); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + fourthDelta * d1half[i]; } // second step of second half delta derive(t0 + halfDelta + fourthDelta, userData, tmpState, d2); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + fourthDelta * d2[i]; } // third step of second half delta derive(t0 + halfDelta + fourthDelta, userData, tmpState, d3); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + halfDelta * d3[i]; } // fourth step of second half delta derive(t0 + delta, userData, tmpState, d4); sixthDelta = halfDelta * (1.0F / 6.0F); for (i = 0; i < dimension; i++) { newState[i] = state[i] + sixthDelta * (d1[i] + 2.0F * (d2[i] + d3[i]) + d4[i]); } // first step of full delta for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + halfDelta * d1[i]; } // second step of full delta derive(t0 + halfDelta, userData, tmpState, d2); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + halfDelta * d2[i]; } // third step of full delta derive(t0 + halfDelta, userData, tmpState, d3); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + delta * d3[i]; } // fourth step of full delta derive(t0 + delta, userData, tmpState, d4); sixthDelta = delta * (1.0F / 6.0F); for (i = 0; i < dimension; i++) { tmpState[i] = state[i] + sixthDelta * (d1[i] + 2.0F * (d2[i] + d3[i]) + d4[i]); } // get max estimated error max = 0.0F; for (i = 0; i < dimension; i++) { error = MathX.Fabs((newState[i] - tmpState[i]) / (delta * d1[i] + 1e-10F)); if (error > max) { max = error; } } error = max / maxError; if (error <= 1.0f) { return(delta * 4.0F); } if (delta <= 1e-7F) { return(delta); } delta *= 0.25F; } return(delta); }