Kollision eines Punktes mit einem Kreisausschnitt
Übersicht

![]() |
KabelbinderSieger des WM-Contest 2006Betreff: Kollision eines Punktes mit einem Kreisausschnitt |
![]() Antworten mit Zitat ![]() |
---|---|---|
Hi
Ich hab sowas ähnliches vor einiger Zeit hier schonmal gepostet. Jetzt hab ich das ganze mal verbessert und eine Funktion daraus gemacht: Diese Funktion findet heraus, ob ein Punkt gerade an einen Kreisausschnitt stößt. Man kann das z.B. für das Sichtfeld von einem Gegner und so nehmen. Ich benutze die Function auch in Macrophage. Die Funktion benötigt außerdem noch die Function Spat(), die den Abstand zwischen zwei Punkten misst Hier die Function: Code: [AUSKLAPPEN] Function segcoll(x,y,midx,midy,arc,alpha,rad)
If alpha = 0 Then Return 0 If (arc+alpha) < 360 Then roll = (ATan2(midy-y,midx-x)+180) Mod 360 If roll>=arc And roll<=arc+alpha And spat(x,y,midx,midy)<=rad Then Return 1 Else Return 0 EndIf EndIf If (arc+alpha) > 360 Then roll = (ATan2(midy-y,midx-x)+180) Mod 360 colli = 0 If roll>=arc And roll<=360 And spat(x,y,midx,midy)<=rad Then colli = 1 If roll>=0 And roll<=(arc+alpha)-360 And spat(x,y,midx,midy)<=rad Then colli = 1 Return colli EndIf End Function x = xkoordinate des punkts y = ykoordinate des punkts midx = xkoordinate des Scheitelpunkts midy = ykoordinate des Scheitelpunkts arc = winkel des Kreiausschnitts alpha = größe des winkels des Kreisausschnittes rad = Radius des Kreiausschnitts und hier mal ein Beispielprogramm, Steuerung: a und s : Kreisausschnitt drehen d und f : Winkel vergrößern/verkleinern bildauf und bildab : Größe ändern Pfeiltasten: Kreisausschnitt verschieben Maus : Den Punkt verschieben : Code: [AUSKLAPPEN] AppTitle "SegColl"
Graphics 640,480,16,2 SetBuffer BackBuffer() HidePointer Global roll# = 0 Global xcor# = 320 Global ycor# = 240 Global rot# = 20 Global winkel# = 30 Global size# = 120 Function spat(x1,y1,x2,y2) Return Sqr((x1-x2)^2+(y1-y2)^2) End Function Function segcoll(x,y,midx,midy,arc,alpha,rad) If alpha = 0 Then Return 0 If (arc+alpha) < 360 Then roll = (ATan2(midy-y,midx-x)+180) Mod 360 If roll>=arc And roll<=arc+alpha And spat(x,y,midx,midy)<=rad Then Return 1 Else Return 0 EndIf EndIf If (arc+alpha) > 360 Then roll = (ATan2(midy-y,midx-x)+180) Mod 360 colli = 0 If roll>=arc And roll<=360 And spat(x,y,midx,midy)<=rad Then colli = 1 If roll>=0 And roll<=(arc+alpha)-360 And spat(x,y,midx,midy)<=rad Then colli = 1 Return colli EndIf End Function Repeat mausx = MouseX() mausy = MouseY() If KeyDown(30) Then rot = rot - 1 If KeyDown(31) Then rot = rot + 1 If KeyDown(32) Then winkel = winkel - 1 If KeyDown(33) Then winkel = winkel + 1 If KeyDown(200) Then ycor = ycor - 1 If KeyDown(208) Then ycor = ycor + 1 If KeyDown(203) Then xcor = xcor - 1 If KeyDown(205) Then xcor = xcor + 1 If KeyDown(209) Then size = size - 1 If KeyDown(201) Then size = size + 1 If rot<0 Then rot=360 If rot>360 Then rot = 0 If winkel<0 Then winkel=0 pulse = segcoll(mausx,mausy,xcor,ycor,rot,winkel,size) Color 255,255,255 Text 0,0,rot,0,0 Text 0,10,winkel,0,0 Text 0,20,size,0,0 Text 0,30,pulse,0,0 Color 255,0,0 Line xcor,ycor,Cos(rot)*size+xcor,Sin(rot)*size+ycor Text Cos(rot)*(size+10)+xcor,Sin(rot)*(size+10)+ycor,1 Color 0,0,255 Line xcor,ycor,Cos(rot+winkel)*size+xcor,Sin(rot+winkel)*size+ycor Text Cos(rot+winkel)*(size+10)+xcor,Sin(rot+winkel)*(size+10)+ycor,2 Color 0,255,0 ;If rot1<rot2 Then For i = rot To rot+winkel Plot Cos(i)*size+xcor,Sin(i)*size+ycor Next ;EndIf ;If rot1>rot2 Then ;For i = rot2 To rot1 ;Plot Cos(i)*size+xcor,Sin(i)*size+ycor ;Next ;EndIf Color 255,255,0 Rect mausx-5,mausy,11,1 Rect mausx,mausy-5,1,11 Flip Cls Until KeyHit(1) End |
||
<Wing Avenger Download> ◊◊◊ <Macrophage Download> |
![]() |
Tobchen |
![]() Antworten mit Zitat ![]() |
---|---|---|
Einfach super. Nun ärgere ich mich, weil ich für mein Spiel das äußerst aufwendig zu programmierende Gegnersichtfeld programmiert habe... | ||
Tobchen - die Welt von Tobi!
|
Übersicht


Powered by phpBB © 2001 - 2006, phpBB Group