1 | package com.framsticks.net.client3D;
|
---|
2 |
|
---|
3 | import pl.vorg.mowa.core.graphics.BoundingBox;
|
---|
4 | import pl.vorg.mowa.core.graphics.Vec3;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * Framstick creature body class.
|
---|
8 | * @author MoMaT, modified by Vorg @ 2007-01-01
|
---|
9 | */
|
---|
10 | public class Creature {
|
---|
11 | private String name;
|
---|
12 | private String genotype;
|
---|
13 |
|
---|
14 | public static enum ModelType {
|
---|
15 | Parts,
|
---|
16 | MechParts
|
---|
17 | }
|
---|
18 |
|
---|
19 | private int[][] joints = null;
|
---|
20 | private float[][] parts = null;
|
---|
21 | private float[][] mechParts = null;
|
---|
22 | private NeuroDef[] neuroDefs = null;
|
---|
23 | private BoundingBox boundingBox;
|
---|
24 | private BoundingBox mechBoundingBox;
|
---|
25 |
|
---|
26 | private int group;
|
---|
27 | private int index;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Class constructor. Initializes data structure.
|
---|
31 | * @param name
|
---|
32 | * @param partsCount number of parts
|
---|
33 | * @param jointsCount number of joints
|
---|
34 | * @param group creature group
|
---|
35 | * @param index position in a group
|
---|
36 | */
|
---|
37 | public Creature(String name, int group, int index) {
|
---|
38 | this.name = name;
|
---|
39 | this.group = group;
|
---|
40 | this.index = index;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public String toString() {
|
---|
44 | return "/" + group + "/" + index + "/" + name;
|
---|
45 | }
|
---|
46 |
|
---|
47 | public String getName() {
|
---|
48 | return name;
|
---|
49 | }
|
---|
50 |
|
---|
51 | public int getGroup() {
|
---|
52 | return group;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public int getIndex() {
|
---|
56 | return index;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public String getGenotype() {
|
---|
60 | return genotype;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void setGenotype(String genotype) {
|
---|
64 | this.genotype = genotype;
|
---|
65 | }
|
---|
66 |
|
---|
67 | public int[][] getJoints() {
|
---|
68 | return joints;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public void setJoints(int[][] joints) {
|
---|
72 | this.joints = joints;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public float[][] getParts(ModelType modelType) {
|
---|
76 | return (modelType == ModelType.Parts) ? parts : mechParts;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void setParts(float[][] value, ModelType modelType) {
|
---|
80 | if (modelType == ModelType.Parts)
|
---|
81 | parts = value;
|
---|
82 | else {
|
---|
83 | mechParts = value;
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | public void setNeuroDefs(NeuroDef[] defs)
|
---|
88 | { neuroDefs=defs; }
|
---|
89 |
|
---|
90 | public NeuroDef[] getNeuroDefs()
|
---|
91 | { return neuroDefs; }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Gets translation vector to the middle point of a joint.
|
---|
95 | * @param index index of joint in a joints array
|
---|
96 | * @return translation vector
|
---|
97 | */
|
---|
98 | public float[] jointTranslation(int index, ModelType modelType) {
|
---|
99 | float[][] arr = (modelType == ModelType.Parts) ? parts : mechParts;
|
---|
100 | Vec3 p1 = new Vec3(arr[joints[index][0]]);
|
---|
101 | Vec3 p2 = new Vec3(arr[joints[index][1]]);
|
---|
102 | Vec3 vector = Vec3.sub(p2, p1);
|
---|
103 |
|
---|
104 | vector.mul(0.5f);
|
---|
105 | p1.add(vector);
|
---|
106 | return p1.getVector();
|
---|
107 | }
|
---|
108 | /**
|
---|
109 | * Calculates the length of vector between two joint points.
|
---|
110 | * @param index index of joint in a joints array
|
---|
111 | * @return length of vector
|
---|
112 | */
|
---|
113 | public float jointLength(int index, ModelType modelType) {
|
---|
114 | float[][] arr = (modelType == ModelType.Parts) ? parts : mechParts;
|
---|
115 | Vec3 p1 = new Vec3(arr[joints[index][0]]);
|
---|
116 | Vec3 p2 = new Vec3(arr[joints[index][1]]);
|
---|
117 | Vec3 vector = Vec3.sub(p2, p1);
|
---|
118 |
|
---|
119 | return vector.length();
|
---|
120 | }
|
---|
121 | /**
|
---|
122 | * Calculates the angle and the rotation axis between a joint and the base vector.
|
---|
123 | * @param index index of joint in a joints array
|
---|
124 | * @return the array of angle and axis x, y, z coordinates
|
---|
125 | */
|
---|
126 | public float[] jointRotation(int index, ModelType modelType) {
|
---|
127 | float[][] arr = (modelType == ModelType.Parts) ? parts : mechParts;
|
---|
128 | Vec3 p1 = new Vec3(arr[joints[index][0]]);
|
---|
129 | Vec3 p2 = new Vec3(arr[joints[index][1]]);
|
---|
130 | //Vec3 vector = p1.vector(p2);
|
---|
131 | Vec3 vector = Vec3.sub(p2, p1);
|
---|
132 | Vec3 base = new Vec3(0, 0, 1);
|
---|
133 |
|
---|
134 | float cos = Vec3.dot(vector, base)/vector.length();
|
---|
135 | float angle = (float)(-Math.acos(cos)*180/Math.PI);
|
---|
136 |
|
---|
137 | Vec3 cross = Vec3.cross(vector,base);
|
---|
138 | float[] v = cross.getVector();
|
---|
139 | float[] result = new float[4];
|
---|
140 | result[0] = angle;
|
---|
141 | result[1] = v[0];
|
---|
142 | result[2] = v[1];
|
---|
143 | result[3] = v[2];
|
---|
144 | return result;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Calculates boundingBox of creature's model
|
---|
149 | * @author vorg
|
---|
150 | * @param modelType
|
---|
151 | * @return boudingBox
|
---|
152 | */
|
---|
153 | public BoundingBox getBoundingBox(ModelType modelType) {
|
---|
154 | BoundingBox bbox = (modelType == ModelType.Parts) ? boundingBox : mechBoundingBox;
|
---|
155 | if (bbox == null) {
|
---|
156 | bbox = new BoundingBox();
|
---|
157 | float[][] arr = (modelType == ModelType.Parts) ? parts : mechParts;
|
---|
158 | for(int i=0; i<arr.length; i++) {
|
---|
159 | bbox.add(arr[i][0], arr[i][1], arr[i][2]);
|
---|
160 | }
|
---|
161 | if (modelType == ModelType.Parts) boundingBox = bbox;
|
---|
162 | else mechBoundingBox = bbox;
|
---|
163 | }
|
---|
164 | return bbox;
|
---|
165 | }
|
---|
166 | }
|
---|