Go to most recent revision |
Blame |
Last modification |
View Log
| RSS feed
/*--------------------------------------------------------------*/
/* SIMULATION OF JUMPING BALLS */
/*--------------------------------------------------------------*/
/* CVS $Id: ball2.c,v 1.1.1.1 2002-09-02 09:37:41 pj Exp $ */
#include "demo.h"
#include <kernel/func.h>
#include <stdlib.h>
//#include <drivers/glib.h>
//#include <drivers/keyb.h>
//#include <math.h>
#define R 2 /* dimension of a ball */
#define G 9.8 /* acceleration of gravity */
#define BASE 30 /* position of the floor */
#define TOP 80 /* initial height of the ball */
#define XMIN 3 /* min position X of the ball */
#define XMAX 380 /* max position X of the ball */
#define VELX 5. /* horizontal ball velocity */
#define VMIN 11. /* velocitÂ… minima per suono */
#define ESC 27 /* ASCII code of ESCAPE key */
#define MAX_P 50 /* max number of balls */
double v0
[MAX_P
]; /* impact velocity with floor */
/*--------------------------------------------------------------*/
/* Periodic task for ball simulation */
/*--------------------------------------------------------------*/
TASK palla
(int i
)
{
int x
, y
; /* coordinate grafiche pallina */
int ox
, oy
; /* vecchia posizione pallina */
int x0
; /* posizione iniziale X pallina */
float vx
, vy
; /* velocitÂ… della pallina */
float t
, tx
; /* variabile temporale */
float dt
; /* incremento temporale */
double arg
; /* variabile di appoggio */
y
= oy
= TOP
;
x
= ox
= x0
= XMIN
;
arg
= 2.
* G
* (float)TOP
;
vy
= sqrt(arg
);
vx
= VELX
;// + rand()%10;
tx
= 0.0;
t
= vy
/ G
;
dt
= 0.1;
while (1) {
y
= TOP
+ vy
*t
- .5*G
*t
*t
;
x
= x0
+ vx
* tx
;
if (y
< BASE
) {
t
= 0.0;
v0
[i
] = .9 * v0
[i
];
// if (v0[i]<VMIN) {
// v0[i] = sqrt(2. * G * (float)TOP);
// v0[i] = sqrt(arg);// * (1-((float)(rand()%20))/100);
//vx = vx + rand()%5 - 2;
//tx = 0.0;
//x0 = x;
// }
vy
= v0
[i
];
y
= TOP
+ vy
*t
- .5*G
*t
*t
;
}
if (x
> XMAX
) {
tx
= 0.0;
x0
= XMAX
;
vx
= -vx
;
x
= x0
+ vx
* tx
;
}
if (x
< XMIN
) {
tx
= 0.0;
x0
= XMIN
;
vx
= -vx
;
x
= x0
+ vx
* tx
;
}
y
= 480-y
;
mutex_lock
(&mutex
);
grx_disc
(ox
, oy
, R
, 0);
grx_disc
(x
, y
, R
, i
);
mutex_unlock
(&mutex
);
oy
= y
; ox
= x
;
t
+= dt
;
tx
+= dt
;
task_endcycle
();
}
}
void ballfun
(KEY_EVT
*k
)
{
static int npc
= 0; /* number of tasks created */
SOFT_TASK_MODEL mp
;
int r
,g
,b
;
PID pid
;
if (npc
== MAX_P
) return;
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
, rgb16
(r
,g
,b
));
soft_task_def_group
(mp
, 1);
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 init_ball
(void)
{
char c
; /* character from keyboard */
int i
; /* pressed number */
double arg
; /* temporary variable */
KEY_EVT k
;
arg
= 2.
* G
* (float)TOP
;
for (i
=0; i
<MAX_P
; i
++) v0
[i
] = sqrt(arg
);
mutex_lock
(&mutex
);
grx_rect
(XMIN
-R
-1, 480-TOP
-BASE
-R
-1, XMAX
+R
+1, 480-BASE
+R
+1, 14);
mutex_unlock
(&mutex
);
k.
flag = 0;
k.
scan = KEY_SPC
;
k.
ascii = ' ';
keyb_hook
(k
,ballfun
);
}
/*--------------------------------------------------------------*/