object animation
play

Object animation 2 General approach Case 1: self drawing For each - PDF document

Object animation 2 General approach Case 1: self drawing For each object OBJ i define: x current position x = old_x = START_X old_x previous position data structures and state variables D x draw object at x position increment task


  1. Object animation 2 General approach Case 1: self drawing For each object OBJ i define: x current position x = old_x = START_X old_x previous position  data structures and state variables D x draw object at x position increment  task for updating state variables (period T i ) compute D x D x = x + D x D For the whole application define: D x delete object at old_x  graphic visualization (period T g ) x draw object at x  User interface & command interpreter (period T u ) old_x old_x = x For the graphic display there are two options: NOTE: If there is a background, wait_for_period() 1. Each object draws itself every period. it has to be managed. 2. All objects are drawn by a single graphic task. 3 4 Case 1: handling background Case 2: graphic task Object task  i Graphic task  g Before drawing the object, we have to save the background in a buffer: x = START_X START global buffer compute D x read state from all global buffer x = x + D x objects states display background y save state in a global buffer display objects x wait_for_period() wait_for_period() To delete the object, we can just restore the background from the buffer. 5 6 1

  2. Physical simulations Jumping balls  Modeling Let us see how to write a program that simulates a number of jumping balls that move in a box following – Derive the system model expressing the position x as a the laws of physics: function of the other state variables.  Speed must be independent of the period – compute: dx = vx * T; – For smooth motion, period should be no larger than 20 ms – If T is larger, you can multiply T by a timescale factor.  Scalability – Express physical variables in MKS units using floats; – Convert to integer coordinates only for displaying; – use offset and scale factor for graphic coordinates: xg = offset + x * scale; 7 8 Things to define General description  General description and requirements 1. Each ball is animated by a dedicated periodic task that updates its state every period.  Application structure 2. The motion of each ball is independent on the other balls,  Graphical layout but depends on its current state.  User interface 3. Balls moves within a box and bounce on its borders.  Motion rules 4. Balls lose some energy when bouncing on the floor.  Constants of the simulation 5. The user can interact with the program and can  State variables of the ball create new balls, make them jumping, vary gravity, etc.  Global variables and data structures 6. The system provides information on:  Auxiliary functions (divide them into categories) number of active balls, deadline misses, current gravity. 9 10 Application structure Application structure The task animating the ball must periodically update The state of multiple balls is stored in an array: the ball state from S ( t ) to S ( t+T ) :  1 state 1 graphic task graphic task S ( t+T ) ball  i state var.:  g state  g task ( x, y, v x , v y ) array  i state i S ( t ) Graphics Graphics parameters T i , D i g v 0 parameters T i , D i g v 0 fixed  u  u user user task 11 task 12 2

  3. Graphical layout User interface Simulations should be done in real units (MKS) and We have to decide the inputs and the outputs: only later converted in pixels. Inputs Graphic window  What variables can the user change? Menu area  Pressing which keys? Virtual world Status  Is the mouse used? (meters) Graphic world window (pixels) Outputs  Which variables are displayed? 13 14 User interface Motion rules v y  SPACE : creates a new ball with random parameters  ESC : exits the program v x ( x , y )  A : makes balls jumping (provide extra energy)   x new x curr v t v x = constant x 1 gt  W : shows/hides the ball trail    y new y curr v curr t 2 new  curr  v v gt y y y 2  LEFT / RIGHT : decrease/increase trail length  These equations must be updated every period T i  UP / DOWN : increase/decrease gravity  A time scale can be used for the integration interval: Corresponding values must be shown on the state window dt = TSCALE * T i 15 16 Makefile #--------------------------------------------------- # Target file to be compiled by default #--------------------------------------------------- MAIN = balls #--------------------------------------------------- # CC will be the compiler to use #--------------------------------------------------- CC = gcc #--------------------------------------------------- # CFLAGS will be the options passed to the compiler #--------------------------------------------------- CFLAGS = -Wall -lpthread -lrt #--------------------------------------------------- # Dependencies #--------------------------------------------------- $(MAIN) : $(MAIN) .o ptask.o $(CC) $(CFLAGS) -o $(MAIN) $(MAIN) .o ptask.o `allegro-config --libs` $(MAIN) .o: $(MAIN) .c $(CC) -c $(MAIN) .c ptask.o: ptask.c $(CC) -c ptask.c 18 3

  4. Header files Constants //----------------------------------------------------- //----------------------------------------------------- // BALLS: SIMULATION OF JUMPING BALLS // GRAPHICS CONSTANTS // with a single display task //----------------------------------------------------- //----------------------------------------------------- #define XWIN 640 // window x resolution #define YWIN 480 // window y resolution #include <stdlib.h> // include standard lib first #define BKG 0 // background color #include <stdio.h> #define MCOL 14 // menu color #include <math.h> #define NCOL 7 // numbers color #include <pthread.h> #define TCOL 15 // trail color #include <sched.h> //----------------------------------------------------- #include <allegro.h> #define LBOX 489 // X length of the ball box #include <time.h> #define HBOX 399 // Y height of the ball box #define XBOX 5 // left box X coordinate #include "ptask.h" // include own lib later #define YBOX 75 // upper box Y coordinate #include "mylib.h" #define RBOX 495 // right box X coordinate #define BBOX 475 // bottom box Y coordinate #define FLEV 5 // floor Y level (in world) 19 20 Constants State variables v y //----------------------------------------------------- // TASK CONSTANTS color //----------------------------------------------------- size v x #define PER 20 // task period in ms (x,y) #define DL 20 // relative deadline in ms #define PRI 80 // task priority struct status { // ball structure //----------------------------------------------------- int c; // color [1,15] // BALL CONSTANTS float r; // radius (m) //----------------------------------------------------- float x; // x coordinate (m) #define MAX_BALLS 20 // max number of balls float y; // y coordinate (m) #define G0 9.8 // acceleration of gravity float vx; // horizontal velocity (m/s) #define TLEN 30 // trail length float vy; // vertical velocity (m/s) #define HMIN 200 // min initial height float v0; // jumping velocity (m/s) #define HMAX 390 // max initial height }; #define VXMIN 20 // min initial hor. speed #define VXMAX 10 // max initial hor. speed struct status ball[MAX_BALLS] ; // balls status buffer #define DUMP 0.9 // dumping coefficient #define TSCALE 10 // time scale factor 21 22 Storing the trail Reading the trail It can be done using a It can be done using a circular buffer of length circular buffer of length L = max trail length: L = max trail length:  top always points to  top always points to x 1 x 2 x 3 x 4 x 5 x 6 x 1 x 2 x 3 x 4 x 5 x 6 the most recent data the most recent data top top  next element:  next element: cbuf cbuf (top + 1) % L (top + 1) % L x 1 x 2 x 3 x 4 x 5 x 6 x 2 x 3 x 4 x 5  previous element:  previous element: 0 1 2 3 4 0 1 2 3 4 (top – 1 + L) % L (top – 1 + L) % L  k th previous element: Getting the k th previous value :  k th previous element: Store a new value x: (top – k + L) % L (top – k + L) % L top = (top + 1) % L; i = (top – k + L) % L; cbuf[top] = x; value = cbuf[i]; 23 24 4

Recommend


More recommend