Ive Been trying to Make my own collision for my 2d game. Ive got my Circle collision Working. That was pretty easy but oddly enough the rectangle collision is giving me Some troubles.
Ive figured out how to create a turning rectangle I just cant figure out how to detect if an object(point) goes inside it. Its easy enogh if the rectangle is straight. You just use the same Code as in a button. I also made some code to detect if an object is inside a perfect diamond. IE the angles are 45 degrees. What I'm after is a way to detect if something is inside a rectangle no matter which angle the Rectangle is on. Help would be much appreciated.
Heres My Code So far:
]
Sync On
Hide Mouse
Type Cord
x as Float
y as Float
EndType
Type Rectangle
X as Float
Y as Float
Width as Float
Height as Float
Angle as Float
EndType
Dim Corner(4) as Cord
Rec as Rectangle
Rec.X=300
Rec.Y=300
Rec.Width=30
Rec.Height=30
Rec.Angle=0
Do
`Calculate the Distance of each Corner From the center Point
Corner(1).x=cos(Wrapvalue(Rec.Angle+45))*Rec.Width
Corner(1).y=sin(Wrapvalue(Rec.Angle+45))*Rec.Height
Corner(2).x=cos(Wrapvalue(Rec.Angle+135))*Rec.Width
Corner(2).y=sin(Wrapvalue(Rec.Angle+135))*Rec.Height
Corner(3).x=cos(Wrapvalue(Rec.Angle+225))*Rec.Width
Corner(3).y=sin(Wrapvalue(Rec.Angle+225))*Rec.Height
Corner(4).x=cos(Wrapvalue(Rec.Angle+315))*Rec.Width
Corner(4).y=sin(Wrapvalue(Rec.Angle+315))*Rec.Height
`Add the Distance of the corner to the Center Point
Corner(1).x=Corner(1).x+Rec.X
Corner(1).y=Corner(1).y+Rec.Y
Corner(2).x=Corner(2).x+Rec.X
Corner(2).y=Corner(2).y+Rec.Y
Corner(3).x=Corner(3).x+Rec.X
Corner(3).y=Corner(3).y+Rec.Y
Corner(4).x=Corner(4).x+Rec.X
Corner(4).y=Corner(4).y+Rec.Y
`Rotate Rectangle
if Leftkey()=1 then Rec.Angle=wrapvalue(Rec.Angle-0.2)
if Rightkey()=1 then Rec.Angle=wrapvalue(Rec.Angle+0.2)
`Set the color to Red
ink rgb(200,0,0),1
`Draw Our Rectangle
line Corner(1).x,Corner(1).y,Corner(2).x,Corner(2).y
line Corner(2).x,Corner(2).y,Corner(3).x,Corner(3).y
line Corner(3).x,Corner(3).y,Corner(4).x,Corner(4).y
line Corner(4).x,Corner(4).y,Corner(1).x,Corner(1).y
sync
cls
Loop
In theory, there is no difference between theory and practice. But, in practice, there is.