aboutsummaryrefslogtreecommitdiffstats
path: root/worksheets/a2_carsoccer.md
blob: da09ad0355fa1c3ac18a0272d4095cacca6ff92e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# Assignment 2 (Car Soccer) Worksheet

## Definitions

Use the following C++ style pseudocode definitions for Q1 and Q2:

```cpp
/* Use this Point3 class to store x,y,z values that define a mathematical
 * point (i.e., a position) in 3-space.
 */
class Point3 {
    float x;
    float y;
    float z;
};

/* Use this Vector3 class to store x,y,z values that define a vector in
 * 3-space.  Remember, mathematically, a vector is quite different than
 * a point.  It has a direction and a magnitude but no position!
 * For vectors it is often useful to be able to compute the length,
 * also known as the magnitude, of the vector.
 */
class Vector3  {
    float x;
    float y;
    float z;
    
    // returns the length (i.e., magnitude) of the vector
    float Length() {
        return sqrt(x*x + y*y + z*z);
    }
};


/* In C++ and other languages we can define operators so we can use
 * the +, -, =, *, / operations on custom classes.  Like many graphics
 * libraries, this is what MinGfx does to make it easy to work with
 * points and vectors in code.  For example, recall from class that
 * if we have a point A (Coffman Union) and we add a vector (direction
 * and magnitude) to this, we arrive at a new point B (e.g., Murphy Hall).
 * Conceptually, a point + a vector = a new point.  Mathematically, it
 * does not make sense to add two points, but it does make sense to 
 * subtract two points.  The "difference" between the Murphy and Coffman 
 * points is a vector that tells us the direction and magnitude we would
 * need to walk from Coffman to get to Murphy.  Here's how we can write
 * that in code using Point3, Vector3, and operators like + and -.
 *
 *   Point3 murphy = Point3(5, 8, 0);
 *   Point3 coffman = Point3(4, 6, 0);
 *   Vector3 toMurphy = murphy - coffman; 
 *
 *   // or, if we were given coffman and toMurphy we could find
 *   // the point "murphy" by starting at point "coffman" and adding
 *   // the vector "toMurphy".
 *   Point3 murphy2 = coffman + toMurhpy; 
 *
 * The code that defines these opertors looks something like this:
*/

// a point + a vector = a new point
Point3 operator+(Point3 p, Vector3 v) { 
   return Point3(p.x + v.x, p.y + v.y, p.z + v.z); 
}

// a point - a point = a vector 
// the dir and magnitude needed to go from point point B to point A
Vector3 operator-(Point3 A, Point3 B) {
    return Vector3(A.x - B.x, A.y - B.y, A.z - B.z);
}

// a vector * a scalar = a new vector with scaled magnitude
Vector3 operator*(Vector3 v, float s) {
    return Vector3(v.x * s, v.y * s, v.z * s);
}



/* Given all these tools, we can define additional classes for geometries
 * that are useful in graphics.  For example, we can represent a sphere
 * using a Point3 for the position of the center point of the sphere and
 * a float for the sphere's radius.
 */
class Sphere {
    Point3 position;
    float radius;
};
```

## Q1: Eulerian Integration

In computer graphics and animation, there are many forms of integration that
are used. For simple physics models like we have in Car Soccer, Eulerian
Integration is good enough. Eulerian Integration uses velocity and position
information from the current frame, and the elapsed time to produce a position
for the next frame. Write pseudocode for determining the position of the sphere in the
next frame:

*Hint: think back to the motion equations from introductory physics. Or, look
around in the assignment handout.*

```cpp
Vector3 velocity = Vector3(1.0, 1.0, 1.0);
float dt = 20; // milliseconds

Sphere s = Sphere {
    position: Point3(0.0, 0.0, 0.0),
    radius: 5.0,
};

s.position = operator+(s.position, operator*(velocity,t/1000));
//That's wrong if the velocity is per ms.
```



## Q2: Sphere Intersection

In this assignment, you will need to test intersections between spheres and
other objects. Using the information contained within each sphere class,
write pseudocode to determine whether or not two spheres are intersecting
(which you can use for car/ball intersections):

```cpp
bool sphereIntersection(Sphere s1, Sphere s2) {
    /* --- Fill in your sphere intersection code here --- */
    Vector3 toSphere1 = abs(operator-(s1.position, s2.position));
    //abs is the absolute value of a vector components
    float distance = toSphere1.Length();
    return distance < (s1.radius + s2.radius);
}
```

To check that your intersections work, try working through the math by hand for the
following two cases.  You can write out the math on a scrap piece of paper.   You do
not need to include that detail in this worksheet.  But, do change the lines below where
it says "Fill in expected output" to indicate whether True or False would be returned:

```cpp
Sphere s1 = Sphere {
    position: Point3(0.0, 1.0, 0.0),
    radius: 1.0,
};

Sphere s2 = Sphere {
    position: Point3(3.0, 0.0, 0.0),
    radius: 1.0,
};

Sphere s3 = Sphere {
    position: Point3(1.0, 1.0, 0.0),
    radius: 2.0,
};

print(sphereIntersection(s1, s2));
/* --- Fill in expected output (True or False) --- */
false

print(sphereIntersection(s1, s3));
/* --- Fill in expected output (True or False) --- */
true
```