hier mal ein netter hilfreicher CODE für leute die ein Rollenspiel oder so machen wollen wechel zwar nur in 2D ist aber die Spielfigur trotzdem immer nach vorne Schaut und die Umgebung sich dreht
Das hier ist/sind die Prosedure(n) mit einem MINI-TESTPROGRAMM als demo.
Steuerung ist "W,A,S,D" und Maus ist Sichtwinkel
Code: Alles auswählen
; Berechnet den Sichtpunkt beim Winkel (w), wenn sich die Umgebung (Px,Py) um (x,y) dreht, abei ist (sw) die Sichtweite, ob es angezeigt wird
Global b.f = #PI/180
Global SichtPunkt_X.f, SichtPunkt_Y.f
Procedure SichtPunkt(x.f, y.f, w.f, Px.f, Py.f, sw.l)
If Abs(x-Px) < sw And Abs(y-Py) < sw :
Wert = 1
r.f = Sqr(Pow(x-Px,2)+Pow(y-Py,2))
t.f = ATan((y-Py)/(x-Px))/b
If x-Px < 0 : t = t + 180 : EndIf
If x-Px = 0 :
If (y-Py)<0 : t = -90 : EndIf
If (y-Py)>0 : t = 90 : EndIf
EndIf
SichtPunkt_X = Cos((t-w-270)*b)*r
SichtPunkt_Y = Sin((t-w-270)*b)*r
EndIf
ProcedureReturn Wert
EndProcedure
; Gibt den errechneten relativen X-Wert zum SichtPunkt zurück
Procedure SichtX()
ProcedureReturn SichtPunkt_X
EndProcedure
; Gibt den errechneten relativen Y-Wert zum SichtPunkt zurück
Procedure SichtY()
ProcedureReturn SichtPunkt_Y
EndProcedure
;Hier das Testprogramm dazu :
InitKeyboard()
InitMouse()
InitSprite()
Structure Punkt
x.f
y.f
EndStructure
Global NewList Punkt.Punkt()
Procedure AddPunkt(x, y)
AddElement(Punkt())
Punkt()\x = x
Punkt()\y = y
EndProcedure
For n = 1 To 1000 :
AddPunkt(Random(512)+256, Random(384)+192)
Next n
OpenScreen(1024,768,32,"TEST")
x = 512
y = 384
Repeat
ClearScreen(0)
StartDrawing(ScreenOutput())
DrawingMode(1)
FrontColor($FFFFFF)
ResetList(Punkt())
While NextElement(Punkt())
SichtPunkt(x, y, w, Punkt()\x, Punkt()\y, 1000)
Circle(512+SichtX(),384+SichtY(),2)
Wend
Circle(512,384,5)
StopDrawing()
FlipBuffers()
ExamineMouse()
w = w + MouseDeltaX()/5
ExamineKeyboard()
If KeyboardPushed(#PB_Key_W) :
x = x + Cos(w*b)*5
y = y + Sin(w*b)*5
EndIf
If KeyboardPushed(#PB_Key_S) :
x = x - Cos(w*b)*5
y = y - Sin(w*b)*5
EndIf
If KeyboardPushed(#PB_Key_A) :
x = x + Sin(w*b)*5
y = y - Cos(w*b)*5
EndIf
If KeyboardPushed(#PB_Key_D) :
x = x - Sin(w*b)*5
y = y + Cos(w*b)*5
EndIf
Until KeyboardPushed(#PB_Key_Escape)