I once altered the car-demo that comes with B3D.
I tried to do collisions with two moving objects.
I positioned a cube on the floor, which moves on it's own (just a MoveEntity inside the main loop).
Then I tried to hit it with my car.
When I was driving against it's backside (the cube would move away from me if I should stop the car), I could collide with it.
When the cube was coming towards me, I ran right through it.
That was because I've setup collisions between the car and the cube, but not the other way around.
That was the solution for my problem.
It might not work in any situation.
; Initiate a 3D window of size 800x600
Graphics3D 800, 600, 0, 2
; Set backbuffer
SetBuffer BackBuffer()
; Set some variables for collision-types
col_cube1 = 1
col_cube2 = 2
; Create a floor-plane
Floor = CreatePlane()
; Create cube1, give it a color, position it, give it a spherical collision radius and set it's collisiontype
cube1 = CreateCube()
EntityColor cube1, 255, 0, 0
PositionEntity cube1, -10, 1, 0
EntityRadius cube1, 1
EntityType cube1, col_cube1
; Create cube2, give it a color, position it, give it a spherical collision radius and set it's collisiontype
cube2 = CreateCube()
EntityColor cube2, 0, 255, 0
PositionEntity cube2, 10, 1, 0
EntityRadius cube2, 1
EntityType cube2, col_cube2
; Setup collisions for collisiontype col_cube1 to col_cube2 and vice versa
Collisions col_cube1, col_cube2, 1, 2
Collisions col_cube2, col_cube1, 1, 2
; Create and position camera
camera = CreateCamera()
MoveEntity camera, 0, 5, -10
; Main loop
While Not KeyHit(1)
; Move both entities towards eachother
MoveEntity cube1, 0.1, 0, 0
MoveEntity cube2, -0.15, 0, 0
; Detect when cube1 has collided with cube2
; When collision occurs, reposition both entities to their starting positions
If EntityCollided(cube1, col_cube2) Then
PositionEntity cube1, -10, 1, 0
PositionEntity cube2, 10, 1, 0
EndIf
; Process collicions
UpdateWorld
; Render scene
RenderWorld
; Flip backbuffer to frontbuffer
Flip
Wend
This short code shows two moving cubes (moving towards eachother) and hitting eachother.
But they stop on impact, even when one cube is moving faster than the other.
You could always check to see if both cubes are still moving.
If not (they have collided and are pushing against eachother), do something else.
Like Beaker said, both cubes here use an entityradius, so their collisionboxes are actually spheres, but you don't see any evidence of that in this example.
To detect collisions, I've added a small If-EndIf part in the main-loop.
As soon as cube1 collides with an object which has cul_cube2 as collisiontype, reset both cubes to their starting positions.