Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Cube to cube collision

dave1707dave1707 Mod
in General Posts: 9,725

@Simeon I haven’t tried this before, but I have a question here. I have a sphere bouncing between 2 cubes. The sphere bounces off the bottom cube and also bounces off the top cube. But if I comment out the createSphere line to remove the sphere, the top cube passes thru the bottom cube. Just curious why the cubes don’t collide, but they both collide with the sphere.

viewer.mode=FULLSCREEN

function setup()
    assert(OrbitViewer, "Please include Cameras as a dependency")
    scene = craft.scene()
    skyMaterial=scene.sky.material
    skyMaterial.sky=color(0, 62, 255, 255)
    skyMaterial.horizon=color(99, 255, 0, 255)   
    scene.sun.rotation=quat.eulerAngles(20,45,-30)
    v=scene.camera:add(OrbitViewer, vec3(0,0,0), 150, 0, 500)
    v.rx,v.ry=10,20
    createFloor()
    createRect(0,15,0)
    createSphere(0,0,0)
end

function draw()
    update(DeltaTime)
    scene:draw()    
end

function update(dt)
    scene:update(dt)
end

function createFloor() 
    floor=scene:entity()
    f=floor:add(craft.rigidbody,STATIC)
    f.restitution=1
    floor.position=vec3(0,-20,0)
    floor.model = craft.model.cube(vec3(100,1,100))
    floor:add(craft.shape.model,floor.model)
    floor.material = craft.material(asset.builtin.Materials.Standard)
    floor.material.map = readImage(asset.builtin.Surfaces.Basic_Bricks_AO)
end

function createRect(x,y,z) 
    rec=scene:entity()
    rc=rec:add(craft.rigidbody,DYNAMIC)
    rc.restitution=1
    rec.position=vec3(x,y,z)
    rec.model = craft.model.cube(vec3(5,5,5))
    rec:add(craft.shape.model,rec.model)
    rec.material = craft.material(asset.builtin.Materials.Standard)
    rec.material.map = readImage(asset.builtin.Surfaces.Basic_Bricks_AO)
end

function createSphere(x,y,z)
    local sphere1=scene:entity()
    local sph=sphere1:add(craft.rigidbody,DYNAMIC)
    sph.restitution=1
    sphere1.position=vec3(x,y,z)
    sphere1.model = craft.model.icosphere(1,2)
    sphere1:add(craft.shape.sphere,1)
    sphere1.material = craft.material(asset.builtin.Materials.Specular)    
    sphere1.material.diffuse=color(0,0,255)
end

Comments

  • Posts: 1,275

    very weird. i don't see anything that would explain it.

  • dave1707dave1707 Mod
    Posts: 9,725

    Not sure why a cube to cube collision doesn’t happen when a sphere to cube collision works. Here’s a way I got around it just in case anyone else needs to do it. It’s a straight on collision, so the cube won’t rotate on any off angle collisions. I just embedded a sphere within the cube. The sphere does the collision and the cube just stays with it.

    viewer.mode=FULLSCREEN
    
    function setup()
        assert(OrbitViewer, "Please include Cameras as a dependency")
        scene = craft.scene()
        skyMaterial=scene.sky.material
        skyMaterial.sky=color(0, 62, 255, 255)
        skyMaterial.horizon=color(99, 255, 0, 255)   
        scene.sun.rotation=quat.eulerAngles(20,45,-30)
        v=scene.camera:add(OrbitViewer, vec3(0,0,0), 150, 0, 500)
        v.rx,v.ry=10,20
        createFloor()
        createRect(0,15,0)
        createSphere(0,15,0)
    end
    
    function draw()
        update(DeltaTime)
        rec.position=sphere1.position
        scene:draw()    
    end
    
    function update(dt)
        scene:update(dt)
    end
    
    function createFloor() 
        floor=scene:entity()
        f=floor:add(craft.rigidbody,STATIC)
        f.restitution=1
        floor.position=vec3(0,-20,0)
        floor.model = craft.model.cube(vec3(100,1,100))
        floor:add(craft.shape.model,floor.model)
        floor.material = craft.material(asset.builtin.Materials.Standard)
        floor.material.map = readImage(asset.builtin.Surfaces.Basic_Bricks_AO)
    end
    
    function createRect(x,y,z) 
        rec=scene:entity()
        rc=rec:add(craft.rigidbody,DYNAMIC)
        rc.restitution=1
        rec.position=vec3(x,y,z)
        rec.model = craft.model.cube(vec3(5,5,5))
        rec:add(craft.shape.model,rec.model)
        rec.material = craft.material(asset.builtin.Materials.Standard)
        rec.material.map = readImage(asset.builtin.Surfaces.Basic_Bricks_AO)
    end
    
    function createSphere(x,y,z)
        sphere1=scene:entity()
        sph=sphere1:add(craft.rigidbody,DYNAMIC)
        sph.restitution=1
        sphere1.position=vec3(x,y,z)
        sphere1.model = craft.model.icosphere(2.49,2)
        sphere1:add(craft.shape.sphere,2.49)
        sphere1.material = craft.material(asset.builtin.Materials.Specular)    
        sphere1.material.diffuse=color(0,0,255)
    end
    
  • Posts: 2,496

    @dave1707 - ingenious but just a backstop, is this a fault in the physics library?

  • dave1707dave1707 Mod
    Posts: 9,725

    @Bri_G I’m not sure if it’s a fault, or I’m doing something wrong, or maybe trying to do a cube to cube collision is too difficult trying to figure out the angles and rotation, etc. This is the first time I tried to do a cube to cube collision after I don’t know how many years, so I don’t think it’s going to bother me that much.

    Just curious why it doesn’t work.

  • Posts: 185

    how does this work? where is the size and shape of the body defined?

    rc=rec:add(craft.rigidbody,DYNAMIC)

  • dave1707dave1707 Mod
    Posts: 9,725

    @skar The rec.model sets the size of the cube, 5x5x5, and the sphere1 sets the radius of the sphere, 2.49 or a diameter of 4.98 . The diameter of the sphere has to be smaller than the side of the cube or it doesn’t work. So the model sets the shape, cube or icosphere. The other code sets the position, texture, color, bouncyness, and the body type DYNAMIC.

    rec.model = craft.model.cube(vec3(5,5,5))
    
    sphere1.model = craft.model.icosphere(2.49,2)
    sphere1:add(craft.shape.sphere,2.49)
    
  • Posts: 2,496
    @dave1707 - I take it three spheres works fine. Sounds like the physics definition of cubes is missing something.
  • dave1707dave1707 Mod
    Posts: 9,725

    @Bri_G Sphere collision is easy, just the standard billiard ball calculations. But I guess trying to do a cube collision would be a lot harder considering the different angles and surfaces that would be involved. I have code where there’s a lot of spheres colliding and bouncing off walls. Those work just fine.

Sign In or Register to comment.