1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.math.collision; 18 19 import java.io.Serializable; 20 21 import com.badlogic.gdx.math.MathUtils; 22 import com.badlogic.gdx.math.Vector3; 23 import com.badlogic.gdx.utils.NumberUtils; 24 25 /** Encapsulates a 3D sphere with a center and a radius 26 * 27 * @author badlogicgames@gmail.com */ 28 public class Sphere implements Serializable { 29 private static final long serialVersionUID = -6487336868908521596L; 30 /** the radius of the sphere **/ 31 public float radius; 32 /** the center of the sphere **/ 33 public final Vector3 center; 34 35 private static final float PI_4_3 = MathUtils.PI * 4f / 3f; 36 37 /** Constructs a sphere with the given center and radius 38 * @param center The center 39 * @param radius The radius */ Sphere(Vector3 center, float radius)40 public Sphere (Vector3 center, float radius) { 41 this.center = new Vector3(center); 42 this.radius = radius; 43 } 44 45 /** @param sphere the other sphere 46 * @return whether this and the other sphere overlap */ overlaps(Sphere sphere)47 public boolean overlaps (Sphere sphere) { 48 return center.dst2(sphere.center) < (radius + sphere.radius) * (radius + sphere.radius); 49 } 50 51 @Override hashCode()52 public int hashCode () { 53 final int prime = 71; 54 int result = 1; 55 result = prime * result + this.center.hashCode(); 56 result = prime * result + NumberUtils.floatToRawIntBits(this.radius); 57 return result; 58 } 59 60 @Override equals(Object o)61 public boolean equals (Object o) { 62 if (this == o) return true; 63 if (o == null || o.getClass() != this.getClass()) return false; 64 Sphere s = (Sphere)o; 65 return this.radius == s.radius && this.center.equals(s.center); 66 } 67 volume()68 public float volume () { 69 return PI_4_3 * this.radius * this.radius * this.radius; 70 } 71 surfaceArea()72 public float surfaceArea () { 73 return 4 * MathUtils.PI * this.radius * this.radius; 74 } 75 } 76