Rev 1085 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1085 | pj | 1 | /*--------------------------------------------------------------*/ |
2 | /* SIMULATION OF JUMPING BALLS */ |
||
3 | /*--------------------------------------------------------------*/ |
||
4 | |||
5 | /* CVS $Id: ball.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 | |||
12 | #define R 2 /* dimension of a ball */ |
||
13 | #define G 9.8 /* acceleration of gravity */ |
||
14 | |||
15 | static int ballexit = 0; |
||
16 | static int npc = 0; /* number of tasks created */ |
||
17 | |||
18 | /*--------------------------------------------------------------*/ |
||
19 | /* Periodic task for ball simulation */ |
||
20 | /*--------------------------------------------------------------*/ |
||
21 | |||
22 | TASK palla(int i) |
||
23 | { |
||
24 | int x, y; /* coordinate grafiche pallina */ |
||
25 | int ox, oy; /* vecchia posizione pallina */ |
||
26 | int x0, y0; /* posizione iniziale X pallina */ |
||
27 | float vx, vy; /* velocit… della pallina */ |
||
28 | float vy0; /* velocita' pallina al primo rimbalzo */ |
||
29 | float ty, tx; /* variabile temporale */ |
||
30 | float dt; /* incremento temporale */ |
||
31 | |||
32 | y = oy = y0 = BALL_HEIGHT; |
||
33 | x = ox = x0 = BALL_XMIN; |
||
34 | |||
35 | vy0= sqrt(2. * G * (float)BALL_HEIGHT); |
||
36 | vy = 0; |
||
37 | vx = BALL_VELX + rand()%9; |
||
38 | tx = 0; |
||
39 | ty = 0; |
||
40 | dt = ((float)PERIOD_BALL)/100000; |
||
41 | |||
42 | while (1) { |
||
43 | y = y0 + vy*ty - .5*G*ty*ty; |
||
44 | x = x0 + vx * tx; |
||
45 | |||
46 | if (y < 0) { |
||
47 | y = 0; |
||
48 | |||
49 | if (vy == 0.0) |
||
50 | vy = vy0; |
||
51 | else if (vy < BALL_VYMIN) |
||
52 | vy = vy0 * (1.0 - (rand()%50)/100.0); |
||
53 | else |
||
54 | vy = 0.9 * vy; |
||
55 | |||
56 | ty = 0.0; |
||
57 | y0 = 0; |
||
58 | } |
||
59 | |||
60 | if (x > BALL_XMAX) { |
||
61 | tx = 0.0; |
||
62 | x0 = BALL_XMAX; |
||
63 | vx = -vx; |
||
64 | x = x0 + vx * tx; |
||
65 | } |
||
66 | |||
67 | if (x < BALL_XMIN) { |
||
68 | tx = 0.0; |
||
69 | x0 = BALL_XMIN; |
||
70 | vx = -vx; |
||
71 | x = x0 + vx * tx; |
||
72 | } |
||
73 | |||
74 | mutex_lock(&mutex); |
||
75 | grx_disc(ox, oy, R, 0); |
||
76 | ox = x; |
||
77 | oy = BALL_Y - y; |
||
78 | mutex_unlock(&mutex); |
||
79 | |||
80 | if (ballexit) { |
||
81 | npc--; |
||
82 | return 0; |
||
83 | } |
||
84 | |||
85 | mutex_lock(&mutex); |
||
86 | grx_disc(ox, oy, R, i); |
||
87 | mutex_unlock(&mutex); |
||
88 | |||
89 | { |
||
90 | int xxx; |
||
91 | for (xxx=0; xxx<10000; xxx++); |
||
92 | } |
||
93 | ty += dt; |
||
94 | tx += dt; |
||
95 | task_endcycle(); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | void killball(KEY_EVT *k) |
||
100 | { |
||
101 | ballexit = 1; |
||
102 | } |
||
103 | |||
104 | void ballfun(KEY_EVT *k) |
||
105 | { |
||
106 | SOFT_TASK_MODEL mp; |
||
107 | int r,g,b; |
||
108 | PID pid; |
||
109 | |||
110 | if (npc == BALL_MAX_P) return; |
||
111 | |||
112 | ballexit = 0; |
||
113 | |||
114 | r = 64 + rand()%192; |
||
115 | g = 64 + rand()%192; |
||
116 | b = 64 + rand()%192; |
||
117 | |||
118 | soft_task_default_model(mp); |
||
119 | soft_task_def_level(mp,1); |
||
120 | soft_task_def_ctrl_jet(mp); |
||
121 | soft_task_def_arg(mp, (void *)rgb16(r,g,b)); |
||
122 | soft_task_def_group(mp, BALL_GROUP); |
||
123 | soft_task_def_met(mp, WCET_BALL); |
||
124 | soft_task_def_period(mp,PERIOD_BALL); |
||
125 | soft_task_def_usemath(mp); |
||
126 | pid = task_create("palla", palla, &mp, NULL); |
||
127 | if (pid != NIL) { |
||
128 | task_activate(pid); |
||
129 | npc++; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | |||
134 | /*--------------------------------------------------------------*/ |
||
135 | /* MAIN process */ |
||
136 | /*--------------------------------------------------------------*/ |
||
137 | |||
138 | void scenario_ball() |
||
139 | { |
||
140 | grx_text("Noise", 0, BALL_Y-BALL_HEIGHT-15, rgb16(0,0,255), black); |
||
141 | grx_line(0,BALL_Y-BALL_HEIGHT-6,383,BALL_Y-BALL_HEIGHT-6,red); |
||
142 | } |
||
143 | |||
144 | void init_ball(void) |
||
145 | { |
||
146 | KEY_EVT k; |
||
147 | |||
148 | mutex_lock(&mutex); |
||
149 | grx_rect(BALL_XMIN-R-1, BALL_Y-BALL_HEIGHT-R-1, |
||
150 | BALL_XMAX+R+1, BALL_Y+R+1, rgb16(0,200,0)); |
||
151 | mutex_unlock(&mutex); |
||
152 | |||
153 | k.flag = 0; |
||
154 | k.scan = KEY_SPC; |
||
155 | k.ascii = ' '; |
||
156 | keyb_hook(k,ballfun); |
||
157 | |||
158 | k.flag = 0; |
||
159 | k.scan = KEY_BKS; |
||
160 | k.ascii = ' '; |
||
161 | keyb_hook(k,killball); |
||
162 | } |
||
163 | |||
164 | /*--------------------------------------------------------------*/ |