class SK::BoxCollider

Attributes

friction[RW]
height[RW]
restitution[RW]
shape[RW]
width[RW]

Public Class Methods

new(width, height) click to toggle source
# File lib/shirokuro/standard_components/physics/box_collider.rb, line 6
def initialize width, height
        @width = width
        @height = height
        @friction = 0.4
        @restitution = 0.1
end

Public Instance Methods

start() click to toggle source
# File lib/shirokuro/standard_components/physics/box_collider.rb, line 13
def start
        rigid_body = get_component(RigidBody)
        
        if rigid_body == nil
                raise "RigidBody required for BoxCollider"
        end

        half_width = @width / 2.0
        half_height = @height / 2.0
        
        vertices = [
                CP::Vec2.new(-half_width, -half_height),
                CP::Vec2.new(-half_width, half_height),
                CP::Vec2.new(half_width, half_height),
                CP::Vec2.new(half_width, -half_height)
        ]

        @shape = CP::Shape::Poly.new(rigid_body.body, vertices, CP::Vec2.new(0, 0))
        @shape.u = @friction
        @shape.e = @restitution

        # recalculate inertia
        rigid_body.body.i = CP.moment_for_box(rigid_body.body.m, @width, @height)

        physics.space.add_shape(@shape)
end