Potential change for PhysicalChaseCamera

I was looking at implementing collision with a ChaseCamera using this class here:
https://gist.github.com/erlend-sh/4252435

I ran into some troubles casting a List to a LinkedList in the “collide” method

 public void collide() {
        LinkedList<PhysicsRayTestResult> testResults;
        testResults = (LinkedList) myPhysicsSpace.rayTest(target.getWorldTranslation(), maxPos);
        float hitFraction = 1f;
        if(testResults != null && testResults.size() > 0) {
            hitFraction = testResults.getFirst().getHitFraction();
        }
        targetDistance = ((float)((int)(hitFraction*100)))/100 * maxDistanceByZoom;
    }

All that was needed to fix the problem was to use ArrayList instead of LinkedList and get the 0th index of the List when setting the hitFraction variable.

public void collide() {
        ArrayList<PhysicsRayTestResult> testResults;
        testResults =  (ArrayList) myPhysicsSpace.rayTest(target.getWorldTranslation(), maxPos);
        float hitFraction = 1f;
        if(testResults != null && testResults.size() > 0) {
            hitFraction = testResults.get(0).getHitFraction();
        }
        targetDistance = ((float)((int)(hitFraction*100)))/100 * maxDistanceByZoom;
    }

Anyone have any idea why it used LinkedList in the first place?

Edit: If anyone is looking at using this, and is having troubles with the PhysicalChaseCamera just barely clipping through terrain; I changed

maxPos = new Vector3f(hDistance * FastMath.cos(rotation), (maxDistanceByZoom) * FastMath.sin(vRotation), hDistance * FastMath.sin(rotation));

to

 maxPos = new Vector3f(hDistance * FastMath.cos(rotation), (maxDistanceByZoom) * FastMath.sin(vRotation-.3f), hDistance * FastMath.sin(rotation));

(The change is in the vRotation) Seems to be working much nicer now.

It’s either wrong or the rayTest used LinkedList before. For cases like this, it’s often better to use the interface List type instead of the concrete LinkedList or ArrayList implementations. As long as you don’t need implementation specific functionality (99% of cases), you totally avoid these sorts of errors.

2 Likes