Hit fraction javadoc

Here is a propsed patch.

I added some documentation for the public float getHitFraction()

I noticed that class PhysicsRayTestResult and class PhysicsSweepTestResult were identical so I created an abstract parent class and pulled up methods and members.



edit: changed the markup to ‘patch’.



[patch]

This patch file was generated by NetBeans IDE

Index: AbstractTestResult.java

— AbstractTestResult.java Locally New

+++ AbstractTestResult.java Locally New

@@ -0,0 +1,92 @@

+/*

    • Copyright © 2009-2012 jMonkeyEngine
    • All rights reserved.
  • *
    • Redistribution and use in source and binary forms, with or without
    • modification, are permitted provided that the following conditions are
    • met:
  • *
      • Redistributions of source code must retain the above copyright
    • notice, this list of conditions and the following disclaimer.
  • *
      • Redistributions in binary form must reproduce the above copyright
    • notice, this list of conditions and the following disclaimer in the
    • documentation and/or other materials provided with the distribution.
  • *
      • Neither the name of ‘jMonkeyEngine’ nor the names of its contributors
    • may be used to endorse or promote products derived from this software
    • without specific prior written permission.
  • *
    • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    • "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
    • TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    • PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
    • CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    • EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    • PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    • PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    • LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    • NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    • SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  • */

    +package com.jme3.bullet.collision;

    +

    +import com.jme3.math.Vector3f;

    +

    +/**
    • Contains the result of a collision sweep (or ray) test.
    • @author Johan Maasing <johan@zoom.nu>
  • */

    +public abstract class AbstractTestResult {

    +
  • private PhysicsCollisionObject collisionObject;
  • private Vector3f hitNormalLocal;
  • private float hitFraction;
  • private boolean normalInWorldSpace;

    +
  • public AbstractTestResult(PhysicsCollisionObject collisionObject, Vector3f hitNormalLocal, float hitFraction, boolean normalInWorldSpace) {
  •    this.collisionObject = collisionObject;<br />
    
  •    this.hitNormalLocal = hitNormalLocal;<br />
    
  •    this.hitFraction = hitFraction;<br />
    
  •    this.normalInWorldSpace = normalInWorldSpace;<br />
    
  • }

    +
  • /**
  • * @return the collisionObject<br />
    
  • */<br />
    
  • public PhysicsCollisionObject getCollisionObject() {
  •    return collisionObject;<br />
    
  • }

    +
  • /**
  • * @return the hitNormalLocal<br />
    
  • */<br />
    
  • public Vector3f getHitNormalLocal() {
  •    return hitNormalLocal;<br />
    
  • }

    +
  • /**
  • * Get the fraction of the sweep where the collision occured. You can use<br />
    
  • * the hitFraction value to determine on what distance of the given sweep<br />
    
  • * length the collision happened. E.g. when the hitFraction is 0.5 and you<br />
    
  • * sweeped from 0,0,0 to 0,6,0 then the hit position was at 0,3,0<br />
    
  • * @return the hitFraction<br />
    
  • */<br />
    
  • public float getHitFraction() {
  •    return hitFraction;<br />
    
  • }

    +
  • /**
  • * @return the normalInWorldSpace<br />
    
  • */<br />
    
  • public boolean isNormalInWorldSpace() {
  •    return normalInWorldSpace;<br />
    
  • }

    +
  • public void fill(PhysicsCollisionObject collisionObject, Vector3f hitNormalLocal, float hitFraction, boolean normalInWorldSpace) {
  •    this.collisionObject = collisionObject;<br />
    
  •    this.hitNormalLocal = hitNormalLocal;<br />
    
  •    this.hitFraction = hitFraction;<br />
    
  •    this.normalInWorldSpace = normalInWorldSpace;<br />
    
  • }

    +}

    Index: PhysicsRayTestResult.java

    — PhysicsRayTestResult.java Base (BASE)

    +++ PhysicsRayTestResult.java Locally Modified (Based On LOCAL)

    @@ -33,59 +33,15 @@



    import com.jme3.math.Vector3f;



    +

    /**
  • Contains the results of a PhysicsSpace rayTest
  • @author normenhansen

    */

    -public class PhysicsRayTestResult {

    +public class PhysicsRayTestResult extends AbstractTestResult {


  • private PhysicsCollisionObject collisionObject;
  • private Vector3f hitNormalLocal;
  • private float hitFraction;
  • private boolean normalInWorldSpace;

    -
  • public PhysicsRayTestResult() {
  • }

    -

    public PhysicsRayTestResult(PhysicsCollisionObject collisionObject, Vector3f hitNormalLocal, float hitFraction, boolean normalInWorldSpace) {
  •    this.collisionObject = collisionObject;<br />
    
  •    this.hitNormalLocal = hitNormalLocal;<br />
    
  •    this.hitFraction = hitFraction;<br />
    
  •    this.normalInWorldSpace = normalInWorldSpace;<br />
    
  •    super(collisionObject, hitNormalLocal, hitFraction, normalInWorldSpace);<br />
    

}


  • /**
  • * @return the collisionObject<br />
    
  • */<br />
    
  • public PhysicsCollisionObject getCollisionObject() {
  •    return collisionObject;<br />
    

}

-

  • /**
  • * @return the hitNormalLocal<br />
    
  • */<br />
    
  • public Vector3f getHitNormalLocal() {
  •    return hitNormalLocal;<br />
    
  • }

    -
  • /**
  • * @return the hitFraction<br />
    
  • */<br />
    
  • public float getHitFraction() {
  •    return hitFraction;<br />
    
  • }

    -
  • /**
  • * @return the normalInWorldSpace<br />
    
  • */<br />
    
  • public boolean isNormalInWorldSpace() {
  •    return normalInWorldSpace;<br />
    
  • }

    -
  • public void fill(PhysicsCollisionObject collisionObject, Vector3f hitNormalLocal, float hitFraction, boolean normalInWorldSpace) {
  •    this.collisionObject = collisionObject;<br />
    
  •    this.hitNormalLocal = hitNormalLocal;<br />
    
  •    this.hitFraction = hitFraction;<br />
    
  •    this.normalInWorldSpace = normalInWorldSpace;<br />
    
  • }

    -}

    Index: PhysicsSweepTestResult.java

    — PhysicsSweepTestResult.java Base (BASE)

    +++ PhysicsSweepTestResult.java Locally Modified (Based On LOCAL)

    @@ -37,55 +37,10 @@
  • Contains the results of a PhysicsSpace rayTest
  • @author normenhansen

    */

    -public class PhysicsSweepTestResult {

    +public class PhysicsSweepTestResult extends AbstractTestResult {


  • private PhysicsCollisionObject collisionObject;
  • private Vector3f hitNormalLocal;
  • private float hitFraction;
  • private boolean normalInWorldSpace;

    -
  • public PhysicsSweepTestResult() {
  • }

    -

    public PhysicsSweepTestResult(PhysicsCollisionObject collisionObject, Vector3f hitNormalLocal, float hitFraction, boolean normalInWorldSpace) {
  •    this.collisionObject = collisionObject;<br />
    
  •    this.hitNormalLocal = hitNormalLocal;<br />
    
  •    this.hitFraction = hitFraction;<br />
    
  •    this.normalInWorldSpace = normalInWorldSpace;<br />
    
  •    super(collisionObject, hitNormalLocal, hitFraction, normalInWorldSpace);<br />
    

}


  • /**
  • * @return the collisionObject<br />
    
  • */<br />
    
  • public PhysicsCollisionObject getCollisionObject() {
  •    return collisionObject;<br />
    

}

-

  • /**
  • * @return the hitNormalLocal<br />
    
  • */<br />
    
  • public Vector3f getHitNormalLocal() {
  •    return hitNormalLocal;<br />
    
  • }

    -
  • /**
  • * @return the hitFraction<br />
    
  • */<br />
    
  • public float getHitFraction() {
  •    return hitFraction;<br />
    
  • }

    -
  • /**
  • * @return the normalInWorldSpace<br />
    
  • */<br />
    
  • public boolean isNormalInWorldSpace() {
  •    return normalInWorldSpace;<br />
    
  • }

    -
  • public void fill(PhysicsCollisionObject collisionObject, Vector3f hitNormalLocal, float hitFraction, boolean normalInWorldSpace) {
  •    this.collisionObject = collisionObject;<br />
    
  •    this.hitNormalLocal = hitNormalLocal;<br />
    
  •    this.hitFraction = hitFraction;<br />
    
  •    this.normalInWorldSpace = normalInWorldSpace;<br />
    
  • }

    -}

    [/patch]
1 Like

Oh yeah, the test also:



edit: changed the markup to ‘patch’



[patch]

This patch file was generated by NetBeans IDE

Index: TestPhysicsRayCast.java

— TestPhysicsRayCast.java Base (BASE)

+++ TestPhysicsRayCast.java Locally Modified (Based On LOCAL)

@@ -8,6 +8,7 @@

import com.jme3.bullet.control.RigidBodyControl;

import com.jme3.bullet.util.CollisionShapeFactory;

import com.jme3.font.BitmapText;

+import com.jme3.math.Vector3f;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;

import java.util.List;

@@ -18,6 +19,7 @@

public class TestPhysicsRayCast extends SimpleApplication {



private BulletAppState bulletAppState = new BulletAppState();

  • private final float RAY_LENGTH = 200f ;



    public static void main(String[] args) {

    new TestPhysicsRayCast().start();

    @@ -37,17 +39,33 @@

    n.getControl(RigidBodyControl.class).setKinematic(true);

    bulletAppState.getPhysicsSpace().add(n);

    rootNode.attachChild(n);

    +

    +

    bulletAppState.getPhysicsSpace().enableDebug(assetManager);

    }



    @Override
  • public void simpleUpdate(float tpf) {
  •    List&lt;PhysicsRayTestResult&gt; rayTest = bulletAppState.getPhysicsSpace().rayTest(cam.getLocation(), cam.getLocation().add(cam.getDirection()));<br />
    
  • public void simpleUpdate(final float tpf) {
  •    final Vector3f from = new Vector3f(cam.getLocation()) ;<br />
    
  •    final Vector3f to = new Vector3f(cam.getDirection().mult(RAY_LENGTH)) ;<br />
    
  •    final List&lt;PhysicsRayTestResult&gt; rayTest = bulletAppState.getPhysicsSpace().rayTest(from, to);<br />
    

if (rayTest.size() > 0) {

  •        PhysicsRayTestResult get = rayTest.get(0);<br />
    
  •        PhysicsCollisionObject collisionObject = get.getCollisionObject();<br />
    
  •        //do stuff<br />
    
  •        fpsText.setText(collisionObject.getUserObject().toString());<br />
    
  •        final PhysicsRayTestResult get = rayTest.get(0);<br />
    
  •        final float hitFraction = get.getHitFraction();<br />
    
  •        final boolean normalInWorldSpace = get.isNormalInWorldSpace();<br />
    
  •        final Vector3f hitNormalLocal = get.getHitNormalLocal();<br />
    
  •        final PhysicsCollisionObject collisionObject = get.getCollisionObject();<br />
    

+

  •        final StringBuilder message = new StringBuilder() ;<br />
    
  •        message.append(&quot;User object: &quot;) ;<br />
    
  •        message.append(collisionObject.getUserObject().toString()) ;<br />
    
  •        message.append(&quot;. Hit fraction: &quot;) ;<br />
    
  •        message.append(hitFraction) ;<br />
    
  •        message.append(&quot; Hit normal: &quot;);<br />
    
  •        message.append(hitNormalLocal) ;<br />
    
  •        message.append(&quot;. In world space: &quot;);<br />
    
  •        message.append(normalInWorldSpace) ;<br />
    
  •        fpsText.setText(message);<br />
    

}

}

[/patch]