tangens Berechnen

Über alles generelle über den Point & Click DK Adventure Creator
Post Reply
japanhonk
PaC-DK God
Posts: 1716
Joined: 05 Feb 2009, 20:36
Location: NRW
Contact:

tangens Berechnen

Post: # 90977Post japanhonk
09 Jan 2022, 09:45

Hi und frohes Neues Jahr 2022.

Ja, wie man im Titel sieht, müsste ich einen Winkel berechnen ( Star Trek Fyne - ich will es besser hinkriegen, als je zuvor...)

Ich habe die x-y Koordinaten innerhalb eines ( virtuellen ) 360 Grad Kreises, kann also Ankathete a und Gegenkathete b bestimmen, damit also den tan Alpha ausrechnen. Soweit , so gut.

Ich müsste aber den tang Alpha in den Winkel Alpha umrechnen. Die Formel wäre ja Alpha= tang hoch -1 * (a/b).

Leider gibts ja keine trigonomischen Funktionen, also .. WIE eingeben ??


Gruß

JH
Es gibt 10 Arten von Usern :
die, die Binärcode verstehen und die, die es nicht tun....
----------------------------------------------------------
STAR TREK FYNE :
http://www.fynegames.homepage.t-online.de

Zimond
Der Engine Papa
Posts: 3420
Joined: 06 Apr 2003, 19:34
Location: Krefeld
Contact:

Re: tangens Berechnen

Post: # 90978Post Zimond
09 Jan 2022, 17:12

Es gibt zumindest eine Funktion Arcsin (Arkussinus) die hatte ich eingebaut eben genau für solche Fälle. Ich glaub das müsste auch dir helfen. Beispiel aus der Hilfe :

Code: Select all

Berechnet den Arkussinus einer Zahl. [Variabeln] können als Parameter eingetragen werden.

Dieses Beispiel berechnet den Winkel 0-90° der Mausposition im Bezug zu einem Mittelpunkt.

   setnum (CenterX ; 500)
   setnum (CenterY ; 500)

   (*1st get absolute distance from center
   by calculating with pytagoras.*)
   setnum (A ; [mouseX]-[CenterX])
   if_num (A ; < 0)
    setnum (A ; 0-[A])
   if_num (A ; < 1)
    setnum (A ; 1)
   setnum (B ; [mouseY]-[CenterY])
   if_num (B ; < 0)
    setnum (B ; 0-[B])
   if_num (B ; < 1)
    setnum (B ; 1)

   setnum (C ; [A]*[A] + [B]*[B])
   sqrt (C)
   if_num (C ; < 1)
    setnum (C ; 1)

    (*Get the angle by dividing B by C *)
    
    setnum (Angle ; [B] / [c])

   (*Getting degree vom Angle by checking a table*)
   arcsin (angle)
Im Demo Adventure berechne ich das Dart Minigame ähnlich, lass mich das mal eben raussuchen.
OK. Das hab ich mittels einer Vorsortierung und einer Tabelle gelöst.

Code: Select all

setobj (Darts_Cross ; 0)
wait(1)
playsound (Darts_Throw)
setstring (DartsLabel ; [empty])


(*Display small dart graphic at the position the player clicked*)
if_num (DartShots ; 3)
 {
 setobj (Darts_Shotsleft2 ; 0)
 moveobj (Darts_Dart ;  [ThrowX] - 15; [ThrowY] - 4 ; 0)
 if_num (ThrowX ; < 508)
  setobj (Darts_Dart ; 1)
 if_num (ThrowX ; > 507)
  setobj (Darts_Dart ; 2)
 }

if_num (DartShots ; 2)
 {
 setobj (Darts_Shotsleft1 ; 0)
 moveobj (Darts_Dart1 ;  [ThrowX] - 15; [ThrowY] - 4 ; 0)
 if_num (ThrowX ; < 508)
  setobj (Darts_Dart1 ; 1)
 if_num (ThrowX ; > 507)
  setobj (Darts_Dart1 ; 2)
 }

if_num (DartShots ; 1)
 {
 setobj (Darts_Shotsleft ; 0)
 moveobj (Darts_Dart2 ;  [ThrowX] - 15; [ThrowY] - 4 ; 0)
 if_num (ThrowX ; < 508)
  setobj (Darts_Dart2 ; 1)
 if_num (ThrowX ; > 507)
  setobj (Darts_Dart2 ; 2)
 }

wait(1)

 (*Calculate Hit*)

(*Center of the dart board in pixel*)
setnum (centerX ; 508)
setnum (centerY ; 346)

(*1st get absolute distance from center
by calculating with pytagoras.*)
setnum (A ; [mouseX]-[CenterX])
if_num (A ; < 0)
 setnum (A ; 0-[A])
if_num (A ; < 1)
 setnum (A ; 1)
