Gegner soll auf Spieler zukommen

Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Neue Antwort erstellen

Diablo

Betreff: Gegner soll auf Spieler zukommen

BeitragSo, Feb 17, 2013 18:37
Antworten mit Zitat
Benutzer-Profile anzeigen
Hallo,

ja ich habe die Suche bemüht und konnte da auch schon Hilfreiches finden, doch bis jetzt läuft mein Code noch nicht so wie gewünscht.

Ziel ist es, dass der Gegner sich zum Spieler hin dreht und auf diesen zu "läuft".
Das drehen habe ich mittels ATan2 ohne Probleme hinbekommen. Das auf den Spieler zulaufen ansich auch, doch läuft der Gegner nur im 90° Winkel auf den Spieler zu... Also irgendwas mache ich da noch verkehrt.

Mein Code:

BlitzMax: [AUSKLAPPEN]
Type TEnemy Extends TCreature

Global imgEnemy:TImage
Field imgFrame:Int = 0
Field resultDegree:Double


Method LoadEnemyImage()
SetMaskColor(255, 0, 255)
imgEnemy = LoadAnimImage(".\graphics\enemy1.png", 32, 32, 0, 24)
End Method

'// Gradzahl vom Bilddrehen erzeugen
Method Rotate(newDegree:Int)
Self.degree = Self.degree + newDegree
EndMethod

Method DrawEnemy(x:Int, y:Int)
SetRotation(Self.resultDegree)
Self.creatureX = x
Self.creatureY = y
DrawImage(Self.imgEnemy, Self.creatureX, Self.creatureY, imgFrame)
SetRotation(currentDegree)
End Method


Method Move(pSpeed:Int)
Self.speed = pSpeed
Self.creatureY = Self.creatureY - Cos(Self.resultDegree) * Self.speed
Self.creatureX = Self.creatureX + Sin(Self.resultDegree) * Self.speed
End Method


Method TurnObj(destinationX, destinationY, startPointX, startPointY)
resultDegree = ATan2(destinationY - startPointY, destinationX - startPointX) + 90
If resultDegree < 0 Then
resultDegree = resultDegree + 360
End If
Return resultDegree
End Method

Method UpdateState()
If ImagesCollide(player.imgPlayer, player.creatureX, player.creatureY, 0, enemy.imgEnemy, enemy.creatureX, enemy.creatureY, enemy.imgFrame) Then
If enemy.imgFrame < 23 Then
enemy.imgFrame:+1
Else
enemy.imgFrame = 0
EndIf
EndIf
enemy.Move(1.0)

degree = enemy.TurnObj(player.creatureX, player.creatureY, enemy.creatureX, enemy.creatureY)

End Method
End Type
www.pb-software.de.vu

Meine Entwicklungsseite

Xeres

Moderator

BeitragSo, Feb 17, 2013 19:00
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich bin relativ sicher, dass du Start- und Endpunkte vertauschen solltest. Außerdem kann man das ohne Bedingung etwas geschickter umschreiben nach:
BlitzMax: [AUSKLAPPEN]
Method TurnObj(destinationX, destinationY, startPointX, startPointY)
Return (ATan2(startPointY-destinationY , startPointX-destinationX ) + 360) Mod 360
End Method


Wenn dein Bild in die falsche Richtung zeigt, ist es nicht nach 0 Grad ausgerichtet.
Win10 Prof.(x64)/Ubuntu 16.04|CPU 4x3Ghz (Intel i5-4590S)|RAM 8 GB|GeForce GTX 960
Wie man Fragen richtig stellt || "Es geht nicht" || Video-Tutorial: Sinus & Cosinus
T
HERE IS NO FAIR. THERE IS NO JUSTICE. THERE IS JUST ME. (Death, Discworld)

Diablo

BeitragSo, Feb 17, 2013 19:53
Antworten mit Zitat
Benutzer-Profile anzeigen
Hmm, habe deinen Code mal ausprobiert. Leider funktioniert der insoweit nicht wie gewünscht. D.h. das Drehen des Objektes funktioniert ja auch, dass auf den Spieler zulaufen ja auch, aber immer nur im 90 Gradwinkel.

