Class Example::Car
In: examples/test_buggy.rb  (CVS)
Parent: Object

Methods

>>   addToSpace   drive   location   new   removeFromSpace   rotation   steer   throttle  

Included Modules

ODE

Constants

RadToDeg = 180.0 / Math::PI
Chassis = Struct::new( "CarChassis", :body, :geometry )
Wheel = Struct::new( "CarWheel", :body, :geometry, :hinge )
DefaultPosition = Position::new( 0, 0, 0.5 )

Attributes

chassis  [R]  The car’s chassis struct
space  [R]  The car’s top-level geometry space
wheels  [R]  The car’s wheel struct array

Public Class methods

[Source]

# File examples/test_buggy.rb, line 48
        def initialize( world, length=0.7, width=0.5, height=0.2, wheelRadius=0.24,
                        chassisMass=1, wheelMass=0.2, position=DefaultPosition )

            @space = Space::new

            @chassis = Chassis::new
            @chassis.body = world.createBody
            @chassis.body.position = position
            @chassis.body.mass = Mass::Box::new( length, width, height, chassisMass )

            @chassis.geometry = Geometry::Box::new( length, width, height, @space )
            @chassis.geometry.body = @chassis.body

            @wheels = (0..2).to_a.collect {|i|
                # Create the wheel body
                body = world.createBody
                body.quaternion = 1, 0, 0, (ODE::Pi*1.5)
                body.mass = Mass::Sphere::new( 1, wheelRadius, wheelMass )

                # Position each wheel appropriately
                case i
                when 1
                    body.position = (0.5 * length), 0, (position.z - height * 0.5);

                when 2
                    body.position = (-0.5 * length), (width * 0.5), (position.z - height * 0.5);

                when 3
                    body.position = (-0.5 * length), (width * -0.5), (position.z - height * 0.5);
                end

                # Create the wheel's collision geometry
                geom = Geometry::Sphere::new( wheelRadius, @space )
                geom.body = body

                # Create and configure a joint, and then use it to connect the wheel
                # to the chassis
                hinge = Hinge2Joint::new( world )
                hinge.attach( @chassis.body, body )
                hinge.anchor = body.position.x, body.position.y, body.position.z
                hinge.axis1 = 0, 0, 1
                hinge.axis2 = 0, 1, 0
                hinge.suspensionERP = 0.4
                hinge.suspensionCFM = 0.8

                Wheel::new( body, geom, hinge )
            }

            # Lock rear wheels along the steering axis
            @wheels[1..2].each {|wheel|
                wheel.hinge.loStop = 0
                wheel.hinge.hiStop = 0
            }

        end

Public Instance methods

Add the car to the specified ODE::Space.

[Source]

# File examples/test_buggy.rb, line 148
        def >>( space )
            if space.is_a?( ODE::Space )
                self.addToSpace( space )

            else
                raise "Cannot add the car to a %s" % space.class.name
            end
        end

Add the car to the specified collision space

[Source]

# File examples/test_buggy.rb, line 158
        def addToSpace( space )
            space << @space
        end

Drive the car

[Source]

# File examples/test_buggy.rb, line 119
        def drive( steering, speed )
            self.steer( steering )
            self.throttle( speed )
        end

Return a string describing the car’s location relative to the origin.

[Source]

# File examples/test_buggy.rb, line 168
        def location
            pos = @chassis.body.position
            return "%d from origin at (%0.1f, %0.1f, %0.1f)" %
                [ pos.distance(ODE::Position::Origin), pos.x, pos.y, pos.z ]
        end

Remove the car from the specified collision space

[Source]

# File examples/test_buggy.rb, line 163
        def removeFromSpace( space )
            space.removeGeometry( @space )
        end

Return a string describing the car’s rotation relative to the default orientation.

[Source]

# File examples/test_buggy.rb, line 177
        def rotation
            quat = @chassis.body.rotation
            return "R: %0.1f  P: %0.1f  Y: %0.1f" %
                [ quat.roll * RadToDeg, quat.pitch * RadToDeg, quat.yaw * RadToDeg ]
        end

Steer the car’s front wheel

[Source]

# File examples/test_buggy.rb, line 126
        def steer( steering )
            vel = steering - @wheels[0].hinge.angle1
            if ( vel > 0.1 ) then vel = 0.1 end
            if ( vel < -0.1 ) then vel = -0.1 end

            vel *= 10
            @wheels[0].hinge.velocity = vel
            @wheels[0].hinge.fMax = 0.2
            @wheels[0].hinge.loStop = -0.75
            @wheels[0].hinge.hiStop = 0.75
            @wheels[0].hinge.fudgeFactor = 0.1
        end

Adjust the speed of the car’s engine by the car’s speed.

[Source]

# File examples/test_buggy.rb, line 141
        def throttle( speed )
            @wheels[0].hinge.vel2 = -speed
            @wheels[0].hinge.fMax2 = 0.1
        end

[Validate]