public override void Update(float dt, float dtReal) { for (int i = 0; i < _geomList.Count; i++) { _totalArea = _geomList[i].localVertices.GetArea(); //If the AABB of the geometry does not intersect the fluidcontainer //continue to the next geometry if (!_fluidContainer.Intersect(ref _geomList[i].AABB)) { continue; } //Find the vertices contained in the fluidcontainer FindVerticesInFluid(_geomList[i]); //The geometry is not in the fluid, up til a certain point. if (_vertices.Count < _geomList[i].LocalVertices.Count * 0.15f) { _geomInFluidList[_geomList[i]] = false; } _area = _vertices.GetArea(); if (_area < .000001) { continue; } _centroid = _vertices.GetCentroid(_area); //Calculate buoyancy force _buoyancyForce = -_gravity * _area * Density; //Calculate linear and rotational drag CalculateDrag(_geomList[i]); //Add the buoyancy force and lienar drag force Vector2.Add(ref _buoyancyForce, ref _linearDragForce, out _totalForce); //Apply total force to the body _geomList[i].body.ApplyForceAtWorldPoint(ref _totalForce, ref _centroid); //Apply rotational drag _geomList[i].body.ApplyTorque(_rotationalDragTorque); if (_geomInFluidList[_geomList[i]] == false) { //The geometry is now in the water. Fire the Entry event _geomInFluidList[_geomList[i]] = true; if (Entry != null) { Entry(_geomList[i], _vertices); } } } }
public override void Update(float dt) { for (int i = 0; i < _geomList.Count; i++) { _totalArea = _geomList[i].localVertices.GetArea(); if (!_fluidContainer.Intersect(_geomList[i].aabb)) { continue; } FindVerticesInFluid(_geomList[i]); if (_vertices.Count < 3) { continue; } _area = _vertices.GetArea(); if (_area < .000001) { continue; } _centroid = _vertices.GetCentroid(_area); CalculateBuoyancy(); CalculateDrag(_geomList[i]); Vector2.Add(ref _buoyancyForce, ref _linearDragForce, out _totalForce); _geomList[i].body.ApplyForceAtWorldPoint(ref _totalForce, ref _centroid); _geomList[i].body.ApplyTorque(_rotationalDragTorque); if (_geomInFluidList[_geomList[i]] == false) { _geomInFluidList[_geomList[i]] = true; if (Entry != null) { Entry(_geomList[i]); } } } }
public override void Update(float dt) { _fluidContainer.Update(dt); for (int i = 0; i < _geomList.Count; i++) { Fixture fixture = _geomList[i]; Body body = fixture.Body; Vertices localVertices = fixture.Shape.GetVertices(); _totalArea = fixture.Shape.Area; //If the AABB of the geometry does not intersect the fluidcontainer //continue to the next geometry AABB aabb; fixture.Shape.ComputeAABB(out aabb, ref fixture.Body._xf, 0); if (!_fluidContainer.Intersect(ref aabb)) { continue; } //Find the vertices contained in the fluidcontainer _vertices.Clear(); for (int k = 0; k < localVertices.Count; k++) { _vert = fixture.Body.GetWorldPoint(localVertices[k]); if (_fluidContainer.Contains(ref _vert)) { _vertices.Add(_vert); } } //The geometry is not in the fluid, up til a certain point. if (_vertices.Count < localVertices.Count * 0.15f) { _geomInFluidList[fixture] = false; } _area = _vertices.GetArea(); if (_area < .0001) { continue; } _centroid = _vertices.GetCentroid(); //Calculate buoyancy force _buoyancyForce = -_gravity * (_area * fixture.Shape.Density) * Density; //Calculate linear and rotational drag _centroidVelocity = fixture.Body.GetLinearVelocityFromWorldPoint(_centroid); _axis.X = -_centroidVelocity.Y; _axis.Y = _centroidVelocity.X; //can't normalize a zero length vector if (_axis.X != 0 || _axis.Y != 0) { _axis.Normalize(); } _vertices.ProjectToAxis(ref _axis, out _min, out _max); _dragArea = Math.Abs(_max - _min); _partialMass = fixture.Body.Mass * (_area / _totalArea); _linearDragForce = -.5f * Density * _dragArea * LinearDragCoefficient * _partialMass * _centroidVelocity; _rotationalDragTorque = -fixture.Body.AngularVelocity * AngularDragCoefficient * _partialMass; //Add the buoyancy force and lienar drag force Vector2.Add(ref _buoyancyForce, ref _linearDragForce, out _totalForce); //Apply total force to the body body.ApplyForce(ref _totalForce); //Apply rotational drag body.ApplyTorque(_rotationalDragTorque); if (_geomInFluidList[_geomList[i]] == false) { //The geometry is now in the water. Fire the Entry event _geomInFluidList[_geomList[i]] = true; if (Entry != null) { Entry(_geomList[i], _vertices); } } } }