-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathBSoftBody.cs
More file actions
142 lines (121 loc) · 4.09 KB
/
BSoftBody.cs
File metadata and controls
142 lines (121 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using UnityEngine;
using System.Collections;
using BulletSharp.SoftBody;
using System;
using BulletSharp;
using System.Collections.Generic;
//using BulletSharp.Math;
namespace BulletUnity
{
public class BSoftBody : BCollisionObject, IDisposable
{
//common Soft body settings class used for all softbodies, parameters set based on type of soft body
[SerializeField]
private SBSettings _softBodySettings = new SBSettings(); //SoftBodyEditor will display this when needed
public SBSettings SoftBodySettings
{
get { return _softBodySettings; }
set { _softBodySettings = value; }
}
//protected SoftBody m_BSoftBody;
SoftRigidDynamicsWorld _world;
protected SoftRigidDynamicsWorld World
{
get {
if (_world != null) {
return _world;
} else {
BPhysicsWorld w = BPhysicsWorld.Get();
if (w == null) {
return null;
} else if (w.world is SoftRigidDynamicsWorld)
{
_world = (SoftRigidDynamicsWorld)w.world;
return _world;
} else
{
return null;
}
}
}
}
//for converting to/from unity mesh
protected UnityEngine.Vector3[] verts = new UnityEngine.Vector3[0];
protected UnityEngine.Vector3[] norms = new UnityEngine.Vector3[0];
protected int[] tris = new int[1];
protected override void Awake()
{
//disable warning
}
protected override void AddObjectToBulletWorld()
{
BPhysicsWorld.Get().AddSoftBody(this);
}
protected override void RemoveObjectFromBulletWorld()
{
BPhysicsWorld world = BPhysicsWorld.Get();
if (world && isInWorld)
{
world.RemoveSoftBody((SoftBody)m_collisionObject);
}
}
public void BuildSoftBody()
{
_BuildCollisionObject();
}
protected override void Dispose(bool isdisposing)
{
SoftBody m_BSoftBody = (SoftBody)m_collisionObject;
if (isInWorld && isdisposing && m_BSoftBody != null)
{
if (m_BSoftBody != null)
{
World.RemoveSoftBody(m_BSoftBody);
}
}
if (m_BSoftBody != null)
{
m_BSoftBody.Dispose();
m_BSoftBody = null;
}
Debug.Log("Destroying SoftBody " + name);
}
public void DumpDataFromBullet()
{
if (isInWorld)
{
SoftBody m_BSoftBody = (SoftBody)m_collisionObject;
int nodeCount = m_BSoftBody.Nodes.Count;
if (verts.Length != nodeCount)
{
verts = new Vector3[nodeCount];
}
if (norms.Length != verts.Length)
{
norms = new Vector3[nodeCount];
}
for (int i = 0; i < nodeCount; i++)
{
BulletSharp.Math.Vector3 pos;
Node.btSoftBody_Node_getX(m_BSoftBody.Nodes.GetNodePointer(i), out pos);
verts[i] = pos.ToUnity();
BulletSharp.Math.Vector3 normal;
Node.btSoftBody_Node_getN(m_BSoftBody.Nodes.GetNodePointer(i), out normal);
norms[i] = normal.ToUnity();
}
}
}
public virtual void Update()
{
DumpDataFromBullet(); //Get Bullet data
UpdateMesh(); //Update mesh based on bullet data
//Make coffee
}
/// <summary>
/// Update Mesh (or line renderer) at runtime, call from Update
/// </summary>
public virtual void UpdateMesh()
{
}
}
}