setnum (B ; [mouseY]-[CenterY])
if_num (B ; < 0)
 setnum (B ; 0-[B])
if_num (B ; < 1)
 setnum (B ; 1)

setnum (C ; [A]*[A] + [B]*[B])
sqrt (C)
if_num (C ; < 1)
 setnum (C ; 1)

(*Get the angle by dividing B by C
Winkel is german for angle
*)
setnum (Winkel ; [B] / [c] * 1000)

(*Check in which quarter of the board the dart is*)
if_num (mousex ; [centerX])
 if_num (mouseY ; [centerY])
  setstring (DartArea ; Bullseye)
(*20 - 6*)
if_num (mousex ; > [centerX])
 if_num (mousey ; < [centerY])
  setstring (DartArea ; UpRight)
(*6 - 3*)
if_num (mousex ; > [centerX])
 if_num (mousey ; > [centerY])
  setstring (DartArea ; DownRight)
(*3 - 11*)
if_num (mousex ; < [centerX])
 if_num (mousey ; > [centerY])
  setstring (DartArea ; DownLeft)
(*11 - 20*)
if_num (mousex ; < [centerX])
 if_num (mousey ; < [centerY])
  setstring (DartArea ; UpLeft)

(*Now check what number was hit by looking at the angle and area*)
if_string (DartArea ; Bullseye)
 {
 setnum (Hit ; 50)
 setstring (DartsLabel ; 50)
 setnum (Section ; 50)
 }
if_string (DartArea ; UpRight)
 {
 if_num (Winkel ; < 156)
  setnum (Section ; 6)
 if_num (Winkel ; > 155)
  if_num (Winkel ; < 453)
   setnum (Section ; 13)
 if_num (Winkel ; > 452)
  if_num (Winkel ; < 707)
   setnum (Section ; 4)
 if_num (Winkel ; > 706)
  if_num (Winkel ; < 891)
   setnum (Section ; 18)
 if_num (Winkel ; > 890)
  if_num (Winkel ; < 987)
   setnum (Section ; 1)
  if_num (Winkel ; > 986)
   setnum (Section ; 20)
 }
if_string (DartArea ; UpLeft)
 {
 if_num (Winkel ; < 156)
  setnum (Section ; 11)
 if_num (Winkel ; > 155)
  if_num (Winkel ; < 453)
   setnum (Section ; 14)
 if_num (Winkel ; > 452)
  if_num (Winkel ; < 707)
   setnum (Section ; 9)
 if_num (Winkel ; > 706)
  if_num (Winkel ; < 891)
   setnum (Section ; 12)
 if_num (Winkel ; > 890)
  if_num (Winkel ; < 987)
   setnum (Section ; 5)
  if_num (Winkel ; > 986)
   setnum (Section ; 20)
 }
if_string (DartArea ; DownRight)
 {
 if_num (Winkel ; < 156)
  setnum (Section ; 6)
 if_num (Winkel ; > 155)
  if_num (Winkel ; < 453)
   setnum (Section ; 10)
 if_num (Winkel ; > 452)
  if_num (Winkel ; < 707)
   setnum (Section ; 15)
 if_num (Winkel ; > 706)
  if_num (Winkel ; < 891)
   setnum (Section ; 2)
 if_num (Winkel ; > 890)
  if_num (Winkel ; < 987)
   setnum (Section ; 17)
  if_num (Winkel ; > 986)
   setnum (Section ; 3)
 }

if_string (DartArea ; DownLeft)
 {
 if_num (Winkel ; < 156)
  setnum (Section ; 11)
 if_num (Winkel ; > 155)
  if_num (Winkel ; < 453)
   setnum (Section ; 8)
 if_num (Winkel ; > 452)
  if_num (Winkel ; < 707)
   setnum (Section ; 16)
 if_num (Winkel ; > 706)
  if_num (Winkel ; < 891)
   setnum (Section ; 7)
 if_num (Winkel ; > 890)
  if_num (Winkel ; < 987)
   setnum (Section ; 19)
  if_num (Winkel ; > 986)
   setnum (Section ; 3)
 }

(*And finally we look at the distance to know if the dart hit
a single, double,triple or bullseye cicrle*)

if_num (C ; < 7)
 {
 setstring (Dartcircle ; Bullseye)
 setnum (Hit ; 50)
 setstring (DartsLabel ; 50)
 }
if_num (C ; > 6)
 if_num (C ; < 11)
  {
  setstring (Dartcircle ; Bullsring)
  setnum (Hit ; 25)
  setstring (DartsLabel ; 25)
  }
if_num (C ; > 10)
 if_num (C ; < 63)
  {
  setstring (Dartcircle ; Normal)
  setnum (Hit ; [Section])
  setstring (DartsLabel ; [Section])
  }
