1/*
2 * Copyright (c) 2009-2010 jMonkeyEngine
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33package com.jme3.collision;
34
35import com.jme3.math.Triangle;
36import com.jme3.math.Vector3f;
37import com.jme3.scene.Geometry;
38import com.jme3.scene.Mesh;
39
40/**
41 * A <code>CollisionResult</code> represents a single collision instance
42 * between two {@link Collidable}. A collision check can result in many
43 * collision instances (places where collision has occured).
44 *
45 * @author Kirill Vainer
46 */
47public class CollisionResult implements Comparable<CollisionResult> {
48
49    private Geometry geometry;
50    private Vector3f contactPoint;
51    private Vector3f contactNormal;
52    private float distance;
53    private int triangleIndex;
54
55    public CollisionResult(Geometry geometry, Vector3f contactPoint, float distance, int triangleIndex) {
56        this.geometry = geometry;
57        this.contactPoint = contactPoint;
58        this.distance = distance;
59        this.triangleIndex = triangleIndex;
60    }
61
62    public CollisionResult(Vector3f contactPoint, float distance) {
63        this.contactPoint = contactPoint;
64        this.distance = distance;
65    }
66
67    public CollisionResult(){
68    }
69
70    public void setGeometry(Geometry geom){
71        this.geometry = geom;
72    }
73
74    public void setContactNormal(Vector3f norm){
75        this.contactNormal = norm;
76    }
77
78    public void setContactPoint(Vector3f point){
79        this.contactPoint = point;
80    }
81
82    public void setDistance(float dist){
83        this.distance = dist;
84    }
85
86    public void setTriangleIndex(int index){
87        this.triangleIndex = index;
88    }
89
90    public Triangle getTriangle(Triangle store){
91        if (store == null)
92            store = new Triangle();
93
94        Mesh m = geometry.getMesh();
95        m.getTriangle(triangleIndex, store);
96        store.calculateCenter();
97        store.calculateNormal();
98        return store;
99    }
100
101    public int compareTo(CollisionResult other) {
102        if (distance < other.distance)
103            return -1;
104        else if (distance > other.distance)
105            return 1;
106        else
107            return 0;
108    }
109
110    @Override
111    public boolean equals(Object obj) {
112        if(obj instanceof CollisionResult){
113            return ((CollisionResult)obj).compareTo(this) == 0;
114        }
115        return super.equals(obj);
116    }
117
118    public Vector3f getContactPoint() {
119        return contactPoint;
120    }
121
122    public Vector3f getContactNormal() {
123        return contactNormal;
124    }
125
126    public float getDistance() {
127        return distance;
128    }
129
130    public Geometry getGeometry() {
131        return geometry;
132    }
133
134    public int getTriangleIndex() {
135        return triangleIndex;
136    }
137
138}
139