How can I detect when any of my cubes collide?

Blitz3D Forums/Blitz3D Beginners Area/How can I detect when any of my cubes collide?

hollifd(Posted 2008) [#1]
I need to be able to move any cube and have it stop when it collides with another cube. I want to be able to press the space bar along with an arrow key to allow a cube to pass over another.

Here is some sample code of what I have so far. To use it, you select a colored box to tell the program what "tool" you want to move. After selecting a tool, you can use the arrow keys to move the tool left and right. The up arrow increases the length of the tool. The down arrow decreases the length of the tool.

I don't know how to make the cubes stop when they run into any other cube. I also need the collisions to be recognized after changing the size of any of the cubes or "tools".

Hopefully, someone will understand what I am having trouble with and can offer some help.

Thanks,

David

Here is some sample code so far...don't laugh at my novice coding.


Graphics3D 1280,1024, 0, 2
SetBuffer BackBuffer()

AppTitle "Z-Axis Simulation -- Uses 3DS files R=Rotate Part, C=Capture Screen, F=Flip Part, S=Show Set Screws, H=Hide Set Screws"


camera=CreateCamera()
PositionEntity camera,0,0,-50

CameraClsColor camera,255,255,255

MovementAmount# = 0.500
FingerToMove$ = "tl1"


light=CreateLight()

;Let's drawing some tools now...
tl1_Length# = 12.00
tl2_Length# = 2.00
tl3_Length# = 2.00

tl1=CreateCube()
ScaleEntity tl1, tl1_Length#/2,.25,.001
EntityColor tl1, 255,0,0
TranslateEntity tl1, -6,0,.002
EntityType tl1, 1

tl2=CreateCube()
ScaleEntity tl2, tl2_Length#/2,.25,.001
EntityColor tl2, 0,255,0
TranslateEntity tl2, -15,0,.002
EntityType tl2, 2

tl3=CreateCube()
ScaleEntity tl3, tl3_Length#/2,.25,.001
EntityColor tl3, 0,0,255
TranslateEntity tl3, -24,0,.002
EntityType tl3, 3



While Not KeyDown( 1 )
Cls

ScaleEntity tl1, tl1_Length#/2,.312,.001
ScaleEntity tl2, tl2_Length#/2,.312,.001
ScaleEntity tl3, tl3_Length#/2,.312,.001


If KeyDown(200) Or MouseDown(1) Then ;up

;Let's check to see if the user is pressing one of the direction buttons instead of the arrow keys...
myerror% = 0
If MouseDown(1) Then
If MouseX() > 770 And MouseX() < 820 Then
If MouseY() > 930 And MouseY() < 955 Then
myerror% = 0
Else
myerror% = 1
End If
Else
myerror% = 1
End If
End If

If myerror% = 0 Then
Select FingerToMove
Case "tl1"
tl1_Length# = tl1_Length# + MovementAmount#
TranslateEntity tl1, - MovementAmount#/2,0,0
Case "tl2"
tl2_Length# = tl2_Length# + MovementAmount#
TranslateEntity tl2, - MovementAmount#/2,0,0
Case "tl3"
tl3_Length# = tl3_Length# + MovementAmount#
TranslateEntity tl3, - MovementAmount#/2,0,0
End Select
End If

End If


If KeyDown(208) Or MouseDown(1) Then ;down

;Let's check to see if the user is pressing one of the direction buttons instead of the arrow keys...
myerror% = 0
If MouseDown(1) Then
If MouseX() > 770 And MouseX() < 820 Then
If MouseY() > 990 And MouseY() < 1015 Then
myerror% = 0
Else
myerror% = 1
End If
Else
myerror% = 1
End If
End If

If myerror% = 0 Then
Select FingerToMove
Case "tl1"
tl1_Length# = tl1_Length# - MovementAmount#
TranslateEntity tl1, + MovementAmount#/2,0,0
Case "tl2"
tl2_Length# = tl2_Length# - MovementAmount#
TranslateEntity tl2, + MovementAmount#/2,0,0
Case "tl3"
tl3_Length# = tl3_Length# - MovementAmount#
TranslateEntity tl3, + MovementAmount#/2,0,0

End Select
End If

End If

If KeyDown(203) Or MouseDown(1) Then ;left

;Let's check to see if the user is pressing one of the direction buttons instead of the arrow keys...
myerror% = 0
If MouseDown(1) Then
If MouseX() > 740 And MouseX() < 790 Then
If MouseY() > 960 And MouseY() < 985 Then
myerror% = 0
Else
myerror% = 1
End If
Else
myerror% = 1
End If
End If

If myerror% = 0 Then
Select FingerToMove
Case "tl1"
TranslateEntity tl1, -MovementAmount#,0,0
Case "tl2"
TranslateEntity tl2, -MovementAmount#,0,0
Case "tl3"
TranslateEntity tl3, -MovementAmount#,0,0
End Select
End If

End If

If KeyDown(205) Or MouseDown(1) Then ;right

;Let's check to see if the user is pressing one of the direction buttons instead of the arrow keys...
myerror% = 0
If MouseDown(1) Then
If MouseX() > 800 And MouseX() < 850 Then
If MouseY() > 960 And MouseY() < 985 Then
myerror% = 0
Else
myerror% = 1
End If
Else
myerror% = 1
End If
End If

If myerror% = 0 Then
Select FingerToMove
Case "tl1"
TranslateEntity tl1, MovementAmount#,0,0
Case "tl2"
TranslateEntity tl2, MovementAmount#,0,0
Case "tl3"
TranslateEntity tl3, MovementAmount#,0,0
End Select
End If

End If


If MouseDown(1) Then
If MouseX() > 10 And MouseX() < 35 Then ;tl1
If MouseY() > 570 And MouseY() < 595 Then
FingerToMove = "tl1"
End If
End If
If MouseX() > 10 And MouseX() < 35 Then
If MouseY() > 600 And MouseY() < 625 Then
FingerToMove = "tl2"
End If
End If
If MouseX() > 10 And MouseX() < 35 Then
If MouseY() > 630 And MouseY() < 655 Then
FingerToMove = "tl3"
End If
End If
End If



UpdateWorld



;Here is where I am trying to detect collisions between any "tools"
For C% = 1 To 3
For D% = 1 To 3
Collisions C%,D%,2,1
Next
Next

;This is so the user can press the spacebar to allow a "tool" to pass over another "tool"
If KeyDown(57) Then
ClearCollisions
End If


RenderWorld




;Let's print some column headings for the tooling information...
Text 10,555, "TL#"
Text 45,555, "LENGTH"
Text 120,555, "LOCATION"



;Let's draw the rectangles for the tool numbers...
Color 255,0,0 ;tl1
Rect 10,570,25,25,1
Color 0,0,0
Text 45,575, tl1_Length# ;Length
Text 120,575, EntityX#(tl1) + tl1_Length#/2

Color 0,255,0 ;tl2
Rect 10,600,25,25,1
Color 0,0,0
Text 45,605, tl2_Length# ;Length
Text 120,605, EntityX#(tl2) + tl2_Length#/2

Color 0,0,255 ;tl3
Rect 10,630,25,25,1
Color 0,0,0
Text 45,635, tl3_Length# ;Length
Text 120,635, EntityX#(tl3) + tl3_Length#/2




;Let's draw some movement buttons now...
Color 0,0,0
Rect 740,960,50,25,0
Text 747, 966, "LEFT"

Color 0,0,0
Rect 800,960,50,25,0
Text 807, 966, "RIGHT"

Color 0,0,0
Rect 770,930,50,25,0
Text 785, 936, "UP"

Color 0,0,0
Rect 770,990,50,25,0
Text 777, 996, "DOWN"


Flip

Wend
End


hollifd(Posted 2008) [#2]
I found a couple of typos in my code and corrected the version above. It now almost works but does not work after changing the size of my cubes or "tools". Before changing the length of the cubes, the collision checking seems to work.

Any ideas?


hollifd(Posted 2008) [#3]
Nevermind,

I figured it out... or at least a way to make it work. Each time I change the length of the cube or "tool", I calculate and set the entityradius. Also, I changed from collision mode 2 to collision mode 1.

Thanks,

David


Ross C(Posted 2008) [#4]
Glad you got it sorted. Just noticed your post!