Hallo zusammen,
ich lese schon lange in diesem Forum mit, seit ich vor etwa einem Jahr mit C++ begonnen habe, heute poste das erste mal da ich ziemlich ratlos bin.
Ich habe Amarillions ersten Teil seines Tutorials "Sin & Cos: The Programmer's Pals!"
Link
übersetzt. Die Codes bin ich gerade dabei auf den neuesten Stand zu bringen. Ich werde verschiedene Versionen beilegen (SFML, SDL, Allegro 5) und noch einige mehr....!
Natürlich modernisiere ich die Codes auch. Aus heutiger Sicht können wir auf die ganzen Festkommageschten verzichten da unsere Grafikkarten schnell genug sind um lächerliche sin(), cos() und atan() Funktionen mit Fließkommawerten durchzuführen!
Angefangen habe ich mit graphics.h da diese Bibliothek sehr einfach und grundlegend ist. Mein Problem ist der Code im Teil "Using atan2(): homing missiles " (circ7.c). Die Rakete findet ihr Ziel nicht. Sie jagt auf einem chaotischen Kurs daran vorbei.
Hier ist mein Code:
|
C-/C++-Quelltext
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#include<windows.h>
#include<graphics.h>
#include<cmath>
#define PI 3.141592654
// circ7
void home_in();
int SCREEN_W = 600;
int SCREEN_H = 400;
main(int argc, char*argv[])
{
// Deklaration und Initialisierung des Grafiktreibers
int graphdriver, graphmode;
graphdriver = DETECT;
initgraph(&graphdriver, &graphmode, "Draw_Sine"); // graphics.h kann keine Windowstitel setzen
setbkcolor(0); // Hintergrundfarbe des Ausgabefensters schwarz
cleardevice(); // Löschen des Ausgabefensters
setcolor(15); // Textfarbe weiss (15)
settextstyle(10, HORIZ_DIR, 2); // Fontsyle, Richtung und Größe
//outtextxy(20, 120, "Sinuskurve zeichnen: "); // Textausgabe
home_in(); // Funktionsaufruf von draw_circle()
return 0;
}
void home_in ()
{
// the x, y position of the homing missile
int x = SCREEN_W / 2;
int y = SCREEN_H / 2;
// the angle and length of the missile's velocity vector
double angle = 0.0;
int length = 1;
double angle_stepsize = 1* PI / 180;
// determines whether the missile has reached
// the target and a new one should be chosen
bool new_target = true;
// angle to the target
double target_angle;
// position of the target
int target_x;
int target_y;
while (!GetAsyncKeyState(VK_ESCAPE))
{
setbkcolor(0); // Hintergrundfarbe des Ausgabefensters schwarz
cleardevice(); // Löschen des Ausgabefensters
// choose new target randomly when needed
if (new_target == true)
{
target_x = ((SCREEN_W + rand() % (2 * SCREEN_W)) / 4);
target_y = ((SCREEN_H + rand() % (2 * SCREEN_H)) / 4);
new_target = false;
}
// draw a pixel where the target is
putpixel ((target_x), (target_y),15);
// draw the missile
// (actually a circle with a line representing the angle)
setcolor(WHITE);
circle (x, y, 20);
line (x, y, x + (9 * cos (angle)), y + (9 * sin (angle)));
// move the missile
x = x + length * cos (angle);
y = y + length * sin (angle);
// if we are very close to the target, set a new target
if (abs (x - target_x) + abs (y - target_y) < 10)
{
new_target = true;
}
// calculate the angle from the missile to the target
target_angle = atan2 (target_y - y, target_x - x);
if ((angle - Atarget_angle) < PI)
{
angle = (angle - angle_stepsize) ;
}
else
{
angle = (angle + angle_stepsize) ;
}
delay (10);
}
}
|
Viele Grüße und danke für Eure Mühe & Hilfe,
Merlin