~ubuntu-branches/ubuntu/oneiric/mmpong/oneiric

« back to all changes in this revision

Viewing changes to lib/linear.c

  • Committer: Bazaar Package Importer
  • Author(s): André Gaul
  • Date: 2009-01-09 16:39:01 UTC
  • Revision ID: james.westby@ubuntu.com-20090109163901-3k701q6yxwailed7
Tags: upstream-0.9
Import upstream version 0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "linear.h"
 
2
#include "helpers.h"
 
3
#include <math.h>
 
4
 
 
5
 
 
6
 
 
7
EXPORT int linear_model(igame, timediff, paddle_action)
 
8
struct gameplay *igame;
 
9
float timediff;
 
10
int (*paddle_action)(const float[2], struct gameball *);
 
11
{
 
12
        int lasttouch= (-1);
 
13
        if (igame->status != gamestatus_running) return PONG_ARGINVALID;
 
14
        // motion
 
15
        for (int idx= 0; idx <= 1; idx++)
 
16
                igame->ball.pos[idx]+= igame->ball.dir[idx] * timediff;
 
17
        // reflections (in the right order)
 
18
        int condition= PONG_SUCCESS;
 
19
        while (outside(&igame->ball)) {
 
20
                if (outby(igame->ball.pos[0]) * igame->ball.dir[0] > outby(igame->ball.pos[1]) * igame->ball.dir[1]) {
 
21
                        // determine where the ball had passed the paddle
 
22
                        int border= (igame->ball.pos[0] > .5f);
 
23
                        if (fabs(igame->ball.dir[0]) < GAME_NUM_EPS)
 
24
                                return PONG_INTERROR;
 
25
 
 
26
                        // try avoiding endless loops caused by paddle implementation issues
 
27
                        if (lasttouch == border) return PONG_INTERROR;
 
28
                        lasttouch= border;
 
29
 
 
30
                        float crosstime= ( ((float)border) - igame->ball.pos[0] ) /igame->ball.dir[0];  // solve linear equation
 
31
                        if (crosstime >GAME_NUM_EPS)
 
32
                                return PONG_INTERROR;   // back through time
 
33
                        if (hit(&igame->pad[border], igame->ball.pos[1] + igame->ball.dir[1] *crosstime)) {
 
34
                                // bounce off the paddle
 
35
                                float cpos_abs= igame->ball.pos[1] + igame->ball.dir[1] *crosstime;
 
36
                                float where[2]= { border, 0.5 };        // x coordinate
 
37
 
 
38
                                if (igame->pad[border].size >0)         // y coordinate relative to its size
 
39
                                        where[1]= (cpos_abs
 
40
                                                - igame->pad[border].mean + igame->pad[border].size/2)
 
41
                                                / igame->pad[border].size;
 
42
 
 
43
                                if ((condition= paddle_action(where, &igame->ball)) != PONG_SUCCESS)
 
44
                                        return condition;
 
45
                                // compute the new ball position after the direction and velocity have been set by paddle_action()
 
46
                                where[1]= cpos_abs;
 
47
                                for (int idx= 0; idx<= 1; idx++)        // forward from the time of collision based on the new motion vector
 
48
                                        igame->ball.pos[idx]= where[idx] + igame->ball.dir[idx] *(-crosstime);
 
49
 
 
50
                                condition= PONG_PADHIT;
 
51
                                continue;
 
52
                        }
 
53
                        // end the game
 
54
                        igame->pad_attr[!border].score++;       // <-- the model decides on the scoring system
 
55
                        return PONG_SCORE;
 
56
                }
 
57
                else {
 
58
                        // bounce off the wall
 
59
                        int border= (igame->ball.dir[1] > 0);
 
60
                        igame->ball.pos[1]= (2.f * (float)border) - igame->ball.pos[1];
 
61
                        igame->ball.dir[1]*= (-1.f);
 
62
 
 
63
                        // try avoiding endless loops caused by paddle implementation issues
 
64
                        if (lasttouch == border+2) return PONG_INTERROR;
 
65
                        lasttouch= border+2;
 
66
                }
 
67
        }
 
68
        return condition;
 
69
}
 
70