AddBody adds a body to the simulation.
(body *object.Body, name string)
| 111 | |
| 112 | // AddBody adds a body to the simulation. |
| 113 | func (s *Simulation) AddBody(body *object.Body, name string) { |
| 114 | |
| 115 | // Do nothing if body is already present |
| 116 | for _, existingBody := range s.bodies { |
| 117 | if existingBody == body { |
| 118 | return // Do nothing |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // If there are any open/nil spots in the body slice - add the body to one of them |
| 123 | // Else, just append to the end of the slice. Either way compute the body's index in the slice. |
| 124 | var idx int |
| 125 | nilLen := len(s.nilBodies) |
| 126 | if nilLen > 0 { |
| 127 | idx = s.nilBodies[nilLen] |
| 128 | s.nilBodies = s.nilBodies[0:nilLen-1] |
| 129 | } else { |
| 130 | idx = len(s.bodies) |
| 131 | s.bodies = append(s.bodies, body) |
| 132 | |
| 133 | // Initialize collision matrix values up to the current index (and set the colliding flag to false) |
| 134 | s.collisionMatrix.Set(idx, idx, false) |
| 135 | s.prevCollisionMatrix.Set(idx, idx, false) |
| 136 | } |
| 137 | |
| 138 | body.SetIndex(idx) |
| 139 | body.SetName(name) |
| 140 | |
| 141 | // TODO dispatch add-body event |
| 142 | //s.Dispatch(AddBodyEvent, BodyEvent{body}) |
| 143 | } |
| 144 | |
| 145 | // RemoveBody removes the specified body from the simulation. |
| 146 | // Returns true if found, false otherwise. |