Skip to content

Commit

Permalink
Jeddic#6 Add a builder for the curve class that allows it to be speci…
Browse files Browse the repository at this point in the history
…fied as a series of anchor and control points
  • Loading branch information
richardTingle committed Dec 18, 2021
1 parent 3633263 commit 3287009
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/com/epaga/particles/valuetypes/Curve.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
*/
package com.epaga.particles.valuetypes;

import com.epaga.particles.valuetypes.curvebuilder.CurveBuilderAtAnchor;
import com.epaga.particles.valuetypes.curvebuilder.CurveBuilderStart;
import com.jme3.export.*;
import com.jme3.math.Vector2f;

Expand Down Expand Up @@ -158,4 +160,37 @@ public boolean equals(Object o) {

return true;
}


/**
* Produces a builder that can be used to fluently build a curve. A Curve will always be continuous (And should
* move in a positive X direction) but the gradient may change sharply.
*
* It is a series of anchor points connected either by straight line sections or cubic besier curves (defined by
* 2 control points).
*
* In normal usage the first anchor point should be at x = 0, all further points should advance in the X axis and
* the final anchor point should have x at 1. This is because usually X is the fractional life of the particle
*
* Example usage:
*
* <pre>{@code
* Curve curve = Curve.builder()
* .anchorPoint(new Vector2f(0,0))
* .anchorPoint(new Vector2f(0.5f,0.5f))
* .controlPoint1(new Vector2f(0.6f,0.5f))
* .controlPoint2(new Vector2f(0.8f,2f))
* .anchorPoint(new Vector2f(1,2f))
* .build();
* }</pre>
*
* This example produces a straight line from (0,0) -> (0.5,0.5), then a cubic Besier curves between (0.5,0.5) -> (1,2) with control points (0.6,0.5) and (0.8,2)
*
* Note that a builder should not be reused.
*
* @return a CurveBuilderStart
*/
public static CurveBuilderStart builder(){
return new CurveBuilderStart();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.epaga.particles.valuetypes.curvebuilder;

import com.epaga.particles.valuetypes.Curve;
import com.jme3.math.Vector2f;

public class CurveBuilderAtAnchor{

Curve curveBeingBuilt;
Vector2f controlPointIn;
Vector2f currentAnchor;

public CurveBuilderAtAnchor(Curve curveBeingBuilt, Vector2f controlPointIn, Vector2f currentAnchor){
this.curveBeingBuilt = curveBeingBuilt;
this.controlPointIn = controlPointIn;
this.currentAnchor = currentAnchor;
}

/**
* Adds a point that the curve will attempt to move towards but may not actually touch.
*
* The 2 control points are used to define a cubic Bézier curve between 2 anchors
* @param nextControlPoint the control point
* @return a CurveBuilderAtControlPoint1 a part of the curve builder system
*/
public CurveBuilderAtControlPoint1 controlPoint1( Vector2f nextControlPoint ){
return new CurveBuilderAtControlPoint1(curveBeingBuilt, controlPointIn, currentAnchor, nextControlPoint);
}

/**
* Produces a straight line between 2 anchor points
* @param nextAnchor the next anchor point
* @return a CurveBuilderAtAnchor a part of the curve builder system
*/
public CurveBuilderAtAnchor anchorPoint(Vector2f nextAnchor ){
//simulate a straight line using a Bézier curve
Vector2f midOne = currentAnchor.mult(2f/3).add(nextAnchor.mult(1f/3));
Vector2f midTwo = currentAnchor.mult(1f/3).add(nextAnchor.mult(2f/3));
return controlPoint1(midOne).controlPoint2(midTwo).nextAnchor(nextAnchor);
}

public Curve end(){
curveBeingBuilt.addControlPoint(controlPointIn, currentAnchor, null);
return curveBeingBuilt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.epaga.particles.valuetypes.curvebuilder;

import com.epaga.particles.valuetypes.Curve;
import com.jme3.math.Vector2f;

public class CurveBuilderAtControlPoint1{

Curve curveBeingBuilt;

public CurveBuilderAtControlPoint1(Curve curveBeingBuilt, Vector2f controlPointIn, Vector2f currentAnchor, Vector2f controlPointOut){
this.curveBeingBuilt = curveBeingBuilt;
this.curveBeingBuilt.addControlPoint(controlPointIn, currentAnchor, controlPointOut);
}

/**
* Adds a point that the curve will attempt to move towards but may not actually touch.
*
* The 2 control points are used to define a cubic Bézier curve between 2 anchors
* @param nextControlPoint the control point
* @return a CurveBuilderAtControlPoint1 a part of the curve builder system
*/
public CurveBuilderAtControlPoint2 controlPoint2( Vector2f nextControlPoint ){
return new CurveBuilderAtControlPoint2(curveBeingBuilt, nextControlPoint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.epaga.particles.valuetypes.curvebuilder;

import com.epaga.particles.valuetypes.Curve;
import com.jme3.math.Vector2f;

public class CurveBuilderAtControlPoint2{

Curve curveBeingBuilt;
Vector2f inControlPoint;

public CurveBuilderAtControlPoint2(Curve curveBeingBuilt, Vector2f inControlPoint){
this.curveBeingBuilt = curveBeingBuilt;
this.inControlPoint = inControlPoint;
}

/**
* Adds a point that the curve go through.
*
* Anchors are the starts and ends of cubic Bézier curves
* @param nextAnchor the anchor point
* @return a CurveBuilderAtAnchor a part of the curve builder system
*/
public CurveBuilderAtAnchor anchorPoint(Vector2f nextAnchor ){
return new CurveBuilderAtAnchor(curveBeingBuilt, inControlPoint, nextAnchor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.epaga.particles.valuetypes.curvebuilder;

import com.epaga.particles.valuetypes.Curve;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;

public class CurveBuilderStart{

Curve curveBeingBuilt = new Curve();

/**
* Adds the first anchor point, where the line will start
* @return CurveBuilderAtAnchor a part of the curve builder system
*/
public CurveBuilderAtAnchor anchorPoint(Vector2f start){
return new CurveBuilderAtAnchor(curveBeingBuilt, null, start);
}

}

0 comments on commit 3287009

Please sign in to comment.