If you think the Android project clash_of_balls listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* Copyright (C) 2012-2013 Hans Hardmeier <hanshardmeier@gmail.com>
* Copyright (C) 2012-2013 Andrin Jenal//www.java2s.com
* Copyright (C) 2012-2013 Beat Kng <beat-kueng@gmx.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/package com.sapos_aplastados.game.clash_of_balls.game;
import android.util.FloatMath;
publicclass Vector3D {
publicfloat x,y,z;
public Vector3D() {
x = 0.0f;
y = 0.0f;
z = 0.0f;
}
public Vector3D(float fx, float fy, float fz) {
x = fx;
y = fy;
z = fz;
}
public Vector3D(Vector3D src) {
x = src.x;
y = src.y;
z = src.z;
}
publicvoid set(Vector3D src) {
x = src.x;
y = src.y;
z = src.z;
}
publicvoid set(float fx, float fy, float fz) {
x = fx;
y = fy;
z = fz;
}
publicfloat length() {
return FloatMath.sqrt((x * x) + (y * y) + (z * z));
}
publicfloat lengthSquared() {
return (x * x) + (y * y) + (z * z);
}
publicvoid normalize() {
float len = length();
if (len != 0.0f) {
x /= len;
y /= len;
z /= len;
}
}
publicvoid add(Vector3D vector) {
x += vector.x;
y += vector.y;
z += vector.z;
}
publicvoid add(float fx, float fy, float fz) {
x += fx;
y += fy;
z += fz;
}
publicvoid sub(Vector3D vector) {
x -= vector.x;
y -= vector.y;
z -= vector.z;
}
publicvoid mul(Vector3D vector) {
x *= vector.x;
y *= vector.y;
z *= vector.z;
}
publicvoid div(Vector3D vector) {
x /= vector.x;
y /= vector.y;
z /= vector.z;
}
publicvoid mul(float scalar) {
x *= scalar;
y *= scalar;
z *= scalar;
}
publicvoid cross(Vector3D v) {
float cx = y*v.z - z*v.y;
float cy = z*v.x - x*v.z;
float cz = x*v.y - y*v.x;
x=cx;
y=cy;
z=cz;
}
publicvoid dot(Vector3D v) {
x*=v.x;
y*=v.y;
z*=v.z;
}
//angle in radians, range [0,pi]
publicfloat angle(Vector3D v) {
float l1 = length();
float l2 = v.length();
if(l1 == 0.f || l2 == 0.f) return 0.f;
return (float)Math.acos((x*v.x + y*v.y + z*v.z) / l1 / l2);
}
//rotate this vector around vector n, by angle radians
//n will be normalized
publicvoid rotate(Vector3D n, float angle) {
float ca=FloatMath.cos(angle);
float sa=FloatMath.sin(angle);
float len=n.length();
if(len==0.0f || angle==0.f) return;
n.mul(1.f/len);
Vector3D v = n;
//x
float rx = x*(ca+v.x*v.x*(1.0f-ca))+
y*(v.x*v.y*(1.0f-ca)-v.z*sa)+
z*(v.x*v.z*(1.0f-ca)+v.y*sa);
//y
float ry = x*(v.x*v.y*(1.0f-ca)+v.z*sa)+
y*(ca+v.y*v.y*(1.0f-ca))+
z*(v.y*v.z*(1.0f-ca)-v.x*sa);
//z
float rz = x*(v.x*v.z*(1.0f-ca)-v.y*sa)+
y*(v.y*v.z*(1.0f-ca)+v.x*sa)+
z*(ca+v.z*v.z*(1.0f-ca));
x = rx;
y = ry;
z = rz;
}
}