Physics: Added a method to Body that resets the applied forces.
[kong.git] / src / net / habraun / kong / physics / Body.scala
blobffb88f3715681d3cb2f84feec3204d0983f1b4f5
1 /*
2 Copyright (c) 2009 Hanno Braun <hanno@habraun.net>
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
19 package net.habraun.kong.physics
23 /**
24 * Models a rigid body.
25 * Body is mostly a data structure with very little logic. It needs to be added to a world in order to do
26 * anything useful.
29 class Body {
31 /**
32 * The body's position in the world, measured in meters.
33 * After creation, a body has an initial position of (0, 0).
34 * Must not be null.
37 private[this] var _position = Vec2D(0, 0)
39 def position = _position
41 def position_=(p: Vec2D) {
42 if (p == null) throw new NullPointerException
43 _position = p
48 /**
49 * The body's shape.
50 * The shape is needed for collision detection.
51 * Must not be null.
54 private[this] var _shape: Shape = NoShape
56 def shape = _shape
58 def shape_=(s: Shape) {
59 if (s == null) throw new NullPointerException
60 _shape = s
65 /**
66 * The body's velocity, measured in meters per second.
67 * Velocity vectors with a size greater than the maximum velocity will be shortened, retaining their
68 * direction.
69 * After creation, a body has an initial velocity of (0, 0).
70 * Must not be null.
73 private[this] var _velocity = Vec2D(0, 0)
75 def velocity = _velocity
77 def velocity_=(v: Vec2D) {
78 if (v == null) throw new NullPointerException
80 // Check if the new velocity is greater than the maximum velocity.
81 if (v * v > maxVelocity * maxVelocity) {
82 // It is. Set the velocity to the maximum velocity while retaining the direction of the new
83 // velocity.
84 _velocity = v * (maxVelocity / Math.sqrt(v * v))
86 else {
87 // It is not, which means we can safely set the velocity to the new velocity.
88 _velocity = v
94 /**
95 * The body's mass, measured in kilograms.
96 * After creation, a body has an initial mass of 1. The mass must never be negative.
97 * A mass of 0 indicates that a body is static. Static bodies don't move. Forces have no effect on them.
100 private[this] var _mass = 1.0
102 def mass = _mass
104 def mass_=(m: Double) {
105 if (m < 0) throw new IllegalArgumentException
106 _mass = m
112 * Applies a force to the body.
113 * The effect of applied forces is computed when the world simulation is stepped.
114 * Calling this method several times between steps will simply add the applied forces up. When the
115 * simulation is stepped, the sum of all the forces is applied.
116 * A force must never be null.
119 private[this] var _appliedForce = Vec2D(0, 0)
121 def appliedForce = _appliedForce
123 def applyForce(f: Vec2D) {
124 _appliedForce = _appliedForce + f
127 def resetForce {
128 _appliedForce = Vec2D(0, 0)
134 * Sets the maximum velocity for this body.
135 * After creation, a body has a maximum velocity of Double.PositiveInfinity.
136 * The maximum velocity must never be negative.
139 private[this] var _maxVelocity = Double.PositiveInfinity
141 def maxVelocity = _maxVelocity
143 def maxVelocity_=(mv: Double) {
144 if (mv < 0) throw new IllegalArgumentException
145 _maxVelocity = mv