Store raw hit object coordinates instead of scaled ones.

Fixes a bug where a resolution change (by restarting through the app) wouldn't re-scale hit object coordinates.  Scaled coordinates are now stored in HitObject fields.

Signed-off-by: Jeffrey Han <itdelatrisu@gmail.com>
This commit is contained in:
Jeffrey Han 2015-03-12 20:12:43 -04:00
parent 3bd9bbdafd
commit 49c85b3b08
7 changed files with 104 additions and 64 deletions

View File

@ -59,7 +59,7 @@ public class OsuHitObject {
xOffset, // offset right of border xOffset, // offset right of border
yOffset; // offset below health bar yOffset; // offset below health bar
/** Starting coordinates (scaled). */ /** Starting coordinates. */
private float x, y; private float x, y;
/** Start time (in ms). */ /** Start time (in ms). */
@ -77,7 +77,7 @@ public class OsuHitObject {
/** Slider curve type (SLIDER_* constant). */ /** Slider curve type (SLIDER_* constant). */
private char sliderType; private char sliderType;
/** Slider coordinate lists (scaled). */ /** Slider coordinate lists. */
private float[] sliderX, sliderY; private float[] sliderX, sliderY;
/** Slider repeat count. */ /** Slider repeat count. */
@ -163,8 +163,8 @@ public class OsuHitObject {
String tokens[] = line.split(","); String tokens[] = line.split(",");
// common fields // common fields
this.x = Float.parseFloat(tokens[0]) * xMultiplier + xOffset; this.x = Float.parseFloat(tokens[0]);
this.y = Float.parseFloat(tokens[1]) * yMultiplier + yOffset; this.y = Float.parseFloat(tokens[1]);
this.time = Integer.parseInt(tokens[2]); this.time = Integer.parseInt(tokens[2]);
this.type = Integer.parseInt(tokens[3]); this.type = Integer.parseInt(tokens[3]);
this.hitSound = Byte.parseByte(tokens[4]); this.hitSound = Byte.parseByte(tokens[4]);
@ -185,8 +185,8 @@ public class OsuHitObject {
this.sliderY = new float[sliderTokens.length - 1]; this.sliderY = new float[sliderTokens.length - 1];
for (int j = 1; j < sliderTokens.length; j++) { for (int j = 1; j < sliderTokens.length; j++) {
String[] sliderXY = sliderTokens[j].split(":"); String[] sliderXY = sliderTokens[j].split(":");
this.sliderX[j - 1] = Integer.parseInt(sliderXY[0]) * xMultiplier + xOffset; this.sliderX[j - 1] = Integer.parseInt(sliderXY[0]);
this.sliderY[j - 1] = Integer.parseInt(sliderXY[1]) * yMultiplier + yOffset; this.sliderY[j - 1] = Integer.parseInt(sliderXY[1]);
} }
this.repeat = Integer.parseInt(tokens[6]); this.repeat = Integer.parseInt(tokens[6]);
this.pixelLength = Float.parseFloat(tokens[7]); this.pixelLength = Float.parseFloat(tokens[7]);
@ -221,17 +221,25 @@ public class OsuHitObject {
} }
/** /**
* Returns the starting x coordinate. * Returns the raw starting x coordinate.
* @return the x coordinate
*/ */
public float getX() { return x; } public float getX() { return x; }
/** /**
* Returns the starting y coordinate. * Returns the raw starting y coordinate.
* @return the y coordinate
*/ */
public float getY() { return y; } public float getY() { return y; }
/**
* Returns the scaled starting x coordinate.
*/
public float getScaledX() { return x * xMultiplier + xOffset; }
/**
* Returns the scaled starting y coordinate.
*/
public float getScaledY() { return y * yMultiplier + yOffset; }
/** /**
* Returns the start time. * Returns the start time.
* @return the start time (in ms) * @return the start time (in ms)
@ -269,17 +277,43 @@ public class OsuHitObject {
public char getSliderType() { return sliderType; } public char getSliderType() { return sliderType; }
/** /**
* Returns a list of slider x coordinates. * Returns a list of raw slider x coordinates.
* @return the slider x coordinates
*/ */
public float[] getSliderX() { return sliderX; } public float[] getSliderX() { return sliderX; }
/** /**
* Returns a list of slider y coordinates. * Returns a list of raw slider y coordinates.
* @return the slider y coordinates
*/ */
public float[] getSliderY() { return sliderY; } public float[] getSliderY() { return sliderY; }
/**
* Returns a list of scaled slider x coordinates.
* Note that this method will create a new array.
*/
public float[] getScaledSliderX() {
if (sliderX == null)
return null;
float[] x = new float[sliderX.length];
for (int i = 0; i < x.length; i++)
x[i] = sliderX[i] * xMultiplier + xOffset;
return x;
}
/**
* Returns a list of scaled slider y coordinates.
* Note that this method will create a new array.
*/
public float[] getScaledSliderY() {
if (sliderY == null)
return null;
float[] y = new float[sliderY.length];
for (int i = 0; i < y.length; i++)
y[i] = sliderY[i] * yMultiplier + yOffset;
return y;
}
/** /**
* Returns the slider repeat count. * Returns the slider repeat count.
* @return the repeat count * @return the repeat count

View File

@ -37,6 +37,9 @@ public class Circle implements HitObject {
/** The associated OsuHitObject. */ /** The associated OsuHitObject. */
private OsuHitObject hitObject; private OsuHitObject hitObject;
/** The scaled starting x, y coordinates. */
private float x, y;
/** The associated Game object. */ /** The associated Game object. */
private Game game; private Game game;
@ -72,6 +75,8 @@ public class Circle implements HitObject {
*/ */
public Circle(OsuHitObject hitObject, Game game, GameData data, Color color, boolean comboEnd) { public Circle(OsuHitObject hitObject, Game game, GameData data, Color color, boolean comboEnd) {
this.hitObject = hitObject; this.hitObject = hitObject;
this.x = hitObject.getScaledX();
this.y = hitObject.getScaledY();
this.game = game; this.game = game;
this.data = data; this.data = data;
this.color = color; this.color = color;
@ -84,7 +89,6 @@ public class Circle implements HitObject {
if (timeDiff >= 0) { if (timeDiff >= 0) {
float oldAlpha = color.a; float oldAlpha = color.a;
float x = hitObject.getX(), y = hitObject.getY();
float scale = timeDiff / (float)game.getApproachTime(); float scale = timeDiff / (float)game.getApproachTime();
float approachScale = 1 + scale * 3; float approachScale = 1 + scale * 3;
@ -129,7 +133,7 @@ public class Circle implements HitObject {
@Override @Override
public boolean mousePressed(int x, int y) { public boolean mousePressed(int x, int y) {
double distance = Math.hypot(hitObject.getX() - x, hitObject.getY() - y); double distance = Math.hypot(this.x - x, this.y - y);
int circleRadius = GameImage.HITCIRCLE.getImage().getWidth() / 2; int circleRadius = GameImage.HITCIRCLE.getImage().getWidth() / 2;
if (distance < circleRadius) { if (distance < circleRadius) {
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
@ -138,11 +142,7 @@ public class Circle implements HitObject {
if (result > -1) { if (result > -1) {
data.addHitError(hitObject.getTime(), x, y, timeDiff); data.addHitError(hitObject.getTime(), x, y, timeDiff);
data.hitResult( data.hitResult(hitObject.getTime(), result, this.x, this.y, color, comboEnd, hitObject, 0);
hitObject.getTime(), result,
hitObject.getX(), hitObject.getY(),
color, comboEnd, hitObject, 0
);
return true; return true;
} }
} }
@ -152,7 +152,6 @@ public class Circle implements HitObject {
@Override @Override
public boolean update(boolean overlap, int delta, int mouseX, int mouseY, boolean keyPressed) { public boolean update(boolean overlap, int delta, int mouseX, int mouseY, boolean keyPressed) {
int time = hitObject.getTime(); int time = hitObject.getTime();
float x = hitObject.getX(), y = hitObject.getY();
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
int[] hitResultOffset = game.getHitResultOffsets(); int[] hitResultOffset = game.getHitResultOffsets();

View File

@ -52,6 +52,9 @@ public class Slider implements HitObject {
/** The associated OsuHitObject. */ /** The associated OsuHitObject. */
private OsuHitObject hitObject; private OsuHitObject hitObject;
/** The scaled starting x, y coordinates. */
protected float x, y;
/** The associated Game object. */ /** The associated Game object. */
private Game game; private Game game;
@ -130,6 +133,8 @@ public class Slider implements HitObject {
*/ */
public Slider(OsuHitObject hitObject, Game game, GameData data, Color color, boolean comboEnd) { public Slider(OsuHitObject hitObject, Game game, GameData data, Color color, boolean comboEnd) {
this.hitObject = hitObject; this.hitObject = hitObject;
this.x = hitObject.getScaledX();
this.y = hitObject.getScaledY();
this.game = game; this.game = game;
this.data = data; this.data = data;
this.color = color; this.color = color;
@ -147,7 +152,6 @@ public class Slider implements HitObject {
int timeDiff = hitObject.getTime() - trackPosition; int timeDiff = hitObject.getTime() - trackPosition;
float scale = timeDiff / (float) game.getApproachTime(); float scale = timeDiff / (float) game.getApproachTime();
float approachScale = 1 + scale * 3; float approachScale = 1 + scale * 3;
float x = hitObject.getX(), y = hitObject.getY();
float alpha = Utils.clamp(1 - scale, 0, 1); float alpha = Utils.clamp(1 - scale, 0, 1);
float oldAlpha = color.a; float oldAlpha = color.a;
@ -256,7 +260,7 @@ public class Slider implements HitObject {
lastPos[0], lastPos[1], color, comboEnd, hitObject, currentRepeats + 1); lastPos[0], lastPos[1], color, comboEnd, hitObject, currentRepeats + 1);
} else { // first circle } else { // first circle
data.hitResult(hitObject.getTime() + (int) sliderTimeTotal, result, data.hitResult(hitObject.getTime() + (int) sliderTimeTotal, result,
hitObject.getX(), hitObject.getY(), color, comboEnd, hitObject, currentRepeats + 1); x, y, color, comboEnd, hitObject, currentRepeats + 1);
} }
return result; return result;
@ -267,7 +271,7 @@ public class Slider implements HitObject {
if (sliderClickedInitial) // first circle already processed if (sliderClickedInitial) // first circle already processed
return false; return false;
double distance = Math.hypot(hitObject.getX() - x, hitObject.getY() - y); double distance = Math.hypot(this.x - x, this.y - y);
int circleRadius = GameImage.HITCIRCLE.getImage().getWidth() / 2; int circleRadius = GameImage.HITCIRCLE.getImage().getWidth() / 2;
if (distance < circleRadius) { if (distance < circleRadius) {
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
@ -285,8 +289,7 @@ public class Slider implements HitObject {
if (result > -1) { if (result > -1) {
data.addHitError(hitObject.getTime(), x,y,trackPosition - hitObject.getTime()); data.addHitError(hitObject.getTime(), x,y,trackPosition - hitObject.getTime());
sliderClickedInitial = true; sliderClickedInitial = true;
data.sliderTickResult(hitObject.getTime(), result, data.sliderTickResult(hitObject.getTime(), result, this.x, this.y, hitObject, currentRepeats);
hitObject.getX(), hitObject.getY(), hitObject, currentRepeats);
return true; return true;
} }
} }
@ -317,7 +320,6 @@ public class Slider implements HitObject {
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
int[] hitResultOffset = game.getHitResultOffsets(); int[] hitResultOffset = game.getHitResultOffsets();
int lastIndex = hitObject.getSliderX().length - 1;
boolean isAutoMod = GameMod.AUTO.isActive(); boolean isAutoMod = GameMod.AUTO.isActive();
if (!sliderClickedInitial) { if (!sliderClickedInitial) {
@ -328,11 +330,9 @@ public class Slider implements HitObject {
sliderClickedInitial = true; sliderClickedInitial = true;
if (isAutoMod) { // "auto" mod: catch any missed notes due to lag if (isAutoMod) { // "auto" mod: catch any missed notes due to lag
ticksHit++; ticksHit++;
data.sliderTickResult(time, GameData.HIT_SLIDER30, data.sliderTickResult(time, GameData.HIT_SLIDER30, x, y, hitObject, currentRepeats);
hitObject.getX(), hitObject.getY(), hitObject, currentRepeats);
} else } else
data.sliderTickResult(time, GameData.HIT_MISS, data.sliderTickResult(time, GameData.HIT_MISS, x, y, hitObject, currentRepeats);
hitObject.getX(), hitObject.getY(), hitObject, currentRepeats);
} }
// "auto" mod: send a perfect hit result // "auto" mod: send a perfect hit result
@ -340,8 +340,7 @@ public class Slider implements HitObject {
if (Math.abs(trackPosition - time) < hitResultOffset[GameData.HIT_300]) { if (Math.abs(trackPosition - time) < hitResultOffset[GameData.HIT_300]) {
ticksHit++; ticksHit++;
sliderClickedInitial = true; sliderClickedInitial = true;
data.sliderTickResult(time, GameData.HIT_SLIDER30, data.sliderTickResult(time, GameData.HIT_SLIDER30, x, y, hitObject, currentRepeats);
hitObject.getX(), hitObject.getY(), hitObject, currentRepeats);
} }
} }
@ -412,11 +411,11 @@ public class Slider implements HitObject {
// held during new repeat // held during new repeat
if (isNewRepeat) { if (isNewRepeat) {
ticksHit++; ticksHit++;
if (currentRepeats % 2 > 0) // last circle if (currentRepeats % 2 > 0) { // last circle
int lastIndex = hitObject.getSliderX().length;
data.sliderTickResult(trackPosition, GameData.HIT_SLIDER30, data.sliderTickResult(trackPosition, GameData.HIT_SLIDER30,
hitObject.getSliderX()[lastIndex], hitObject.getSliderY()[lastIndex], curve.getX(lastIndex), curve.getY(lastIndex), hitObject, currentRepeats);
hitObject, currentRepeats); } else // first circle
else // first circle
data.sliderTickResult(trackPosition, GameData.HIT_SLIDER30, data.sliderTickResult(trackPosition, GameData.HIT_SLIDER30,
c[0], c[1], hitObject, currentRepeats); c[0], c[1], hitObject, currentRepeats);
} }

View File

@ -34,6 +34,12 @@ public abstract class Curve {
/** The color of this curve. */ /** The color of this curve. */
protected Color color; protected Color color;
/** The scaled starting x, y coordinates. */
protected float x, y;
/** The scaled slider x, y coordinate lists. */
protected float[] sliderX, sliderY;
/** /**
* Constructor. * Constructor.
* @param hitObject the associated OsuHitObject * @param hitObject the associated OsuHitObject
@ -41,6 +47,10 @@ public abstract class Curve {
*/ */
protected Curve(OsuHitObject hitObject, Color color) { protected Curve(OsuHitObject hitObject, Color color) {
this.hitObject = hitObject; this.hitObject = hitObject;
this.x = hitObject.getScaledX();
this.y = hitObject.getScaledY();
this.sliderX = hitObject.getScaledSliderX();
this.sliderY = hitObject.getScaledSliderY();
this.color = color; this.color = color;
} }
@ -67,18 +77,16 @@ public abstract class Curve {
public abstract float getStartAngle(); public abstract float getStartAngle();
/** /**
* Returns the x coordinate of the control point at index i. * Returns the scaled x coordinate of the control point at index i.
* @param i the control point index
*/ */
protected float getX(int i) { public float getX(int i) { return (i == 0) ? x : sliderX[i - 1]; }
return (i == 0) ? hitObject.getX() : hitObject.getSliderX()[i - 1];
}
/** /**
* Returns the y coordinate of the control point at index i. * Returns the scaled y coordinate of the control point at index i.
* @param i the control point index
*/ */
protected float getY(int i) { public float getY(int i) { return (i == 0) ? y : sliderY[i - 1]; }
return (i == 0) ? hitObject.getY() : hitObject.getSliderY()[i - 1];
}
/** /**
* Linear interpolation of a and b at t. * Linear interpolation of a and b at t.

View File

@ -275,8 +275,8 @@ public class Replay {
for (int i = 0; i < frames.length; i++) { for (int i = 0; i < frames.length; i++) {
ReplayFrame frame = frames[i]; ReplayFrame frame = frames[i];
sb.append(String.format("%d|%s|%s|%d,", sb.append(String.format("%d|%s|%s|%d,",
frame.getTimeDiff(), nf.format(frame.getRawX()), frame.getTimeDiff(), nf.format(frame.getX()),
nf.format(frame.getRawY()), frame.getKeys())); nf.format(frame.getY()), frame.getKeys()));
} }
sb.append(String.format("%s|0|0|%d", SEED_STRING, seed)); sb.append(String.format("%s|0|0|%d", SEED_STRING, seed));

View File

@ -85,25 +85,25 @@ public class ReplayFrame {
*/ */
public void setTimeDiff(int diff) { this.timeDiff = diff; } public void setTimeDiff(int diff) { this.timeDiff = diff; }
/**
* Returns the scaled cursor x coordinate.
*/
public int getX() { return (int) (x * OsuHitObject.getXMultiplier() + OsuHitObject.getXOffset()); }
/**
* Returns the scaled cursor y coordinate.
*/
public int getY() { return (int) (y * OsuHitObject.getYMultiplier() + OsuHitObject.getYOffset()); }
/** /**
* Returns the raw cursor x coordinate. * Returns the raw cursor x coordinate.
*/ */
public float getRawX() { return x; } public float getX() { return x; }
/** /**
* Returns the raw cursor y coordinate. * Returns the raw cursor y coordinate.
*/ */
public float getRawY() { return y; } public float getY() { return y; }
/**
* Returns the scaled cursor x coordinate.
*/
public int getScaledX() { return (int) (x * OsuHitObject.getXMultiplier() + OsuHitObject.getXOffset()); }
/**
* Returns the scaled cursor y coordinate.
*/
public int getScaledY() { return (int) (y * OsuHitObject.getYMultiplier() + OsuHitObject.getYOffset()); }
/** /**
* Returns the keys pressed (KEY_* bitmask). * Returns the keys pressed (KEY_* bitmask).

View File

@ -911,8 +911,8 @@ public class Game extends BasicGameState {
if (frame.getTime() > 0) if (frame.getTime() > 0)
replaySkipTime = frame.getTime(); replaySkipTime = frame.getTime();
} else if (frame.getTime() == 0) { } else if (frame.getTime() == 0) {
replayX = frame.getX(); replayX = frame.getScaledX();
replayY = frame.getY(); replayY = frame.getScaledY();
replayKeyPressed = frame.isKeyPressed(); replayKeyPressed = frame.isKeyPressed();
} else } else
break; break;
@ -928,8 +928,8 @@ public class Game extends BasicGameState {
int trackPosition = MusicController.getPosition(); int trackPosition = MusicController.getPosition();
while (replayIndex < replay.frames.length && trackPosition >= replay.frames[replayIndex].getTime()) { while (replayIndex < replay.frames.length && trackPosition >= replay.frames[replayIndex].getTime()) {
ReplayFrame frame = replay.frames[replayIndex]; ReplayFrame frame = replay.frames[replayIndex];
replayX = frame.getX(); replayX = frame.getScaledX();
replayY = frame.getY(); replayY = frame.getScaledY();
replayKeyPressed = frame.isKeyPressed(); replayKeyPressed = frame.isKeyPressed();
if (replayKeyPressed) // send a key press if (replayKeyPressed) // send a key press
gameKeyPressed(frame.getKeys(), replayX, replayY); gameKeyPressed(frame.getKeys(), replayX, replayY);