In einem Winkel von 124 z.b. bleibt der Gegner einfach stehen bzw. läuft erst weiter wenn der Spieler gerade zum Gegner steht.
www.pb-software.de.vu

Meine Entwicklungsseite

Xeres

Moderator

BeitragSo, Feb 17, 2013 20:22
Antworten mit Zitat
Benutzer-Profile anzeigen
Ich kann mir das Ergebnis wirklich nicht aus dem Code allein vorstellen - da müsstest du ein lauffähiges Beispiel bringen.
Was mir aber grade noch auffällt: Du vertauscht Sinus und Kosinus beim Bewegen...
Win10 Prof.(x64)/Ubuntu 16.04|CPU 4x3Ghz (Intel i5-4590S)|RAM 8 GB|GeForce GTX 960
Wie man Fragen richtig stellt || "Es geht nicht" || Video-Tutorial: Sinus & Cosinus
T
HERE IS NO FAIR. THERE IS NO JUSTICE. THERE IS JUST ME. (Death, Discworld)

Diablo

BeitragSo, Feb 17, 2013 21:05
Antworten mit Zitat
Benutzer-Profile anzeigen
Hier ist eine ausführbare Datei: http://pb-software.pf-control....r_demo.zip

Vielleicht hilft das ja um mein Anliegen zu unterstreichen Wink
www.pb-software.de.vu

Meine Entwicklungsseite
 

PhillipK

BeitragSo, Feb 17, 2013 23:41
Antworten mit Zitat
Benutzer-Profile anzeigen
Hab mir deins nicht angeschaut, aber ich hab dir nen simplen code geschrieben, der bewegung zu einem punkt hin zeigt Smile

BlitzMax: [AUSKLAPPEN]
SuperStrict

Type TEntity
Global list:TList = CreateList() 'globale liste um alle entity zu updaten.

Field x:Float 'position
Field y:Float

Field rotation:Float 'richtung

Field speed:Float 'bewegungstempo

Method New()
speed = Rnd(0.5, 1.5)
x = Rand(0, GraphicsWidth())
y = Rand(0, GraphicsHeight())

list.AddLast(Self)
End Method

Method pointTo(destinationX:Float, destinationY:Float)
rotation = ATan2(destinationY - y, destinationX - x)
End Method

Function Update()
Local mx:Float = MouseX()
Local my:Float = MouseY()
For Local ent:TEntity = EachIn list
ent._update(mx, my)
Next
End Function
Method _update(destX:Float, destY:Float)
Self.pointTo(destX, destY)

If Pythagoras(x, y, destX, destY) > 30 Then

Self.x:+Cos(rotation) * speed
Self.y:+Sin(rotation) * speed

EndIf

End Method

Function Draw()
For Local ent:TEntity = EachIn list
ent._draw()
Next
End Function
Method _Draw()
'zum zentrierten rotieren um entity-origin..
SetOrigin(Self.x, Self.y)
SetRotation(Self.rotation)

'zeichnen
DrawRect(-10, -5, 20, 10)
' DrawRect(x - 10, y - 5, 20, 10)

'zurücksetzen der matrix
SetOrigin(0, 0)
SetRotation(0)
End Method
End Type

Function Pythagoras:Float(x1:Float, y1:Float, x2:Float, y2:Float)
Return Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
End Function

Graphics(1024, 768)

For Local i:Int = 0 Until 5
New TEntity
Next


While Not KeyHit(KEY_ESCAPE) And Not AppTerminate()
If KeyHit(KEY_SPACE) Then New TEntity

TEntity.Update()

Cls

TEntity.Draw()
DrawText("hallo! " + MilliSecs(), 5, 5)
Flip 1
Wend


Eine recht simple methode "point to" , sowie eine distanz von 30 einheiten (in dem fall pixel) zur maus.
Mit space packst du neue "Entity" in die testumgebung - alle laufen zur maus hin.
Vielleicht hilft dir das? Smile

