• Advertisement
Sign in to follow this  

Cubic Splines!

This topic is 4215 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Iam trying to implement dead reckoning with Cubic splines and when I ran my code to see what kind of output the class would give me, I couldnt really tell if my code is working right or wrong. So can anyone tell me if this is the kind of output I should get for a spline of Constant velocity of 1unit; Starting Pos = (0, 0, 0); Starting angle (Direction of velocity) of 60degrees Ending Pos = (5, 5, 0); Ending angle (Direction of velocity) of 135degrees I have the code continue after the end position but the 2 turning points will show where its start position and end position are. and here is my code for the Cubic Spline
[Source="java"]
package sphere;


public class CubicSpline 
{
    float[] start = new float[3]; // The starting position
    float[] end = new float[3]; // The Ending position
    
    float[] sVel = new float[3]; // The velocity from the start position
    float[] eVel = new float[3]; // The velocity from the end position
    
    float[][] v = new float[3][4]; // holds coefficients for position getting
    public CubicSpline()
    {
        
    }
    
    public void init(float[] _start, short h_ang, short v_ang, float speed) // must be called to initialize certain variables
    {
        for(int i=0; i < 3; i++)
            start = _start;

        double a = Math.toRadians((h_ang%512)); // converts the horizontal angle to radians
        double b = Math.toRadians((v_ang%512)); // converts the verticle angle to radians
        System.out.println("ang = (" + Math.toDegrees(a) + ", " + Math.toDegrees(b) + ")");
        // We have to calculate the end velocity with the newly arrived info
        sVel[0] = (float)(Math.sin(a)*Math.cos(b))*speed; // X velocity
//        sVel[1] = (float)(Math.sin(a)*speed);
        sVel[1] = (float)(Math.sin(a)*Math.sin(b))*speed; // Z Velocity in 3D space (!Y Velocity in 2D Space)!
        sVel[2] = (float)(Math.cos(a))*speed; // Y Velocity (!only in 3D space will this be used)!
        System.out.println("(" + sVel[0] + ", " + sVel[1] + ", " + sVel[2] + ")");

    }
    
    public void getVel(short h_ang, short v_ang, float speed)
    {
        double a = Math.toRadians((h_ang%512)); // converts the horizontal angle to radians
        double b = Math.toRadians((v_ang%512)); // converts the verticle angle to radians
        // We have to calculate the end velocity with the newly arrived info
        eVel[0] = (float)(Math.sin(a)*Math.cos(b))*speed; // X velocity
//        eVel[1] = (float)(Math.sin(a)*speed);
        eVel[1] = (float)(Math.sin(a)*Math.sin(b))*speed; // Z Velocity in 3D space (!Y Velocity in 2D Space)!
        eVel[2] = (float)(Math.cos(a))*speed; // Y Velocity (!only in 3D space will this be used)!
        // Calculations done
        
        
    }
    
    public void setPos(float x, float y, float z)
    {
        // for 3D space the y has to be the z ex:  setPos(x, z, y);
        end[0] = x;
        end[1] = y;
        end[2] = z;
    }
    
    public void calcCoefficients(float tspan)
    {
        float[] X = new float[4];
        float[] Y = new float[4];
        float[] Z = new float[4];
        
        // first coordinate for the starting position
        // Second coordinate for the starting position + velocity * tspan
        // Third Coordinate for Position after (tspan) using reversed ending velocity
        // forth ending position;
        
        X[0] = start[0];    X[1] = start[0] + (sVel[0]);      X[2] = end[0] + (eVel[0]*tspan);   X[3] = X[2]-(eVel[0]);    
        Y[0] = start[1];    Y[1] = start[1] + (sVel[1]);      Y[2] = end[1] + (eVel[1]*tspan);   Y[3] = Y[2]-(eVel[1]); 
        Z[0] = start[2];    Z[1] = start[2] + (sVel[2]);      Z[2] = end[2] + (eVel[2]*tspan);   Z[3] = Z[2]-(eVel[2]);
        
        v[0] = getCoefficients(X);
        v[1] = getCoefficients(Y);
        v[2] = getCoefficients(Z);
    }
    
    public float[] getPos(float t)
    {
        float[] ret = new float[3];
        
        ret[0] = v[0][0]*(t*t*t) + v[0][1]*(t*t) + v[0][2]*t + v[0][3];
        ret[1] = v[1][0]*(t*t*t) + v[1][1]*(t*t) + v[1][2]*t + v[1][3];
        ret[2] = v[2][0]*(t*t*t) + v[2][1]*(t*t) + v[2][2]*t + v[2][3];

        System.out.println("(" + ret[0] + ", "+ ret[1] + ", " + ret[2] + ")");
        
/*        try
        {
            Thread.sleep(1000);
        }
        catch(Exception e)
        {
        }*/

        return ret;
    }
    
    private float[] getCoefficients(float[] points)
    {
        float[] ret = new float[4];
        ret[0] = points[3] - (3*points[2]) + (3*points[1]) - points[0];
        ret[1] = (3*points[2]) - (6*points[1]) + (3*points[0]);
        ret[2] = (3*points[1]) - (3*points[0]);
        ret[3] = points[0];
        return ret;
    }
    
    public void swap() // call this so the velocitys swap so we can calculate the spline with the new data
    {
        for(int i=0; i < 3; i++)
        {
            start = end;
            sVel = eVel;
        }
    }
    
    
    
    
}
[/Source]
[/source][/source] I dont know if it makes a difference but in the tutorial I learned this from they had acceleration but in my game there is no need for acceleration so I dont use it in my code. Tell me if I did anything wrong or if everything is fine THANK YOU!. -Edit Also a working example of a application that succesfully implemented cubic splines will also be helpful with the source code if possible

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement