non mi ricordo nemmeno io che cosa ho aggiunto
This commit is contained in:
18
main.c
18
main.c
@@ -2,6 +2,7 @@
|
|||||||
#include <raylib.h>
|
#include <raylib.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
|
|
||||||
struct global {
|
struct global {
|
||||||
@@ -10,9 +11,10 @@ struct global {
|
|||||||
object *objs[128];
|
object *objs[128];
|
||||||
size_t obj_count;
|
size_t obj_count;
|
||||||
double terminal_v;
|
double terminal_v;
|
||||||
|
double friction;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct global gl = {1600, 900, 144, {}, 0, 15};
|
struct global gl = {1600, 900, 144, {}, 0, 15, 0.001};
|
||||||
|
|
||||||
void addobj(object *obj)
|
void addobj(object *obj)
|
||||||
{
|
{
|
||||||
@@ -25,6 +27,8 @@ void drawobjs()
|
|||||||
{
|
{
|
||||||
object *obj = gl.objs[i];
|
object *obj = gl.objs[i];
|
||||||
DrawSphere(obj->pos, obj->r, obj->color);
|
DrawSphere(obj->pos, obj->r, obj->color);
|
||||||
|
DrawSphereWires(obj->pos, obj->r, 16, 16, ColorBrightness(obj->color, -0.5f));
|
||||||
|
DrawLine3D(obj->pos, Vector3Add(obj->pos, obj->vel), PURPLE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +63,7 @@ void updateobjs()
|
|||||||
{
|
{
|
||||||
obj->vel = Vector3Scale(obj->vel, 1.0f/ratio_speed_to_terminal);
|
obj->vel = Vector3Scale(obj->vel, 1.0f/ratio_speed_to_terminal);
|
||||||
}
|
}
|
||||||
DrawLine3D(obj->pos, Vector3Add(obj->pos, obj->vel), GREEN);
|
obj->vel = Vector3Scale(obj->vel, (1.0f-gl.friction));
|
||||||
obj->pos = Vector3Add(obj->pos, Vector3Scale(obj->vel, GetFrameTime()));
|
obj->pos = Vector3Add(obj->pos, Vector3Scale(obj->vel, GetFrameTime()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,18 +82,18 @@ int main(void) {
|
|||||||
InitWindow(gl.scwidth, gl.scheight, "raylib engine");
|
InitWindow(gl.scwidth, gl.scheight, "raylib engine");
|
||||||
|
|
||||||
SetTargetFPS(gl.fps);
|
SetTargetFPS(gl.fps);
|
||||||
addobj(newobj((Vector3){0, 20, 0}, 1, RED, 1, 1000));
|
addobj(newobj((Vector3){10, 40, 0}, 2, BLUE, 1, 200000));
|
||||||
addobj(newobj((Vector3){8, 20, 0}, 2, BLUE, 1, 2000));
|
addobj(newobj((Vector3){50, 20, 0}, 5, RAYWHITE, 0., 5000000));
|
||||||
gl.objs[1]->vel = (Vector3){0, 0, 0};
|
Camera3D camera = {{0, 20, 10}, {0, 20, 0}, {0, 1, 0}, 50, CAMERA_PERSPECTIVE};
|
||||||
Camera3D camera = {{0, 20, -30}, {0, 0, 0}, {0, 1, 0}, 45, CAMERA_PERSPECTIVE};
|
|
||||||
DisableCursor();
|
DisableCursor();
|
||||||
|
SetWindowMonitor(0);
|
||||||
|
|
||||||
while(!WindowShouldClose())
|
while(!WindowShouldClose())
|
||||||
{
|
{
|
||||||
|
|
||||||
UpdateCamera(&camera, CAMERA_FREE);
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
{
|
{
|
||||||
|
UpdateCamera(&camera, CAMERA_FREE);
|
||||||
ClearBackground(BLACK);
|
ClearBackground(BLACK);
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
DrawPlane((Vector3){0, 0, 0}, (Vector2){500, 500}, RAYWHITE);
|
DrawPlane((Vector3){0, 0, 0}, (Vector2){500, 500}, RAYWHITE);
|
||||||
|
|||||||
12
objects.c
12
objects.c
@@ -1,6 +1,7 @@
|
|||||||
#include "objects.h"
|
#include "objects.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <raymath.h>
|
#include <raymath.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define min(a, b) (a>b?b:a);
|
#define min(a, b) (a>b?b:a);
|
||||||
#define max(a, b) (a>b?a:b);
|
#define max(a, b) (a>b?a:b);
|
||||||
@@ -61,7 +62,7 @@ void resolve_collision(object *obj1, object *obj2)
|
|||||||
float sumrad = obj1->r+obj2->r;
|
float sumrad = obj1->r+obj2->r;
|
||||||
float mindistsq = sumrad*sumrad;
|
float mindistsq = sumrad*sumrad;
|
||||||
|
|
||||||
if (distsq < mindistsq) {
|
if (distsq <= mindistsq) {
|
||||||
float dist = sqrtf(distsq);
|
float dist = sqrtf(distsq);
|
||||||
|
|
||||||
if (dist == 0) {
|
if (dist == 0) {
|
||||||
@@ -73,9 +74,8 @@ void resolve_collision(object *obj1, object *obj2)
|
|||||||
|
|
||||||
float overlap = sumrad - dist;
|
float overlap = sumrad - dist;
|
||||||
Vector3 separation = Vector3Scale(normal, overlap / 2.0f);
|
Vector3 separation = Vector3Scale(normal, overlap / 2.0f);
|
||||||
|
obj1->pos = Vector3Add(obj1->pos, separation);
|
||||||
obj1->pos = Vector3Subtract(obj1->pos, separation);
|
obj2->pos = Vector3Subtract(obj2->pos, separation);
|
||||||
obj2->pos = Vector3Add(obj2->pos, separation);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
normal = Vector3Normalize(normal);
|
normal = Vector3Normalize(normal);
|
||||||
@@ -87,8 +87,8 @@ void resolve_collision(object *obj1, object *obj2)
|
|||||||
if (vel1_normal - vel2_normal > 0) return;
|
if (vel1_normal - vel2_normal > 0) return;
|
||||||
|
|
||||||
float totalmass = obj1->mass + obj2->mass;
|
float totalmass = obj1->mass + obj2->mass;
|
||||||
float vel1_normal_final = (obj1->mass * vel1_normal + obj2->mass * vel2_normal - obj2->mass * obj2->restitution * (vel1_normal - vel2_normal)) / totalmass;
|
float vel1_normal_final = (obj1->mass * vel1_normal + obj2->mass * vel2_normal - obj2->mass * (vel1_normal - vel2_normal)) * obj1->restitution / totalmass;
|
||||||
float vel2_normal_final = (obj1->mass * vel1_normal + obj2->mass * vel2_normal - obj2->mass * obj1->restitution * (vel2_normal - vel1_normal)) / totalmass;
|
float vel2_normal_final = (obj1->mass * vel1_normal + obj2->mass * vel2_normal - obj2->mass * (vel2_normal - vel1_normal)) * obj2->restitution / totalmass;
|
||||||
|
|
||||||
Vector3 vel1_tang = Vector3Subtract(obj1->vel, Vector3Scale(normal, vel1_normal));
|
Vector3 vel1_tang = Vector3Subtract(obj1->vel, Vector3Scale(normal, vel1_normal));
|
||||||
Vector3 vel2_tang = Vector3Subtract(obj2->vel, Vector3Scale(normal, vel2_normal));
|
Vector3 vel2_tang = Vector3Subtract(obj2->vel, Vector3Scale(normal, vel2_normal));
|
||||||
|
|||||||
Reference in New Issue
Block a user