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.
54 bodyShape = new Shape.
55 bodyShape init-with-cube size (4.0, .75, 3.0).
57 wheelShape = new Shape.
58 wheelShape init-with-polygon-disk radius ( self get-wheel-radius ) sides 20 height ( self get-wheel-width ).
61 sensorShape = new Shape.
62 sensorShape init-with-polygon-cone radius .2 sides 5 height .5.
65 bodyShape set-density to ( self get-density ).
67 bodyLink set-shape to bodyShape.
68 bodyLink set-mu to -1.0.
69 bodyLink set-eT to .8.
71 self set-root to bodyLink.
73 self move to (0, 0.9, 0).
74 self set-texture-scale to 1.5.
82 - to get-wheel-radius:
85 + section "Adding Wheels and Sensors to a Vehicle"
87 + to add-wheel at location (vector):
88 % Adds a wheel at location on the vehicle. This method returns
89 % the wheel which is created, a OBJECT(CeldasWheel).
91 wheel, joint (object).
93 wheel = new CeldasWheel.
94 wheel set-shape to wheelShape.
96 joint = new RevoluteJoint.
98 joint set-relative-rotation around-axis (1, 0, 0) by 1.5708.
99 joint link parent bodyLink to-child wheel with-normal (0, 0, 1)
100 with-parent-point location with-child-point (0, 0, 0).
103 wheel set-texture to 0.
104 wheel set-joint to joint.
105 joint set-strength-limit to (joint get-strength-hard-limit) / 2.
106 wheel set-color to (.6, .6, .6).
107 wheel set-mu to 100000.
109 self add-dependency on joint.
110 self add-dependency on wheel.
112 push wheel onto wheels.
116 + to add-sensor at location (vector):
117 % Adds a sensor at location on the vehicle. This method returns
118 % the sensor which is created, a OBJECT(CeldasSensor).
120 sensor, joint (object).
122 sensor = new CeldasSensor.
123 sensor set-shape to sensorShape.
125 joint = new RevoluteJoint.
127 joint set-relative-rotation around-axis (0, 0, 1) by -1.57.
128 joint link parent bodyLink to-child sensor with-normal (1, 0, 0)
129 with-parent-point location with-child-point (0, 0, 0).
131 joint set-double-spring with-strength 300 with-max 0.01 with-min -0.01.
133 self add-dependency on joint.
134 self add-dependency on sensor.
136 sensor set-color to (0, 0, 0).
138 push sensor onto sensors.
150 CeldasLightVehicle : CeldasVehicle (aka CeldasVehicles) {
151 % A heavy duty version of OBJECT(CeldasLightVehicle), this
152 % vehicle is heavier and harder to control, but more stable
158 - to get-wheel-width:
161 - to get-wheel-radius:
165 Stationary : CeldasObstacle (aka CeldasObstacles) {
166 % A CeldasObstacle is used in conjunction with OBJECT(CeldasControl)
167 % and OBJECT(CeldasVehicle). It is what the OBJECT(CeldasSensor)
168 % objects on the CeldasVehicle detect.
170 % There are no special behaviors associated with the walls--they're
171 % basically just plain OBJECT(Stationary) objects.
173 + 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):
174 self init-with-shape shape (new Shape init-with-cube size theSize) color theColor at-location theLocation with-rotation theRotation.
176 + 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):
177 self register with-shape theShape at-location theLocation with-rotation theRotation.
178 self set-color to theColor.
181 Link : CeldasWheel (aka CeldasWheels) {
182 % A CeldasWheel is used in conjunction with OBJECT(CeldasVehicle)
183 % to build Celdas vehicles. This class is typically not instantiated
184 % manually, since OBJECT(CeldasVehicle) creates one for you when you
185 % add a wheel to the vehicle.
190 - to set-joint to j (object):
195 + section "Configuring the Wheel's Velocity"
197 + to set-velocity to n (float):
198 % Sets the velocity of this wheel.
200 joint set-joint-velocity to n.
203 % Gets the velocity of this wheel.
205 return (joint get-joint-velocity).
209 Link : CeldasSensor (aka CeldasSensors) {
210 % A CeldasSensor is used in conjunction with OBJECT(CeldasVehicle)
211 % to build Celdas vehicles. This class is typically not instantiated
212 % manually, since OBJECT(CeldasVehicle) creates one for you when you
213 % add a sensor to the vehicle.
221 direction = (0, 1, 0).
225 + section "Configuring the Sensor Values"
227 + to set-sensor-angle to n (float):
228 % Sets the angle in which this sensor can detect obstacles. The default
229 % value of 1.6 means that the sensor can see most of everything in
230 % front of it. Setting the value to be any higher leads to general
231 % wackiness, so I don't suggest it.
235 + section "Getting the Sensor Values"
237 + to get-sensor-value:
238 % Gets the sensor value. This should be used from post-iterate,
239 % if not, the sensor reading correspond to the previous
246 strength, angle (float).
247 toObstacle, transDir (vector).
249 transDir = (self get-rotation) * direction.
253 foreach i in (all CeldasObstacles): {
254 toObstacle = (i get-location) - (self get-location).
255 angle = angle(toObstacle, transDir).
257 if angle < sensorAngle: {
258 strength = | (self get-location) - (i get-location) |.
259 strength = 100.0 / (strength * strength) .
261 if strength > value: value = strength.