if_num (C ; > 62)
 if_num (C ; < 69)
  {
  setstring (Dartcircle ; Triple)
  setnum (Hit ; 3*[Section])
  setstring (DartsLabel ; Tripple [Section])
  }
if_num (C ; > 68)
 if_num (C ; < 102)
  {
  setstring (Dartcircle ; Normal)
  setnum (Hit ; [Section])
  setstring (DartsLabel ; [Section])
  }
if_num (C ; > 101)
 if_num (C ; < 109)
  {
  setstring (Dartcircle ; Double)
  setnum (Hit ; 2*[Section])
  setstring (DartsLabel ; Double [Section])
  }
if_num (C ; > 108)
  {
  setstring (Dartcircle ; Out)
  setnum (Hit ; 0)
  setstring (Dartslabel ; Out)
  }

(*Add result to score*)
setnum (NewScore ; [DartsScore]-[hit])
(*reset Score if overshot.*)
if_num (NewScore ; < 0)
 {
 playsound (Darts_Overshot)
 setstring (DartsLabel ; Overshot)
 wait(1,5)
 setnum (Dartsscore ; [DartsBackupScore])
 setstring (DartsScore ; [Dartsscore])
 }

ifnot_num (NewScore ; < 0)
 {
 (*Lower Score*)
 wait(2)
 setstring (DartsLabel ; [empty])
 setnum (Dartsscore ; [Newscore])
 setstring (DartsScore ; [Dartsscore])
 wait(0,5)
 }

(*End Game when score is exactly 0*)
if_num (DartsScore ; 0)
 {
 setstring (DartsLabel ; Game Over)
 setbool (Arcade_Darts ; true)
 playsound (Darts_Gameover)
 wait(3)

 (*go Back to normal game*)
 enablemouse (true)
 showmouse (true)
 enablemenu (true)
 unloadroom ()
 wait(1)
 setfocus(ben)

 function (afterload)
 break()
 }


(*Preparing Next Throw*)
playsound (Darts_Next)
if_num (DartShots ; 3)
 setstring (DartsLabel ; Throw 2)
if_num (DartShots ; 2)
 setstring (DartsLabel ; Throw 3)
if_num (DartShots ; 1)
 {
 setnum (DartsBackupscore ; [Dartsscore])
 setstring (DartsLabel ; Throw 1)
 setobj (Darts_Shotsleft ; 1)
 setobj (Darts_Shotsleft1 ; 1)
 setobj (Darts_Shotsleft2 ; 1)
 setobj (Darts_dart ; 0)
 setobj (Darts_dart1 ; 0)
 setobj (Darts_dart2 ; 0)
 }
setnum (DartShots ; -1)
setobj (Darts_Cross ; 1)

if_num (DartShots ; 0)
 setnum (Dartshots ; 3)
Quasi erstmal gucken im welchem Quadranten des Kreises du bist und dann nehm ich den Wert von B / C * 1000 und erhalte einen Wert zwischen 0 und 1000 den ich dann Tabellenartig überprüfe um zu wissen in welchen Dartscheibensegment ich bin.
Image

japanhonk
PaC-DK God
Posts: 1716
Joined: 05 Feb 2009, 20:36
Location: NRW
Contact:

Re: tangens Berechnen

Post: # 90979Post japanhonk
09 Jan 2022, 19:28

Junge junge, wo hast Du das denn die ganze Zeit versteckt ??? :shock:

Danke, ich wühle mich mal da durch. Mal sehen, was draus wird... :mrgreen:

Manni
Es gibt 10 Arten von Usern :
die, die Binärcode verstehen und die, die es nicht tun....
----------------------------------------------------------
STAR TREK FYNE :
http://www.fynegames.homepage.t-online.de

japanhonk
PaC-DK God
Posts: 1716
Joined: 05 Feb 2009, 20:36
Location: NRW
Contact:

Re: tangens Berechnen

Post: # 90980Post japanhonk
11 Jan 2022, 17:59

Ich musste CenterX und CenterY vertauschen, dann passt es. Je nach Quadranten ( rechts oben, rechts unten usw ) muss ich das noch versuchen, anzupassen, dann könnte es genau das sein, was ich brauche. Es sieht vielversprechend aus.

Gepreiset seist Du, Engine Papa ! :rock:
Es gibt 10 Arten von Usern :
die, die Binärcode verstehen und die, die es nicht tun....
----------------------------------------------------------
STAR TREK FYNE :
http://www.fynegames.homepage.t-online.de

japanhonk
PaC-DK God
Posts: 1716
Joined: 05 Feb 2009, 20:36
Location: NRW
Contact:

Re: tangens Berechnen

Post: # 90982Post japanhonk
15 Jan 2022, 09:51

SO - PROBLEM GELÖST !!!

