January 15, 2021, 05:53:16 PM

### Author Topic: [bb] Isometric collision by Nebula [ 1+ years ago ]  (Read 468 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Isometric collision by Nebula [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Isometric collision
Author : Nebula
Posted : 1+ years ago

Description : 2 functions that returns when a point is inside a isometric area. The function also returns when the mouse is in one of the four corners.

RRectcollison is my latest attempt at isocollision. Only 3 lines of code are required to check for the collision.

Code :
Code: BlitzBasic
1. ;
2. ; Simple 2D Isometric collision routine - By Nebula
3. ;
4.
5. ;
6. ; Usage :       The input coordinates for the isocollision funtion need to be under the size
7. ;                       of the isometric square. x<64 and y<32.
8. ;
9. ;                       The isocollision function returns 1=true if the mouse is inside a iso square and
10. ;                       returns higher numbers if it is in one of the outside corners. lt=2,rt=3,lb=4,rb=5.
11. ;
12. ;
13. ;
14.
15.
16. Graphics 640,480,16,2
17.
18. HidePointer()
19.
20. Repeat
21.         Cls
22.         drawiso(320,150)
23.         ;
24.         Plot MouseX(),MouseY()
25.         ;
26.         Text 0,0,Str(MouseX()-320) + " : " + Str(MouseY()-150)
27.         ;
28.         a = isocollision((MouseX()-320),MouseY()-150)
29.         If a > 0 Then
30.                 If a = 1 Then
31.                         Text 0,32,"Collision !!!"
32.                 End If
33.                 If a > 1 Then Text 0,32,"Border :   " + a
34.         End If
35.
36.         Flip
37. Until KeyDown(1)
38.
39.
40. End
41.
42. ;
43. ; My Latest iso collision algorithm using Cos and Sin to rotate the mouse
44. ; coordinates and check for simple rectangular collision.
45. ;
46. ; Tested on 64*32 iso tiles and 128*64. Still needs a little
47. ; tweaking
48. Function RRectcollision(x,y,w,h,rot#)
49.         a = ((((Cos(rot)*((x/2)-(w/2)/2.5) )  + Sin(rot) * (y-h/2.5))+(w/2))*1.4)-h
50.         b = (((-Sin(rot) * ((x/2)-(w/2)/2.5) ) + Cos(rot)       *(y-h/2.5)+h) * 1.4)-3
51.         If a< 0 Or a> w/2 Or b<h Or b>h+h Then Return True
52.         Return False
53. End Function
54.
55.
56. ;
57. ; This function returns :
58. ;                           1 - If the mouse is inside a isometric square
59. ;                                                       2 - Left top just outside of the square
60. ;                                                       3 - right top just outside of the square
61. ;                                                       4 - Left bottom just outside of the square
62. ;                                                       5 - right bottom just outside of the square
63. ;
64. Function isocollision(x1,y1)
65. If (x1<0 Or x1>64) Or (y1<0 Or y>32) Then Return
66. For y=0 To 32
67.         For l=0 To c
68.                 If ((x1 = (-c+32+l)) Or (x1 = (c+32-l))) And (y1 = y) Then Return True
69.         Next
70.         If y=>16 Then c=c-2 Else c=c+2
71. Next
72. If x1<64 And y1<32 Then Return ((y1/16*2)+x1/32)+2
73. End Function
74.
75. ;
76. ; Draw a simple isometric square
77. ;
78. Function drawiso(x1,y1)
79. Rect x1,y1,64,32,0
80. Line x1+31,y1,x1+61,y1+16
81. Line x1+31,y1,x1,y1+16
82. Line x1+31,y1+31,x1+63,y1+16
83. Line x1+31,y1+31,x1,y1+16
84. End Function