GLBasic forum

Other languages => GLBasic - de => Topic started by: backslider on 2010-Mar-17

Title: Kanonenkugel direction berechnen
Post by: backslider on 2010-Mar-17
Hi Leute!

Ich habs irgendwie immernoch nicht ganz begriffen, wie ich Winkel berechne  :x
Kann mir wer helfen und mir zuersteinmal sagen, wie ich den Ausgangswinkel relativ zum Kanonenrohr berechne?

Code (glbasic) Select
// --------------------------------- //
// Project: cannon
// Start: Wednesday, March 17, 2010
// IDE Version: 7.082

TYPE Tcannon
x
y
power
phi
isAlive
shoot
ENDTYPE

TYPE Tbowl
x
y
dir_x
dir_y
ENDTYPE


SETCURRENTDIR("Media") // seperate media and binaries?

//GFX
LOADSPRITE "cannon.png", 0 //cannon-foot
LOADSPRITE "cannon1.png", 1 //cannon
LOADSPRITE "bowl.png", 2 //cannon bowl

//GLOBALS
GLOBAL scr_x, scr_y
GETSCREENSIZE scr_x, scr_y //get the screensize into scr_x and scr_y

GLOBAL cannon AS Tcannon //Create a newcannonn
GLOBAL newBowl AS Tbowl
GLOBAL bowls[] AS Tbowl

//Set the cannon properties
cannon.x = scr_x/2-64
cannon.y = scr_y-38
cannon.power = 1
cannon.phi = 0
cannon.isAlive = TRUE

//CONSTANTS
GLOBAL LEFT% = 203
GLOBAL RIGHT% = 205
GLOBAL SPACE% = 57

WHILE TRUE //main loop

DRAWRECT 0,0,scr_x,scr_y,RGB(200,200,255)

//For each shot do this
FOREACH Shot IN bowls[]
INC Shot.x, Shot.dir_x
INC Shot.y, Shot.dir_y

DRAWSPRITE 2,Shot.x,Shot.y //Draw the cannon ball
NEXT

IF cannon.isAlive = TRUE
IF KEY(LEFT%) //turn left
IF cannon.phi<180
INC cannon.phi, 2
ENDIF
ENDIF

IF KEY(RIGHT%) //turn right
IF cannon.phi>0
DEC cannon.phi, 2
ENDIF
ENDIF

IF KEY(SPACE%) //shoot
IF cannon.shoot

//create a new shot
newBowl.x = cannon.x + 80
newBowl.y = cannon.y + 16
newBowl.dir_x = SIN(cannon.phi)                  //??????????????????????????
newBowl.dir_y = COS(cannon.phi)      //??????????????????????????

DIMPUSH bowls[], newBowl

cannon.shoot = FALSE //only one shot per press
ENDIF
ELSE
cannon.shoot = TRUE //if space = unpressed then shoot = true
ENDIF

ROTOSPRITE 1, cannon.x, cannon.y, cannon.phi //Draw the cannon
DRAWSPRITE 0,scr_x/2-16,scr_y-32 //Draw the cannon-foot
ENDIF

SHOWSCREEN //Draw the screen
WEND //aaaaaand again


Danke im Voraus!   :booze:
Title: Re: Kanonenkugel direction berechnen
Post by: Schnatterplatsch on 2010-Mar-17
Hab grade keine Zeit mir den Code anzuschauen, aber nur für den Fall, dass du die Richtung von Punkt 1 zu Punkt 2 brauchst:

QuoteFUNCTION Get_Angle: x1 , y1 , x2 , y2   

   RETURN  ATAN(-(y2 - y1) , x2 - x1)

ENDFUNCTION


EDIT:  Das scheints zu sein, oder?
Title: Re: Kanonenkugel direction berechnen
Post by: S. P. Gardebiter on 2010-Mar-17
Falls du einen Winkel angeben willst und eine Geschwindigkeit und dafür eine X und Y Geschwindigkeit haben willst, kannst du folgenden Code benutzen:

Code (glbasic) Select
XSpeed = SIN(Angle + 90) * Speed
YSpeed = COS(Angle + 90) * Speed


0 Grad sind demnach wie auf einem Geodreieck etc. rechts und 90 Grad sind oben.

Danach benutzt du folgenden Code:

Code (glbasic) Select
INC X, XSpeed
INC Y, YSpeed


Falls für eine Bewegung von Punkt 1 zu Punkt 2 einen Winkel brauchst, ist das von meinem Vorposter der richtige Code.
Title: Re: Kanonenkugel direction berechnen
Post by: backslider on 2010-Mar-18
Ah ok... Das probier ich nachher mal aus!
Ich sollte vielleicht mal ein bisschen Geometrie nachholen  :good:

Danke schonmal  ;)