Skip to content

Commit

Permalink
More curve types
Browse files Browse the repository at this point in the history
Catmull curves
Line Curves(half assed)
Curve refactor
#73 near 0 length sliders
  • Loading branch information
fd committed Apr 12, 2015
1 parent 13d3834 commit 12e7000
Show file tree
Hide file tree
Showing 11 changed files with 444 additions and 182 deletions.
5 changes: 4 additions & 1 deletion src/itdelatrisu/opsu/objects/Slider.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import itdelatrisu.opsu.OsuFile;
import itdelatrisu.opsu.OsuHitObject;
import itdelatrisu.opsu.Utils;
import itdelatrisu.opsu.objects.curves.CatmullCurve;
import itdelatrisu.opsu.objects.curves.CircumscribedCircle;
import itdelatrisu.opsu.objects.curves.Curve;
import itdelatrisu.opsu.objects.curves.LinearBezier;
Expand Down Expand Up @@ -459,8 +460,10 @@ public void updatePosition() {

if (hitObject.getSliderType() == OsuHitObject.SLIDER_PASSTHROUGH && hitObject.getSliderX().length == 2)
this.curve = new CircumscribedCircle(hitObject, color);
else if ( hitObject.getSliderType() == OsuHitObject.SLIDER_CATMULL)
this.curve = new CatmullCurve(hitObject, color);
else
this.curve = new LinearBezier(hitObject, color);
this.curve = new LinearBezier(hitObject, color, hitObject.getSliderType() == OsuHitObject.SLIDER_LINEAR);
}

