Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1085 | pj | 1 | /*--------------------------------------------------------------*/ |
2 | /* SIMULATION OF JUMPING BALLS */ |
||
3 | /*--------------------------------------------------------------*/ |
||
4 | |||
5 | /* CVS $Id: ball2.c,v 1.1.1.1 2002-09-02 09:37:41 pj Exp $ */ |
||
6 | |||
7 | |||
8 | #include "demo.h" |
||
9 | #include <kernel/func.h> |
||
10 | #include <stdlib.h> |
||
11 | //#include <drivers/glib.h> |
||
12 | //#include <drivers/keyb.h> |
||
13 | //#include <math.h> |
||
14 | |||
15 | #define R 2 /* dimension of a ball */ |
||
16 | #define G 9.8 /* acceleration of gravity */ |
||
17 | #define BASE 30 /* position of the floor */ |
||
18 | #define TOP 80 /* initial height of the ball */ |
||
19 | #define XMIN 3 /* min position X of the ball */ |
||
20 | #define XMAX 380 /* max position X of the ball */ |
||
21 | #define VELX 5. /* horizontal ball velocity */ |
||
22 | #define VMIN 11. /* velocit… minima per suono */ |
||
23 | #define ESC 27 /* ASCII code of ESCAPE key */ |
||
24 | #define MAX_P 50 /* max number of balls */ |
||
25 | |||
26 | double v0[MAX_P]; /* impact velocity with floor */ |
||
27 | |||
28 | /*--------------------------------------------------------------*/ |
||
29 | /* Periodic task for ball simulation */ |
||
30 | /*--------------------------------------------------------------*/ |
||
31 | |||
32 | TASK palla(int i) |
||
33 | { |
||
34 | int x, y; /* coordinate grafiche pallina */ |
||
35 | int ox, oy; /* vecchia posizione pallina */ |
||
36 | int x0; /* posizione iniziale X pallina */ |
||
37 | float vx, vy; /* velocit… della pallina */ |
||
38 | float t, tx; /* variabile temporale */ |
||
39 | float dt; /* incremento temporale */ |
||
40 | double arg; /* variabile di appoggio */ |
||
41 | |||
42 | y = oy = TOP; |
||
43 | x = ox = x0 = XMIN; |
||
44 | |||
45 | arg = 2. * G * (float)TOP; |
||
46 | vy = sqrt(arg); |
||
47 | vx = VELX;// + rand()%10; |
||
48 | tx = 0.0; |
||
49 | t = vy / G; |
||
50 | dt = 0.1; |
||
51 | |||
52 | while (1) { |
||
53 | y = TOP + vy*t - .5*G*t*t; |
||
54 | x = x0 + vx * tx; |
||
55 | |||
56 | if (y < BASE) { |
||
57 | t = 0.0; |
||
58 | v0[i] = .9 * v0[i]; |
||
59 | // if (v0[i]<VMIN) { |
||
60 | |||
61 | // v0[i] = sqrt(2. * G * (float)TOP); |
||
62 | // v0[i] = sqrt(arg);// * (1-((float)(rand()%20))/100); |
||
63 | //vx = vx + rand()%5 - 2; |
||
64 | //tx = 0.0; |
||
65 | //x0 = x; |
||
66 | // } |
||
67 | |||
68 | vy = v0[i]; |
||
69 | y = TOP + vy*t - .5*G*t*t; |
||
70 | } |
||
71 | |||
72 | |||
73 | if (x > XMAX) { |
||
74 | tx = 0.0; |
||
75 | x0 = XMAX; |
||
76 | vx = -vx; |
||
77 | x = x0 + vx * tx; |
||
78 | } |
||
79 | |||
80 | if (x < XMIN) { |
||
81 | tx = 0.0; |
||
82 | x0 = XMIN; |
||
83 | vx = -vx; |
||
84 | x = x0 + vx * tx; |
||
85 | } |
||
86 | y = 480-y; |
||
87 | |||
88 | mutex_lock(&mutex); |
||
89 | grx_disc(ox, oy, R, 0); |
||
90 | grx_disc(x, y, R, i); |
||
91 | mutex_unlock(&mutex); |
||
92 | |||
93 | oy = y; ox = x; |
||
94 | t += dt; |
||
95 | tx += dt; |
||
96 | task_endcycle(); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | |||
101 | void ballfun(KEY_EVT *k) |
||
102 | { |
||
103 | static int npc = 0; /* number of tasks created */ |
||
104 | SOFT_TASK_MODEL mp; |
||
105 | int r,g,b; |
||
106 | PID pid; |
||
107 | |||
108 | if (npc == MAX_P) return; |
||
109 | |||
110 | r = 64 + rand()%192; |
||
111 | g = 64 + rand()%192; |
||
112 | b = 64 + rand()%192; |
||
113 | |||
114 | soft_task_default_model(mp); |
||
115 | soft_task_def_level(mp,1); |
||
116 | soft_task_def_ctrl_jet(mp); |
||
117 | soft_task_def_arg(mp, rgb16(r,g,b)); |
||
118 | soft_task_def_group(mp, 1); |
||
119 | soft_task_def_met(mp, WCET_BALL); |
||
120 | soft_task_def_period(mp,PERIOD_BALL); |
||
121 | soft_task_def_usemath(mp); |
||
122 | pid = task_create("palla", palla, &mp, NULL); |
||
123 | if (pid != NIL) { |
||
124 | task_activate(pid); |
||
125 | npc++; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | |||
130 | /*--------------------------------------------------------------*/ |
||
131 | /* MAIN process */ |
||
132 | /*--------------------------------------------------------------*/ |
||
133 | |||
134 | |||
135 | void init_ball(void) |
||
136 | { |
||
137 | char c; /* character from keyboard */ |
||
138 | int i; /* pressed number */ |
||
139 | double arg; /* temporary variable */ |
||
140 | KEY_EVT k; |
||
141 | |||
142 | arg = 2. * G * (float)TOP; |
||
143 | for (i=0; i<MAX_P; i++) v0[i] = sqrt(arg); |
||
144 | |||
145 | mutex_lock(&mutex); |
||
146 | grx_rect(XMIN-R-1, 480-TOP-BASE-R-1, XMAX+R+1, 480-BASE+R+1, 14); |
||
147 | mutex_unlock(&mutex); |
||
148 | |||
149 | k.flag = 0; |
||
150 | k.scan = KEY_SPC; |
||
151 | k.ascii = ' '; |
||
152 | keyb_hook(k,ballfun); |
||
153 | |||
154 | } |
||
155 | |||
156 | /*--------------------------------------------------------------*/ |