• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.jbox2d.particle;
2 
3 import org.jbox2d.common.Transform;
4 import org.jbox2d.common.Vec2;
5 
6 public class ParticleGroup {
7 
8   ParticleSystem m_system;
9   int m_firstIndex;
10   int m_lastIndex;
11   int m_groupFlags;
12   float m_strength;
13   ParticleGroup m_prev;
14   ParticleGroup m_next;
15 
16   int m_timestamp;
17   float m_mass;
18   float m_inertia;
19   final Vec2 m_center = new Vec2();
20   final Vec2 m_linearVelocity = new Vec2();
21   float m_angularVelocity;
22   final Transform m_transform = new Transform();
23 
24   boolean m_destroyAutomatically;
25   boolean m_toBeDestroyed;
26   boolean m_toBeSplit;
27 
28   Object m_userData;
29 
ParticleGroup()30   public ParticleGroup() {
31     // m_system = null;
32     m_firstIndex = 0;
33     m_lastIndex = 0;
34     m_groupFlags = 0;
35     m_strength = 1.0f;
36 
37     m_timestamp = -1;
38     m_mass = 0;
39     m_inertia = 0;
40     m_angularVelocity = 0;
41     m_transform.setIdentity();
42 
43     m_destroyAutomatically = true;
44     m_toBeDestroyed = false;
45     m_toBeSplit = false;
46   }
47 
getNext()48   public ParticleGroup getNext() {
49     return m_next;
50   }
51 
getParticleCount()52   public int getParticleCount() {
53     return m_lastIndex - m_firstIndex;
54   }
55 
getBufferIndex()56   public int getBufferIndex() {
57     return m_firstIndex;
58   }
59 
getGroupFlags()60   public int getGroupFlags() {
61     return m_groupFlags;
62   }
63 
setGroupFlags(int flags)64   public void setGroupFlags(int flags) {
65     m_groupFlags = flags;
66   }
67 
getMass()68   public float getMass() {
69     updateStatistics();
70     return m_mass;
71   }
72 
getInertia()73   public float getInertia() {
74     updateStatistics();
75     return m_inertia;
76   }
77 
getCenter()78   public Vec2 getCenter() {
79     updateStatistics();
80     return m_center;
81   }
82 
getLinearVelocity()83   public Vec2 getLinearVelocity() {
84     updateStatistics();
85     return m_linearVelocity;
86   }
87 
getAngularVelocity()88   public float getAngularVelocity() {
89     updateStatistics();
90     return m_angularVelocity;
91   }
92 
getTransform()93   public Transform getTransform() {
94     return m_transform;
95   }
96 
getPosition()97   public Vec2 getPosition() {
98     return m_transform.p;
99   }
100 
getAngle()101   public float getAngle() {
102     return m_transform.q.getAngle();
103   }
104 
getUserData()105   public Object getUserData() {
106     return m_userData;
107   }
108 
setUserData(Object data)109   public void setUserData(Object data) {
110     m_userData = data;
111   }
112 
113 
114 
updateStatistics()115   public void updateStatistics() {
116     if (m_timestamp != m_system.m_timestamp) {
117       float m = m_system.getParticleMass();
118       m_mass = 0;
119       m_center.setZero();
120       m_linearVelocity.setZero();
121       for (int i = m_firstIndex; i < m_lastIndex; i++) {
122         m_mass += m;
123         Vec2 pos = m_system.m_positionBuffer.data[i];
124         m_center.x += m * pos.x;
125         m_center.y += m * pos.y;
126         Vec2 vel = m_system.m_velocityBuffer.data[i];
127         m_linearVelocity.x += m * vel.x;
128         m_linearVelocity.y += m * vel.y;
129       }
130       if (m_mass > 0) {
131         m_center.x *= 1 / m_mass;
132         m_center.y *= 1 / m_mass;
133         m_linearVelocity.x *= 1 / m_mass;
134         m_linearVelocity.y *= 1 / m_mass;
135       }
136       m_inertia = 0;
137       m_angularVelocity = 0;
138       for (int i = m_firstIndex; i < m_lastIndex; i++) {
139         Vec2 pos = m_system.m_positionBuffer.data[i];
140         Vec2 vel = m_system.m_velocityBuffer.data[i];
141         float px = pos.x - m_center.x;
142         float py = pos.y - m_center.y;
143         float vx = vel.x - m_linearVelocity.x;
144         float vy = vel.y - m_linearVelocity.y;
145         m_inertia += m * (px * px + py * py);
146         m_angularVelocity += m * (px * vy - py * vx);
147       }
148       if (m_inertia > 0) {
149         m_angularVelocity *= 1 / m_inertia;
150       }
151       m_timestamp = m_system.m_timestamp;
152     }
153   }
154 }
155