Sound update
Dependencies: 4DGL-uLCD-SE Physac-MBED PinDetect SDFileSystem mbed-rtos mbed
Revision 9:4e6fae5f9b23, committed 20 months ago
- Comitter:
- jstephens78
- Date:
- Tue Nov 29 22:50:04 2022 +0000
- Parent:
- 6:fc95af217f01
- Child:
- 10:f5a84133bd65
- Commit message:
- Add initial airhockey test
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Physac-MBED.lib Tue Nov 29 22:50:04 2022 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/jstephens78/code/Physac-MBED/#e39efa4f4f58
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/hockey/hockey.cpp Tue Nov 29 22:50:04 2022 +0000
@@ -0,0 +1,131 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "hockey.h"
+#include "uLCD_4DGL.h"
+
+#define PHYSAC_NO_THREADS
+#define PHYSAC_STANDALONE
+#define PHYSAC_IMPLEMENTATION
+#define _STDBOOL_H
+#include "physac.h"
+
+uLCD_4DGL uLCD(p9,p10,p30);
+Serial pc(USBTX, USBRX);
+
+void thread2(void)
+{
+ uLCD.baudrate(BAUD_3000000);
+ wait(.05);
+
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 128;
+ int screenHeight = 128;
+
+ PhysicsBody puck = CreatePhysicsBodyCircle((Vector2) {
+ screenWidth/2, screenHeight/2
+ }, 4, 10);
+ puck->enabled = true;
+ puck->useGravity = false;
+ puck->restitution = 1.0;
+ puck->velocity = (Vector2) {
+ 5, 0
+ };
+
+
+
+ // Create paddle_a rectangle physics body
+ PhysicsBody paddle_a = CreatePhysicsBodyRectangle((Vector2) {
+ 32, 64
+ }, 8, 24, 10);
+ paddle_a->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
+ paddle_a->useGravity = false;
+ paddle_a->restitution = 1.0;
+ SetPhysicsBodyRotation(paddle_a, 3.14159 / 6);
+
+ PhysicsBody paddle_b = CreatePhysicsBodyRectangle((Vector2) {
+ 96, 64
+ }, 8, 24, 10);
+ paddle_b->enabled = false; // Disable body state to convert it to static (no dynamics, but collisions)
+ paddle_b->useGravity = false;
+ paddle_b->restitution = 1.0;
+ SetPhysicsBodyRotation(paddle_b, 3.14159 / 6);
+
+
+ // Simulation Loop
+ Timer timer;
+ timer.start();
+
+ deltaTime = 1.66;
+
+ while (true) {
+ float dt = timer.read() * 1000;
+ timer.reset();
+
+
+ accumulator += dt;
+ if (accumulator >= deltaTime) {
+ PhysicsStep();
+ accumulator -= deltaTime;
+ }
+
+ if (puck->position.y < 6 || puck->position.y > 122) {
+ puck->velocity.y *= -1;
+ } else if ((puck->position.y < 42 || puck->position.y > 86) &&
+ (puck->position.x < 6 || puck->position.x > 122)) {
+ puck->velocity.x *= -1;
+ }
+
+ float phys_time = timer.read() * 1000;
+
+
+ // Draw created physics bodies
+ int a_count = GetPhysicsShapeVerticesCount(paddle_a->id);
+ for (int j = 0; j < a_count; j++) {
+ Vector2 vertexA = GetPhysicsShapeVertex(paddle_a, j);
+ int jj = (((j + 1) < a_count) ? (j + 1) : 0);
+ Vector2 vertexB = GetPhysicsShapeVertex(paddle_a, jj);
+ uLCD.line(vertexA.x, vertexA.y, vertexB.x, vertexB.y, BLUE);
+ }
+ int b_count = GetPhysicsShapeVerticesCount(paddle_b->id);
+ for (int j = 0; j < b_count; j++) {
+ Vector2 vertexA = GetPhysicsShapeVertex(paddle_b, j);
+ int jj = (((j + 1) < b_count) ? (j + 1) : 0);
+ Vector2 vertexB = GetPhysicsShapeVertex(paddle_b, jj);
+ uLCD.line(vertexA.x, vertexA.y, vertexB.x, vertexB.y, RED);
+ }
+
+ uLCD.filled_circle(puck->position.x, puck->position.y, 4, GREEN);
+
+ /*int bodiesCount = GetPhysicsBodiesCount();
+ for (int i = 0; i < bodiesCount; i++) {
+ PhysicsBody body = GetPhysicsBody(i);
+
+ if (body != NULL) {
+ int vertexCount = GetPhysicsShapeVerticesCount(i);
+ for (int j = 0; j < vertexCount; j++) {
+ // Get physics bodies shape vertices to draw lines
+ // Note: GetPhysicsShapeVertex() already calculates rotation transformations
+ Vector2 vertexA = GetPhysicsShapeVertex(body, j);
+
+ int jj = (((j + 1) < vertexCount) ? (j + 1) : 0); // Get next vertex or first to close the shape
+ Vector2 vertexB = GetPhysicsShapeVertex(body, jj);
+
+ uLCD.line(vertexA.x, vertexA.y, vertexB.x, vertexB.y, GREEN); // Draw a line between two vertex positions
+ }
+ }
+ }*/
+ float render_time = timer.read()*1000 - phys_time;
+
+
+ pc.printf("[%2.2f] Phys: %4.4f/%4.4f Render: %4.4f | %4.2f %4.2f\r\n",
+ dt,
+ phys_time,
+ accumulator,
+ render_time,
+ puck->position.x,
+ puck->position.y);
+ }
+
+ ClosePhysics(); // Unitialize physics
+
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/hockey/hockey.h Tue Nov 29 22:50:04 2022 +0000 @@ -0,0 +1,3 @@ + + +void thread2(void); \ No newline at end of file