PSY

BeitragMo, Feb 18, 2013 0:00
Antworten mit Zitat
Benutzer-Profile anzeigen
Hoi,

hab leider kein BMax. Deine Exe sieht ganz nach Ausrichtungsproblem aus (wie Xeres schon sagte).

Hier ein Beispielcode in 3D (war zu faul fuer 2D ^^).
Wenn du bei Code: [AUSKLAPPEN]
RotateEntity e, 0, 0, faceRambo-90
die -90 rausnimmst, siehst Du was ich mein!
Hoffe Du hast B3D...

Steuerung: Cursortasten

Code: [AUSKLAPPEN]
Graphics3D 800,600,32,2
SetBuffer BackBuffer()


Local faceRambo# ; ATAN2 WINKEL AUF RAMBO
Local cam% = CreateCamera()
PositionEntity cam, 0, 0, -25

; GEGNER
Type TEnemy
   Field x#, y#
   Field speed#
End Type
Global enemy.TEnemy

; RAMBO
Type TRambo
   Field x#, y#
   Field speed#
End Type
Global rambo.TRambo

; STARTPOS GEGNER (IS IMMER LANGSAMER ALS RAMBO)
enemy = New TEnemy
enemy\x = -9.5
enemy\y = -9.5
enemy\speed = 0.03

; STARTPOS RAMBO
rambo = New TRambo
rambo\x = 10.0
rambo\y = 4.0
rambo\speed = 0.05

; GEGNER UND RAMBO CUBE ERSTELLEN
Local r% = CreateCube()
ScaleEntity r, 2, 1, .001
EntityColor r, 0, 255, 0
Local e% = CreateCube()
ScaleEntity e, 2, 1, .001
EntityColor e, 255, 0, 0

; GEGNER UND RAMBO POSITIONIEREN
PositionEntity r, rambo\x, rambo\y, 0
PositionEntity e, enemy\x, enemy\y, 0





Repeat
   ; X UND Y WERTE AUSLESEN
   rambo\x = EntityX(r)
   rambo\y = EntityY(r)
   enemy\x = EntityX(e)
   enemy\y = EntityY(e)
   
   ; RAMBO BEWEGUNG
   TurnEntity r, 0, 0, KeyDown(203)-KeyDown(205)
   MoveEntity r, 0, (KeyDown(200)-KeyDown(208)) * rambo\speed, 0
   
   ; ATAN2 WINKEL AUF RAMBO BERECHNEN
   faceRambo# = ATan2 (rambo\y-enemy\y, rambo\x - enemy\x)
   
   ; GEGNER AUF RAMBO AUSRICHTEN UND BEWEGEN (-90 WEGEN 0-AUSRICHTUNG)
   RotateEntity e, 0, 0, faceRambo-90
   MoveEntity e, 0, enemy\speed, 0
   
   RenderWorld()
   
   Text 0, 0, faceRambo
   Flip 1
   
Until KeyHit(1)
End



PSY
PSY LABS Games
Coders don't die, they just gosub without return
 

Lion

BeitragMo, Feb 18, 2013 1:40
Antworten mit Zitat
Benutzer-Profile anzeigen
Du hast die X und Y Koordinaten nicht als Float. Umändern und es geht top.
Das liegt daran, dass mit Cos/Sin Floatwerte rauskommen und er die neuen Koordinaten nicht richtig speichern kann wenn die Werte zu gering sind (<0.5 oder so, jenachdem wie BMax da aufrundet).
Intel Core 2 Quad 4x2.66 ghz - 4gb ddr2 - nvidia GeForce GTX660 2gb
Intel Atom 1x1.83 ghz - 2gb ddr2 - intel GMA 3150 256mb
AMD A10-5750M 4x2.5 ghz - 8 gb ddr4 - AMD R9 M290x

Neue Antwort erstellen


Übersicht BlitzMax, BlitzMax NG Beginners-Corner

Gehe zu:

Powered by phpBB © 2001 - 2006, phpBB Group