Blame |
Last modification |
View Log
| RSS feed
/*--------------------------------------------------------------*/
/* SIMULATION OF JUMPING BALLS */
/*--------------------------------------------------------------*/
/* CVS $Id: ball.c,v 1.1.1.1 2004-05-24 18:03:47 giacomo Exp $ */
#include "demo.h"
#include <kernel/func.h>
#include <stdlib.h>
#define R 2 /* dimension of a ball */
#define G 9.8 /* acceleration of gravity */
static int ballexit
= 0;
static int npc
= 0; /* number of tasks created */
/*--------------------------------------------------------------*/
/* Periodic task for ball simulation */
/*--------------------------------------------------------------*/
TASK palla
(int i
)
{
int x
, y
; /* coordinate grafiche pallina */
int ox
, oy
; /* vecchia posizione pallina */
int x0
, y0
; /* posizione iniziale X pallina */
float vx
, vy
; /* velocitÂ… della pallina */
float vy0
; /* velocita' pallina al primo rimbalzo */
float ty
, tx
; /* variabile temporale */
float dt
; /* incremento temporale */
y
= oy
= y0
= BALL_HEIGHT
;
x
= ox
= x0
= BALL_XMIN
;
vy0
= sqrt(2.
* G
* (float)BALL_HEIGHT
);
vy
= 0;
vx
= BALL_VELX
+ rand()%9;
tx
= 0;
ty
= 0;
dt
= ((float)PERIOD_BALL
)/100000;
while (1) {
y
= y0
+ vy
*ty
- .5*G
*ty
*ty
;
x
= x0
+ vx
* tx
;
if (y
< 0) {
y
= 0;
if (vy
== 0.0)
vy
= vy0
;
else if (vy
< BALL_VYMIN
)
vy
= vy0
* (1.0 - (rand()%50)/100.0);
else
vy
= 0.9 * vy
;
ty
= 0.0;
y0
= 0;
}
if (x
> BALL_XMAX
) {
tx
= 0.0;
x0
= BALL_XMAX
;
vx
= -vx
;
x
= x0
+ vx
* tx
;
}
if (x
< BALL_XMIN
) {
tx
= 0.0;
x0
= BALL_XMIN
;
vx
= -vx
;
x
= x0
+ vx
* tx
;
}
mutex_lock
(&mutex
);
grx_disc
(ox
, oy
, R
, 0);
ox
= x
;
oy
= BALL_Y
- y
;
mutex_unlock
(&mutex
);
if (ballexit
) {
npc
--;
return 0;
}
mutex_lock
(&mutex
);
grx_disc
(ox
, oy
, R
, i
);
mutex_unlock
(&mutex
);
{
int xxx
;
for (xxx
=0; xxx
<10000; xxx
++);
}
ty
+= dt
;
tx
+= dt
;
task_endcycle
();
}
}
void killball
(KEY_EVT
*k
)
{
ballexit
= 1;
}
void ballfun
(KEY_EVT
*k
)
{
SOFT_TASK_MODEL mp
;
int r
,g
,b
;
PID pid
;
if (npc
== BALL_MAX_P
) return;
ballexit
= 0;
r
= 64 + rand()%192;
g
= 64 + rand()%192;
b
= 64 + rand()%192;
soft_task_default_model
(mp
);
soft_task_def_level
(mp
,1);
soft_task_def_ctrl_jet
(mp
);
soft_task_def_arg
(mp
, (void *)rgb16
(r
,g
,b
));
soft_task_def_group
(mp
, BALL_GROUP
);
soft_task_def_met
(mp
, WCET_BALL
);
soft_task_def_period
(mp
,PERIOD_BALL
);
soft_task_def_usemath
(mp
);
pid
= task_create
("palla", palla
, &mp
, NULL
);
if (pid
!= NIL
) {
task_activate
(pid
);
npc
++;
}
}
/*--------------------------------------------------------------*/
/* MAIN process */
/*--------------------------------------------------------------*/
void scenario_ball
()
{
grx_text
("Noise", 0, BALL_Y
-BALL_HEIGHT
-15, rgb16
(0,0,255), black
);
grx_line
(0,BALL_Y
-BALL_HEIGHT
-6,383,BALL_Y
-BALL_HEIGHT
-6,red
);
}
void init_ball
(void)
{
KEY_EVT k
;
mutex_lock
(&mutex
);
grx_rect
(BALL_XMIN
-R
-1, BALL_Y
-BALL_HEIGHT
-R
-1,
BALL_XMAX
+R
+1, BALL_Y
+R
+1, rgb16
(0,200,0));
mutex_unlock
(&mutex
);
k.
flag = 0;
k.
scan = KEY_SPC
;
k.
ascii = ' ';
keyb_hook
(k
,ballfun
);
k.
flag = 0;
k.
scan = KEY_BKS
;
k.
ascii = ' ';
keyb_hook
(k
,killball
);
}
/*--------------------------------------------------------------*/