7 @define CELDAS_MAX_VELOCITY 30.
9 PhysicalControl : CeldasControl {
10 % This class is used for building simple vehicle
11 % simulations. To create a vehicle simulation,
12 % subclass CeldasControl and use the init method to
13 % create OBJECT(CeldasObstacle) and
14 % OBJECT(CeldasVehicle) objects.
19 cloudTexture (object).
23 #self enable-smooth-drawing.
25 floorShape = new Shape.
26 floorShape init-with-cube size (200, .2, 200).
28 floor = new Stationary.
29 floor register with-shape floorShape at-location (0, 0, 0).
32 self point-camera at (0, 0, 0) from (3, 3, 24).
35 #self enable-reflections.
37 cloudTexture = (new Image load from "images/clouds.png").
38 self set-background-color to (.4, .6, .9).
39 self set-background-texture-image to cloudTexture.
42 MultiBody : CeldasLightVehicle (aka CeldasLightVehicles) {
43 % This object is used in conjunction with OBJECT(CeldasControl) to
44 % create simple vehicles.
56 bodyShape = new Shape.
57 bodyShape init-with-cube size (4.0, .75, 3.0).
59 wheelShape = new Shape.
60 wheelShape init-with-polygon-disk radius ( self get-wheel-radius ) sides 20 height ( self get-wheel-width ).
63 sensorShape = new Shape.
64 sensorShape init-with-polygon-cone radius .2 sides 5 height .5.
67 bodyShape set-density to ( self get-density ).
69 bodyLink set-shape to bodyShape.
70 bodyLink set-mu to -1.0.
71 bodyLink set-eT to .8.
73 self set-root to bodyLink.
75 self move to (0, 0.9, 0).
76 self set-texture-scale to 1.5.
84 - to get-wheel-radius:
87 + section "Adding Wheels and Sensors to a Vehicle"
89 + to add-wheel at location (vector):
90 % Adds a wheel at location on the vehicle. This method returns
91 % the wheel which is created, a OBJECT(CeldasWheel).
93 wheel, joint (object).
95 wheel = new CeldasWheel.
96 wheel set-shape to wheelShape.
98 joint = new RevoluteJoint.
100 joint set-relative-rotation around-axis (1, 0, 0) by 1.5708.
101 joint link parent bodyLink to-child wheel with-normal (0, 0, 1)
102 with-parent-point location with-child-point (0, 0, 0).
105 wheel set-texture to 0.
106 wheel set-joint to joint.
107 joint set-strength-limit to (joint get-strength-hard-limit) / 2.
108 wheel set-color to (.6, .6, .6).
109 wheel set-mu to 100000.
111 self add-dependency on joint.
112 self add-dependency on wheel.
114 push wheel onto wheels.
118 + to add-sensor at location (vector):
119 % Adds a sensor at location on the vehicle. This method returns
120 % the sensor which is created, a OBJECT(CeldasSensor).
122 sensor, joint (object).
124 sensor = new CeldasSensor.
125 sensor set-shape to sensorShape.
127 joint = new RevoluteJoint.
129 joint set-relative-rotation around-axis (0, 0, 1) by -1.57.
130 joint link parent bodyLink to-child sensor with-normal (1, 0, 0)
131 with-parent-point location with-child-point (0, 0, 0).
133 joint set-double-spring with-strength 300 with-max 0.01 with-min -0.01.
135 self add-dependency on joint.
136 self add-dependency on sensor.
138 sensor set-color to (0, 0, 0).
140 push sensor onto sensors.
152 CeldasLightVehicle : CeldasVehicle (aka CeldasVehicles) {
153 % A heavy duty version of OBJECT(CeldasLightVehicle), this
154 % vehicle is heavier and harder to control, but more stable
160 - to get-wheel-width:
163 - to get-wheel-radius:
167 Stationary : CeldasObstacle (aka CeldasObstacles) {
168 % A CeldasObstacle is used in conjunction with OBJECT(CeldasControl)
169 % and OBJECT(CeldasVehicle). It is what the OBJECT(CeldasSensor)
170 % objects on the CeldasVehicle detect.
172 % There are no special behaviors associated with the walls--they're
173 % basically just plain OBJECT(Stationary) objects.
175 + 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):
176 self init-with-shape shape (new Shape init-with-cube size theSize) color theColor at-location theLocation with-rotation theRotation.
178 + 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):
179 self register with-shape theShape at-location theLocation with-rotation theRotation.
180 self set-color to theColor.
183 Link : CeldasWheel (aka CeldasWheels) {
184 % A CeldasWheel is used in conjunction with OBJECT(CeldasVehicle)
185 % to build Celdas vehicles. This class is typically not instantiated
186 % manually, since OBJECT(CeldasVehicle) creates one for you when you
187 % add a wheel to the vehicle.
196 - to set-joint to j (object):
201 + section "Configuring the Wheel's Velocity"
203 + to set-velocity to n (float):
204 % Sets the velocity of this wheel.
206 if n > CELDAS_MAX_VELOCITY: n = CELDAS_MAX_VELOCITY.
209 joint set-joint-velocity to velocity.
212 % Gets the velocity of this wheel.
218 Link : CeldasSensor (aka CeldasSensors) {
219 % A CeldasSensor is used in conjunction with OBJECT(CeldasVehicle)
220 % to build Celdas vehicles. This class is typically not instantiated
221 % manually, since OBJECT(CeldasVehicle) creates one for you when you
222 % add a sensor to the vehicle.
230 direction = (0, 1, 0).
234 + section "Configuring the Sensor Values"
236 + to set-sensor-angle to n (float):
237 % Sets the angle in which this sensor can detect obstacles. The default
238 % value of 1.6 means that the sensor can see most of everything in
239 % front of it. Setting the value to be any higher leads to general
240 % wackiness, so I don't suggest it.
244 + section "Getting the Sensor Values"
246 + to get-sensor-value:
247 % Gets the sensor value. This should be used from post-iterate,
248 % if not, the sensor reading correspond to the previous
253 strength, angle (float).
254 toObstacle, transDir (vector).
256 transDir = (self get-rotation) * direction.
260 foreach i in (all CeldasObstacles): {
261 toObstacle = (i get-location) - (self get-location).
262 angle = angle(toObstacle, transDir).
264 if angle < sensorAngle: {
265 strength = | (self get-location) - (i get-location) |.
266 strength = 1.0 / (strength * strength) .
268 if strength > 10: strength = 10.
270 if strength > value: value = strength.