OSDN Git Service

Set optimal mime types and executable settings.
[mikumikustudio/MikuMikuStudio.git] / src / com / jme / math / spring / SpringPoint.java
1 /*
2  * Copyright (c) 2003-2009 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
33 package com.jme.math.spring;
34
35 import java.io.IOException;
36
37 import com.jme.math.Vector3f;
38 import com.jme.util.export.InputCapsule;
39 import com.jme.util.export.JMEExporter;
40 import com.jme.util.export.JMEImporter;
41 import com.jme.util.export.OutputCapsule;
42 import com.jme.util.export.Savable;
43
44 /**
45  * <code>SpringPoint</code> defines a single point in a SpringSystem.
46  * @author Joshua Slack
47  * @version $Id: SpringPoint.java,v 1.5 2007/09/21 15:45:33 nca Exp $
48  */
49 public class SpringPoint implements Savable {
50
51         /**
52          * index of this point in the system.  Needs to be set by the programmer.
53          * can be useful for derivatives of SpringpointForce that may apply force
54          * differently to different points based on location in the matrix.
55          */
56         public int index = 0;
57         /** Mass of this point. */
58         public float mass = 1;
59         /** Inverse Mass of this point. */
60         public float invMass = 1;
61         /** Position of this point in space. */
62         public Vector3f position;
63         /** Previous Position of this point in space. */
64         public Vector3f oldPos;
65         /** Acceleration vector, zeroed and recalculated on each SpringSystem.calcForces(float). */
66         public Vector3f acceleration;
67
68         /**
69          * Public constructor.
70          * @param pos Vertex position of this point.
71          */
72         public SpringPoint(Vector3f pos) {
73                 position = pos;
74                 oldPos = new Vector3f(pos);
75                 acceleration = new Vector3f(0, 0, 0);
76         }
77
78         /**
79          * Set the mass for this point.  Also calculates and stores the inverse
80          * mass to invMass field for future use.
81          * @param m float
82          */
83         public void setMass(float m) {
84                 mass = m;
85                 if (m == Float.POSITIVE_INFINITY || m == Float.NEGATIVE_INFINITY)
86                         invMass = 0;
87                 else if (m == 0)
88                         invMass = Float.POSITIVE_INFINITY;
89                 else
90                         invMass = 1f / m;
91         }
92
93         /**
94          * Verlet update of point location.  Pretty stable.  Updates position
95          * by using implied velocity derived from the distance travled since
96          * last update.  Thus velocity and position do not get out of sync.
97          * @param dt float - change in time since last update.
98          */
99         public void update(float dt) {
100                 float dtSquared = dt * dt;
101                 if (invMass == 0) return;
102                 float x = position.x, y = position.y, z = position.z;
103                 position.set(
104                                 2*position.x - oldPos.x + acceleration.x * dtSquared,
105                                 2*position.y - oldPos.y + acceleration.y * dtSquared,
106                                 2*position.z - oldPos.z + acceleration.z * dtSquared);
107                 oldPos.set(x, y, z);
108         }
109
110     public void write(JMEExporter e) throws IOException {
111         OutputCapsule capsule = e.getCapsule(this);
112         capsule.write(index, "index", 0);
113         capsule.write(mass, "mass", 1);
114         capsule.write(position, "position", Vector3f.ZERO);
115         capsule.write(acceleration, "acceleration", Vector3f.ZERO);
116         
117     }
118
119     public void read(JMEImporter e) throws IOException {
120         InputCapsule capsule = e.getCapsule(this);
121         index = capsule.readInt("index", 0);
122         mass = capsule.readFloat("mass", 1);
123         invMass = 1f / mass;
124         position = (Vector3f)capsule.readSavable("position", Vector3f.ZERO.clone());
125         acceleration = (Vector3f)capsule.readSavable("acceleration", Vector3f.ZERO.clone());
126     }
127     
128     public Class getClassTag() {
129         return this.getClass();
130     }
131 }