7 PhysicalControl : CeldasControl {
8 % This class is used for building simple vehicle
9 % simulations. To create a vehicle simulation,
10 % subclass CeldasControl and use the init method to
11 % create OBJECT(CeldasObstacle) and
12 % OBJECT(CeldasVehicle) objects.
17 cloudTexture (object).
21 #self enable-smooth-drawing.
23 floorShape = new Shape.
24 floorShape init-with-cube size (200, .2, 200).
26 floor = new Stationary.
27 floor register with-shape floorShape at-location (0, 0, 0).
30 self point-camera at (0, 0, 0) from (3, 3, 24).
33 #self enable-reflections.
35 cloudTexture = (new Image load from "images/clouds.png").
36 self set-background-color to (.4, .6, .9).
37 self set-background-texture-image to cloudTexture.
40 MultiBody : CeldasLightVehicle (aka CeldasLightVehicles) {
41 % This object is used in conjunction with OBJECT(CeldasControl) to
42 % create simple vehicles.
51 bodyShape = new Shape.
52 bodyShape init-with-cube size (4.0, .75, 3.0).
54 wheelShape = new Shape.
55 wheelShape init-with-polygon-disk radius ( self get-wheel-radius ) sides 20 height ( self get-wheel-width ).
58 sensorShape = new Shape.
59 sensorShape init-with-polygon-cone radius .2 sides 5 height .5.
62 bodyShape set-density to ( self get-density ).
64 bodyLink set-shape to bodyShape.
65 bodyLink set-mu to -1.0.
66 bodyLink set-eT to .8.
68 self set-root to bodyLink.
70 self move to (0, 0.9, 0).
71 self set-texture-scale to 1.5.
79 - to get-wheel-radius:
82 + section "Adding Wheels and Sensors to a Vehicle"
84 + to add-wheel at location (vector):
85 % Adds a wheel at location on the vehicle. This method returns
86 % the wheel which is created, a OBJECT(CeldasWheel).
88 wheel, joint (object).
90 wheel = new CeldasWheel.
91 wheel set-shape to wheelShape.
93 joint = new RevoluteJoint.
95 joint set-relative-rotation around-axis (1, 0, 0) by 1.5708.
96 joint link parent bodyLink to-child wheel with-normal (0, 0, 1)
97 with-parent-point location with-child-point (0, 0, 0).
100 wheel set-texture to 0.
101 wheel set-joint to joint.
102 joint set-strength-limit to (joint get-strength-hard-limit) / 2.
103 wheel set-color to (.6, .6, .6).
104 wheel set-mu to 100000.
106 self add-dependency on joint.
107 self add-dependency on wheel.
111 + to add-sensor at location (vector) with-rotation angle = -1.57 (float)
112 arround-axis axis = (0, 0, 1) (vector)
113 with-normal normal = (1, 0, 0) (vector):
114 % Adds a sensor at location on the vehicle. This method returns
115 % the sensor which is created, a OBJECT(CeldasSensor).
117 sensor, joint (object).
119 sensor = new CeldasSensor.
120 sensor set-shape to sensorShape.
122 joint = new RevoluteJoint.
124 joint set-relative-rotation around-axis axis by angle.
125 joint link parent bodyLink to-child sensor with-normal normal
126 with-parent-point location with-child-point (0, 0, 0).
128 joint set-double-spring with-strength 300 with-max 0.01 with-min -0.01.
130 self add-dependency on joint.
131 self add-dependency on sensor.
133 sensor set-color to (0, 0, 0).
145 CeldasLightVehicle : CeldasVehicle (aka CeldasVehicles) {
146 % A heavy duty version of OBJECT(CeldasLightVehicle), this
147 % vehicle is heavier and harder to control, but more stable
153 - to get-wheel-width:
156 - to get-wheel-radius:
160 Stationary : CeldasObstacle (aka CeldasObstacles) {
161 % A CeldasObstacle is used in conjunction with OBJECT(CeldasControl)
162 % and OBJECT(CeldasVehicle). It is what the OBJECT(CeldasSensor)
163 % objects on the CeldasVehicle detect.
165 % There are no special behaviors associated with the walls--they're
166 % basically just plain OBJECT(Stationary) objects.
168 + to init with-size theSize = (10, 3, .1) (vector) with-color theColor = (1, 0, 0) (vector) at-location theLocation = (0, 0, 0) (vector) with-rotation theRotation = [ ( 0, 0, 1 ), ( 0, 1, 0 ), ( 1, 0, 0 ) ] (matrix):
169 self init-with-shape shape (new Shape init-with-cube size theSize) color theColor at-location theLocation with-rotation theRotation.
171 + to init-with-shape shape theShape (object) color theColor = (1, 0, 0) (vector) at-location theLocation = (0, 0, 0) (vector) with-rotation theRotation = [ ( 1, 0, 0 ), ( 0, 1, 0 ), ( 0, 0, 1 ) ] (matrix):
172 self register with-shape theShape at-location theLocation with-rotation theRotation.
173 self set-color to theColor.
176 Link : CeldasWheel (aka CeldasWheels) {
177 % A CeldasWheel is used in conjunction with OBJECT(CeldasVehicle)
178 % to build Celdas vehicles. This class is typically not instantiated
179 % manually, since OBJECT(CeldasVehicle) creates one for you when you
180 % add a wheel to the vehicle.
185 - to set-joint to j (object):
190 + section "Configuring the Wheel's Velocity"
192 + to set-velocity to n (float):
193 % Sets the velocity of this wheel.
195 joint set-joint-velocity to n.
198 % Gets the velocity of this wheel.
200 return (joint get-joint-velocity).
204 Link : CeldasSensor (aka CeldasSensors) {
205 % A CeldasSensor is used in conjunction with OBJECT(CeldasVehicle)
206 % to build Celdas vehicles. This class is typically not instantiated
207 % manually, since OBJECT(CeldasVehicle) creates one for you when you
208 % add a sensor to the vehicle.
215 + to init with-direction theDirection = (0, 1, 0) (vector)
216 with-angle theAngle = 1.6 (float):
217 direction = theDirection.
218 sensorAngle = theAngle.
221 + section "Configuring the Sensor Values"
223 + to set-sensor-angle to n (float):
224 % Sets the angle in which this sensor can detect obstacles. The default
225 % value of 1.6 means that the sensor can see most of everything in
226 % front of it. Setting the value to be any higher leads to general
227 % wackiness, so I don't suggest it.
231 + section "Getting the Sensor Values"
233 + to get-sensor-value:
234 % Gets the sensor value. This should be used from post-iterate,
235 % if not, the sensor reading correspond to the previous
242 strength, angle (float).
243 toObstacle, transDir (vector).
245 transDir = (self get-rotation) * direction.
249 foreach i in (all CeldasObstacles): {
250 toObstacle = (i get-location) - (self get-location).
251 angle = angle(toObstacle, transDir).
253 print "angle: $angle -- sensorAngle = $sensorAngle".
254 if angle < sensorAngle: {
255 strength = | (self get-location) - (i get-location) |.
256 strength = 100.0 / (strength * strength) .
258 if strength > value: value = strength.