@Override
Expand Down
47 changes: 2 additions & 45 deletions src/itdelatrisu/opsu/objects/curves/Bezier2.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,10 @@
*
* @author fluddokt (https://github.com/fluddokt)
*/
public class Bezier2 {
public class Bezier2 extends CurveType{
/** The control points of the Bezier curve. */
private Vec2f[] points;

/** Points along the curve of the Bezier curve. */
private Vec2f[] curve;

/** Distances between a point of the curve and the last point. */
private float[] curveDis;

/** The number of points along the curve. */
private int ncurve;

/** The total distances of this Bezier. */
private float totalDistance;

/**
* Constructor.
* @param points the control points
Expand All @@ -52,19 +40,7 @@ public Bezier2(Vec2f[] points) {
for (int i = 0; i < points.length - 1; i++)
approxlength += points[i].cpy().sub(points[i + 1]).len();

// subdivide the curve
this.ncurve = (int) (approxlength / 4);
this.curve = new Vec2f[ncurve];
for (int i = 0; i < ncurve; i++)
curve[i] = pointAt(i / (float) ncurve);

// find the distance of each point from the previous point
this.curveDis = new float[ncurve];
this.totalDistance = 0;
for (int i = 0; i < ncurve; i++) {
curveDis[i] = (i == 0) ? 0 : curve[i].cpy().sub(curve[i - 1]).len();
totalDistance += curveDis[i];
}
init(approxlength);
// System.out.println("New Bezier2 "+points.length+" "+approxlength+" "+totalDistance());
}

Expand All @@ -84,25 +60,6 @@ public Vec2f pointAt(float t) {
return c;
}

/**
* Returns the points along the curve of the Bezier curve.
*/
public Vec2f[] getCurve() { return curve; }

/**
* Returns the distances between a point of the curve and the last point.
*/
public float[] getCurveDistances() { return curveDis; }

/**
* Returns the number of points along the curve.
*/
public int points() { return ncurve; }

/**
* Returns the total distances of this Bezier curve.
*/
public float totalDistance() { return totalDistance; }

/**
* Calculates the binomial coefficient.
Expand Down
53 changes: 53 additions & 0 deletions src/itdelatrisu/opsu/objects/curves/CatmullCurve.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014, 2015 Jeffrey Han
*
* opsu! is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu! is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
*/

package itdelatrisu.opsu.objects.curves;

import java.util.LinkedList;

import itdelatrisu.opsu.OsuHitObject;

import org.newdawn.slick.Color;

public class CatmullCurve extends EqualDistanceMultiCurve{

public CatmullCurve(OsuHitObject hitObject, Color color) {
super(hitObject, color);
LinkedList<CurveType> catmulls = new LinkedList<CurveType>();
int ncontrolPoints = hitObject.getSliderX().length + 1;
LinkedList<Vec2f> points = new LinkedList<Vec2f>(); // temporary list of points to separate different curves

//aaa .... needs at least two points
//aabb
//aabc abcc
//aabc abcd bcdd
points.addLast(new Vec2f(getX(0),getY(0)));
for(int i=0;i<ncontrolPoints;i++){
points.addLast(new Vec2f(getX(i),getY(i)));
if(points.size()>=4){
catmulls.add(new CentripetalCatmullRom(points.toArray(new Vec2f[0])));
points.removeFirst();
}
}
points.addLast(new Vec2f(getX(ncontrolPoints-1),getY(ncontrolPoints-1)));
if(points.size()>=4){
catmulls.add(new CentripetalCatmullRom(points.toArray(new Vec2f[0])));
}
init(catmulls);
}
}
101 changes: 101 additions & 0 deletions src/itdelatrisu/opsu/objects/curves/CentripetalCatmullRom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* opsu! - an open-source osu! client
* Copyright (C) 2014, 2015 Jeffrey Han
*
* opsu! is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu! is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
*/

package itdelatrisu.opsu.objects.curves;

/**
* Representation of a Centripetal Catmull–Rom spline.
* http://en.wikipedia.org/wiki/Centripetal_Catmull%E2%80%93Rom_spline
*
* Currently not technically Centripetal Catmull–Rom
*
* @author fluddokt (https://github.com/fluddokt)
*/
public class CentripetalCatmullRom extends CurveType{

float [] time;
Vec2f[] points;
protected CentripetalCatmullRom(Vec2f[] points) {
if (points.length != 4)
throw new Error("need exactly 4 points");

this.points = points;
System.out.println("C");
for(int i=0;i<4;i++){
System.out.println(points[i]);
}
time = new float[4];
time[0] = 0;
float approxLength = 0;
for(int i=1;i<4;i++){
float len = 0;
if(i>0)
len = points[i].cpy().sub(points[i-1]).len();
if(len<=0)
len+=0.0001f;
approxLength += len;
//time[i] = (float) Math.sqrt(len) + time[i-1];// ^(0.5)
time[i] = i;
System.out.println("knot:"+i+" "+time[i]);
}
init(approxLength/2);
}
public Vec2f pointAt(float t, int start, int end){
int ind1 = start;
int ind2 = end;
if(end-start==3){
ind1 = 1;
ind2 = 2;
}
//System.out.println("ST:"+start+" "+end+" "+ind1+" "+ind2);
float div = (time[ind2] - time[ind1]);
float v1 = (time[ind2] - t)/div;
float v2 = (t - time[ind1])/div;
System.out.println("div"+div+" "+v1+" "+v2);
if(end-start==1)
return points[start].cpy().scale(v1).add(points[end].cpy().scale(v2));
else
return pointAt(t,start,end-1).scale(v1).add(pointAt(t,start+1,end).scale(v2));
}

public Vec2f pointAt(float t){
t = t * (time[2]-time[1]) + time[1];

Vec2f A1 = points[0].cpy().scale((time[1]-t)/(time[1]-time[0])).add(points[1].cpy().scale((t-time[0])/(time[1]-time[0])));
Vec2f A2 = points[1].cpy().scale((time[2]-t)/(time[2]-time[1])).add(points[2].cpy().scale((t-time[1])/(time[2]-time[1])));
Vec2f A3 = points[2].cpy().scale((time[3]-t)/(time[3]-time[2])).add(points[3].cpy().scale((t-time[2])/(time[3]-time[2])));

Vec2f B1 = A1.cpy().scale((time[2]-t)/(time[2]-time[0])).add(A2.cpy().scale((t-time[0])/(time[2]-time[0])));
Vec2f B2 = A2.cpy().scale((time[3]-t)/(time[3]-time[1])).add(A3.cpy().scale((t-time[1])/(time[3]-time[1])));

Vec2f C = B1.cpy().scale((time[2]-t)/(time[2]-time[1])).add(B2.cpy().scale((t-time[1])/(time[2]-time[1])));

return C;
}
public String toString(){
String t = "CATMULLROM";
for(Vec2f p: points){
t+=p;
}

t+=pointAt(0);
t+=pointAt(1);

return t;
}
}
16 changes: 2 additions & 14 deletions src/itdelatrisu/opsu/objects/curves/CircumscribedCircle.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ public class CircumscribedCircle extends Curve {
/** The number of steps in the curve to draw. */
private float step;

/** Points along the curve. */
private Vec2f[] curve;

/**
* Constructor.
* @param hitObject the associated OsuHitObject
Expand All @@ -67,8 +64,7 @@ public class CircumscribedCircle extends Curve {
public CircumscribedCircle(OsuHitObject hitObject, Color color) {
super(hitObject, color);

this.step = hitObject.getPixelLength() / 5f;


// construct the three points
this.start = new Vec2f(getX(0), getY(0));
this.mid = new Vec2f(getX(1), getY(1));
Expand Down Expand Up @@ -125,6 +121,7 @@ else if (Math.abs(startAng - (endAng - TWO_PI)) < TWO_PI && isIn(startAng, midAn
this.drawStartAngle = (float) ((startAng + (startAng > endAng ? -HALF_PI : HALF_PI)) * 180 / Math.PI);

// calculate points
this.step = hitObject.getPixelLength() / CURVE_POINTS_SEPERATION;
curve = new Vec2f[(int) step + 1];
for (int i = 0; i < curve.length; i++) {
float[] xy = pointAt(i / step);
Expand Down Expand Up @@ -178,15 +175,6 @@ public float[] pointAt(float t) {
};
}

@Override
public void draw(Color color) {
Image hitCircle = GameImage.HITCIRCLE.getImage();
Image hitCircleOverlay = GameImage.HITCIRCLE_OVERLAY.getImage();
for (int i = 0; i < step; i++)
hitCircleOverlay.drawCentered(curve[i].x, curve[i].y, Utils.COLOR_WHITE_FADE);
for (int i = 0; i < step; i++)
hitCircle.drawCentered(curve[i].x, curve[i].y, color);
}

@Override
public float getEndAngle() { return drawEndAngle; }
Expand Down
22 changes: 21 additions & 1 deletion src/itdelatrisu/opsu/objects/curves/Curve.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@

package itdelatrisu.opsu.objects.curves;

import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.OsuHitObject;
import itdelatrisu.opsu.Utils;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;

/**
* Representation of a curve.
*
* @author fluddokt (https://github.com/fluddokt)
*/
public abstract class Curve {

protected static float CURVE_POINTS_SEPERATION = 50;
/** The associated OsuHitObject. */
protected OsuHitObject hitObject;

Expand All @@ -37,6 +42,9 @@ public abstract class Curve {
/** The scaled slider x, y coordinate lists. */
protected float[] sliderX, sliderY;

/** Points along the curve. To be set be inherited classes*/
protected Vec2f[] curve;

/**
* Constructor.
* @param hitObject the associated OsuHitObject
Expand All @@ -48,6 +56,7 @@ protected Curve(OsuHitObject hitObject, Color color) {
this.y = hitObject.getScaledY();
this.sliderX = hitObject.getScaledSliderX();
this.sliderY = hitObject.getScaledSliderY();
CURVE_POINTS_SEPERATION = 5;
}

/**
Expand All @@ -61,7 +70,18 @@ protected Curve(OsuHitObject hitObject, Color color) {
* Draws the full curve to the graphics context.
* @param color the color filter
*/
public abstract void draw(Color color);
public void draw(Color color) {
if ( curve == null){
System.out.println("draw curve"+this);
return;
}
Image hitCircle = GameImage.HITCIRCLE.getImage();
Image hitCircleOverlay = GameImage.HITCIRCLE_OVERLAY.getImage();
for (int i = 0; i < curve.length; i++)
hitCircleOverlay.drawCentered(curve[i].x, curve[i].y, Utils.COLOR_WHITE_FADE);
for (int i = 0; i < curve.length; i++)
hitCircle.drawCentered(curve[i].x, curve[i].y, color);
}

/**
* Returns the angle of the first control point.
Expand Down
Loading

0 comments on commit 12e7000

Please sign in to comment.