Um eine virtuelle Bildschirmmitte meines Raumschiffs sollte der Mauszeiger einen Kurs von 0 - 360 Grad anzeigen. Im 3 D Raum wird daraus später bei 0 Grad eine Drehung nach oben und bei 90 Grad zB eine Drehung nach rechts. Die sichtbaren Sterne und Objekte sollen dabei natürlich in die jeweils entgegengesetzte Richtung laufen.

Im Prinzip habe ich den Bildschirm mit einem Kreuz in der Mitte also in 4 Quadranten geteilt und durch Vertauschen der Variablen jeweils die (gedachte) Ankathete oder die Gegenkathete bewegt und daraus Hypotenuse und Winkel berechnen können.

Jeder Quadrant spuckte dabei einen Winkel von 0 - 90 Grad aus. Das Ganze wurde dann horizontal und vertikal gespiegelt und
umgerechnet, so dass die einzelnen 90 Grad Winkel in einen fortlaufenden 360 Grad Kurs umgerechnet wurden

DANKE :rock: :rock: :rock:

Hier das Skript:

(* Funktionen anhalten, wenn Mauszeiger den Bereich verlässt*)
if_num (xmouse;<210)
break()
if_num (xmouse;>440)
break()
if_num (ymouse;>280)
break ()
if_num (ymouse;<150)
break ()

setnum (circle;360)

(*Bildschirmhälfte LINKS abfragen und Winkel in 360 Grad Kurs umrechnen*)
if_num (xmouse;<320)
{
(*Quadrant OBEN links*)
if_num (ymouse;<200)
{
setnum (A ; [mousey]-[CenterY])
if_num (A ; < 0)
setnum (A ; 0-[A])
if_num (A ; < 1)
setnum (A ; 1)
setnum (B ; [mousex]-[CenterX])
if_num (B ; < 0)
setnum (B ; 0-)
if_num (B ; < 1)
setnum (B ; 1)

setnum (C ; [A]*[A] + *)
sqrt (C)
if_num (C ; < 1)
setnum (C ; 1)
setnum (Angle ; / [c])
arcsin (angle)
setnum (course;[circle]-[angle])
}
(*Quadrant UNTEN links *)
if_num (ymouse;>200)
{
setnum (A ; [mousey]-[CenterY])
if_num (A ; < 0)
setnum (A ; 0-[A])
if_num (A ; < 1)
setnum (A ; 1)
setnum (B ; [mousex]-[CenterX])
if_num (B ; < 0)
setnum (B ; 0-)
if_num (B ; < 1)
setnum (B ; 1)

setnum (C ; [A]*[A] + *)
sqrt (C)
if_num (C ; < 1)
setnum (C ; 1)

setnum (Angle ; / [c])
arcsin (angle)
setnum (course;[angle]+180)
}
}

(*Rechte Bildschirmhälfte abfragen und Winkel in 360 Grad Kurs umrechnen*)
if_num (xmouse;>320)
{
(*Rechts OBEN*)
if_num (ymouse;<200)
{
setnum (A ; [CenterY]-[mousey])
if_num (A ; < 0)
setnum (A ; 0-[A])
if_num (A ; < 1)
setnum (A ; 1)
setnum (B ; [CenterX]-[mousex])
if_num (B ; < 0)
setnum (B ; 0-)
if_num (B ; < 1)
setnum (B ; 1)

setnum (C ; [A]*[A] + *[B])
sqrt (C)
if_num (C ; < 1)
setnum (C ; 1)

setnum (Angle ; [B] / [C])
arcsin (angle)
setnum (course;[angle])
}
(*Rechts UNTEN*)
if_num (ymouse;>200)
{
setnum (A ; [mousex]-[CenterX])
if_num (A ; < 0)
setnum (A ; 0-[A])
if_num (A ; < 1)
setnum (A ; 1)
setnum (B ; [mousey]-[CenterY])
if_num (B ; < 0)
setnum (B ; 0-[B])
if_num (B ; < 1)
setnum (B ; 1)

setnum (C ; [A]*[A] + [B]*[B])
sqrt (C)
if_num (C ; < 1)
setnum (C ; 1)

setnum (Angle ; [B] / [c])
arcsin (angle)
setnum (course;[angle]+90)
}
}
setnum (pardir;360-[course])
setparticles (starpoint;[particlespeed];151;[pardir];0;80)
startparticles ()
Es gibt 10 Arten von Usern :
die, die Binärcode verstehen und die, die es nicht tun....
----------------------------------------------------------
STAR TREK FYNE :
http://www.fynegames.homepage.t-online.de

Zimond
Der Engine Papa
Posts: 3420
Joined: 06 Apr 2003, 19:34
Location: Krefeld
Contact:

Re: tangens Berechnen

Post: # 90983Post Zimond
15 Jan 2022, 20:07

geht doch immer irgendwie
Image

Post Reply