Jay Balar / Mbed 2 deprecated 4180_Project_3

Dependencies:   4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed

Committer:
jstephens78
Date:
Tue Nov 29 22:50:04 2022 +0000
Revision:
9:4e6fae5f9b23
Child:
12:5d913b57da7c
Add initial airhockey test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jstephens78 9:4e6fae5f9b23 1 #include "mbed.h"
jstephens78 9:4e6fae5f9b23 2 #include "rtos.h"
jstephens78 9:4e6fae5f9b23 3 #include "hockey.h"
jstephens78 9:4e6fae5f9b23 4 #include "uLCD_4DGL.h"
jstephens78 9:4e6fae5f9b23 5
jstephens78 9:4e6fae5f9b23 6 #define PHYSAC_NO_THREADS
jstephens78 9:4e6fae5f9b23 7 #define PHYSAC_STANDALONE
jstephens78 9:4e6fae5f9b23 8 #define PHYSAC_IMPLEMENTATION
jstephens78 9:4e6fae5f9b23 9 #define _STDBOOL_H
jstephens78 9:4e6fae5f9b23 10 #include "physac.h"
jstephens78 9:4e6fae5f9b23 11
jstephens78 9:4e6fae5f9b23 12 uLCD_4DGL uLCD(p9,p10,p30);
jstephens78 9:4e6fae5f9b23 13 Serial pc(USBTX, USBRX);
jstephens78 9:4e6fae5f9b23 14
jstephens78 9:4e6fae5f9b23 15 void thread2(void)
jstephens78 9:4e6fae5f9b23 16 {
jstephens78 9:4e6fae5f9b23 17 uLCD.baudrate(BAUD_3000000);
jstephens78 9:4e6fae5f9b23 18 wait(.05);
jstephens78 9:4e6fae5f9b23 19
jstephens78 9:4e6fae5f9b23 20 //--------------------------------------------------------------------------------------
jstephens78 9:4e6fae5f9b23 21 int screenWidth = 128;
jstephens78 9:4e6fae5f9b23 22 int screenHeight = 128;
jstephens78 9:4e6fae5f9b23 23
jstephens78 9:4e6fae5f9b23 24 PhysicsBody puck = CreatePhysicsBodyCircle((Vector2) {
jstephens78 9:4e6fae5f9b23 25 screenWidth/2, screenHeight/2
jstephens78 9:4e6fae5f9b23 26 }, 4, 10);
jstephens78 9:4e6fae5f9b23 27 puck->enabled = true;
jstephens78 9:4e6fae5f9b23 28 puck->useGravity = false;
jstephens78 9:4e6fae5f9b23 29 puck->restitution = 1.0;
jstephens78 9:4e6fae5f9b23 30 puck->velocity = (Vector2) {
jstephens78 9:4e6fae5f9b23 31 5, 0
jstephens78 9:4e6fae5f9b23 32 };
jstephens78 9:4e6fae5f9b23 33
jstephens78 9:4e6fae5f9b23 34
jstephens78 9:4e6fae5f9b23 35
jstephens78 9:4e6fae5f9b23 36 // Create paddle_a rectangle physics body
jstephens78 9:4e6fae5f9b23 37 PhysicsBody paddle_a = CreatePhysicsBodyRectangle((Vector2) {
jstephens78 9:4e6fae5f9b23 38 32, 64
jstephens78 9:4e6fae5f9b23 39 }, 8, 24, 10);
jstephens78 9:4e6fae5f9b23 40 paddle_a->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
jstephens78 9:4e6fae5f9b23 41 paddle_a->useGravity = false;
jstephens78 9:4e6fae5f9b23 42 paddle_a->restitution = 1.0;
jstephens78 9:4e6fae5f9b23 43 SetPhysicsBodyRotation(paddle_a, 3.14159 / 6);
jstephens78 9:4e6fae5f9b23 44
jstephens78 9:4e6fae5f9b23 45 PhysicsBody paddle_b = CreatePhysicsBodyRectangle((Vector2) {
jstephens78 9:4e6fae5f9b23 46 96, 64
jstephens78 9:4e6fae5f9b23 47 }, 8, 24, 10);
jstephens78 9:4e6fae5f9b23 48 paddle_b->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
jstephens78 9:4e6fae5f9b23 49 paddle_b->useGravity = false;
jstephens78 9:4e6fae5f9b23 50 paddle_b->restitution = 1.0;
jstephens78 9:4e6fae5f9b23 51 SetPhysicsBodyRotation(paddle_b, 3.14159 / 6);
jstephens78 9:4e6fae5f9b23 52
jstephens78 9:4e6fae5f9b23 53
jstephens78 9:4e6fae5f9b23 54 // Simulation Loop
jstephens78 9:4e6fae5f9b23 55 Timer timer;
jstephens78 9:4e6fae5f9b23 56 timer.start();
jstephens78 9:4e6fae5f9b23 57
jstephens78 9:4e6fae5f9b23 58 deltaTime = 1.66;
jstephens78 9:4e6fae5f9b23 59
jstephens78 9:4e6fae5f9b23 60 while (true) {
jstephens78 9:4e6fae5f9b23 61 float dt = timer.read() * 1000;
jstephens78 9:4e6fae5f9b23 62 timer.reset();
jstephens78 9:4e6fae5f9b23 63
jstephens78 9:4e6fae5f9b23 64
jstephens78 9:4e6fae5f9b23 65 accumulator += dt;
jstephens78 9:4e6fae5f9b23 66 if (accumulator >= deltaTime) {
jstephens78 9:4e6fae5f9b23 67 PhysicsStep();
jstephens78 9:4e6fae5f9b23 68 accumulator -= deltaTime;
jstephens78 9:4e6fae5f9b23 69 }
jstephens78 9:4e6fae5f9b23 70
jstephens78 9:4e6fae5f9b23 71 if (puck->position.y < 6 || puck->position.y > 122) {
jstephens78 9:4e6fae5f9b23 72 puck->velocity.y *= -1;
jstephens78 9:4e6fae5f9b23 73 } else if ((puck->position.y < 42 || puck->position.y > 86) &&
jstephens78 9:4e6fae5f9b23 74 (puck->position.x < 6 || puck->position.x > 122)) {
jstephens78 9:4e6fae5f9b23 75 puck->velocity.x *= -1;
jstephens78 9:4e6fae5f9b23 76 }
jstephens78 9:4e6fae5f9b23 77
jstephens78 9:4e6fae5f9b23 78 float phys_time = timer.read() * 1000;
jstephens78 9:4e6fae5f9b23 79
jstephens78 9:4e6fae5f9b23 80
jstephens78 9:4e6fae5f9b23 81 // Draw created physics bodies
jstephens78 9:4e6fae5f9b23 82 int a_count = GetPhysicsShapeVerticesCount(paddle_a->id);
jstephens78 9:4e6fae5f9b23 83 for (int j = 0; j < a_count; j++) {
jstephens78 9:4e6fae5f9b23 84 Vector2 vertexA = GetPhysicsShapeVertex(paddle_a, j);
jstephens78 9:4e6fae5f9b23 85 int jj = (((j + 1) < a_count) ? (j + 1) : 0);
jstephens78 9:4e6fae5f9b23 86 Vector2 vertexB = GetPhysicsShapeVertex(paddle_a, jj);
jstephens78 9:4e6fae5f9b23 87 uLCD.line(vertexA.x, vertexA.y, vertexB.x, vertexB.y, BLUE);
jstephens78 9:4e6fae5f9b23 88 }
jstephens78 9:4e6fae5f9b23 89 int b_count = GetPhysicsShapeVerticesCount(paddle_b->id);
jstephens78 9:4e6fae5f9b23 90 for (int j = 0; j < b_count; j++) {
jstephens78 9:4e6fae5f9b23 91 Vector2 vertexA = GetPhysicsShapeVertex(paddle_b, j);
jstephens78 9:4e6fae5f9b23 92 int jj = (((j + 1) < b_count) ? (j + 1) : 0);
jstephens78 9:4e6fae5f9b23 93 Vector2 vertexB = GetPhysicsShapeVertex(paddle_b, jj);
jstephens78 9:4e6fae5f9b23 94 uLCD.line(vertexA.x, vertexA.y, vertexB.x, vertexB.y, RED);
jstephens78 9:4e6fae5f9b23 95 }
jstephens78 9:4e6fae5f9b23 96
jstephens78 9:4e6fae5f9b23 97 uLCD.filled_circle(puck->position.x, puck->position.y, 4, GREEN);
jstephens78 9:4e6fae5f9b23 98
jstephens78 9:4e6fae5f9b23 99 /*int bodiesCount = GetPhysicsBodiesCount();
jstephens78 9:4e6fae5f9b23 100 for (int i = 0; i < bodiesCount; i++) {
jstephens78 9:4e6fae5f9b23 101 PhysicsBody body = GetPhysicsBody(i);
jstephens78 9:4e6fae5f9b23 102
jstephens78 9:4e6fae5f9b23 103 if (body != NULL) {
jstephens78 9:4e6fae5f9b23 104 int vertexCount = GetPhysicsShapeVerticesCount(i);
jstephens78 9:4e6fae5f9b23 105 for (int j = 0; j < vertexCount; j++) {
jstephens78 9:4e6fae5f9b23 106 // Get physics bodies shape vertices to draw lines
jstephens78 9:4e6fae5f9b23 107 // Note: GetPhysicsShapeVertex() already calculates rotation transformations
jstephens78 9:4e6fae5f9b23 108 Vector2 vertexA = GetPhysicsShapeVertex(body, j);
jstephens78 9:4e6fae5f9b23 109
jstephens78 9:4e6fae5f9b23 110 int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
jstephens78 9:4e6fae5f9b23 111 Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
jstephens78 9:4e6fae5f9b23 112
jstephens78 9:4e6fae5f9b23 113 uLCD.line(vertexA.x, vertexA.y, vertexB.x, vertexB.y, GREEN); // Draw a line between two vertex positions
jstephens78 9:4e6fae5f9b23 114 }
jstephens78 9:4e6fae5f9b23 115 }
jstephens78 9:4e6fae5f9b23 116 }*/
jstephens78 9:4e6fae5f9b23 117 float render_time = timer.read()*1000 - phys_time;
jstephens78 9:4e6fae5f9b23 118
jstephens78 9:4e6fae5f9b23 119
jstephens78 9:4e6fae5f9b23 120 pc.printf("[%2.2f] Phys: %4.4f/%4.4f Render: %4.4f | %4.2f %4.2f\r\n",
jstephens78 9:4e6fae5f9b23 121 dt,
jstephens78 9:4e6fae5f9b23 122 phys_time,
jstephens78 9:4e6fae5f9b23 123 accumulator,
jstephens78 9:4e6fae5f9b23 124 render_time,
jstephens78 9:4e6fae5f9b23 125 puck->position.x,
jstephens78 9:4e6fae5f9b23 126 puck->position.y);
jstephens78 9:4e6fae5f9b23 127 }
jstephens78 9:4e6fae5f9b23 128
jstephens78 9:4e6fae5f9b23 129 ClosePhysics(); // Unitialize physics
jstephens78 9:4e6fae5f9b23 130
jstephens78 9:4e6fae5f9b23 131 }