first commit
This commit is contained in:
104
main.c
Normal file
104
main.c
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
#include <stddef.h>
|
||||||
|
#include <raylib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "objects.h"
|
||||||
|
|
||||||
|
struct global {
|
||||||
|
const int scwidth, scheight;
|
||||||
|
int fps;
|
||||||
|
object *objs[128];
|
||||||
|
size_t obj_count;
|
||||||
|
double terminal_yv;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct global gl = {1600, 900, 60, {}, 0, -20};
|
||||||
|
|
||||||
|
void addobj(object *obj)
|
||||||
|
{
|
||||||
|
gl.objs[gl.obj_count++] = obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
void drawobjs()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < gl.obj_count; i++)
|
||||||
|
{
|
||||||
|
object *obj = gl.objs[i];
|
||||||
|
DrawSphere(obj->pos, obj->r, obj->color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateobjs()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < gl.obj_count; i++)
|
||||||
|
{
|
||||||
|
object *obj = gl.objs[i];
|
||||||
|
obj->vel.y -= 9.81 * GetFrameTime();
|
||||||
|
if (obj->vel.y < gl.terminal_yv)
|
||||||
|
{
|
||||||
|
obj->vel.y = gl.terminal_yv;
|
||||||
|
}
|
||||||
|
if (obj->pos.y < obj->r) {
|
||||||
|
obj->pos.y = obj->r;
|
||||||
|
obj->vel.y *= -obj->restitution;
|
||||||
|
}
|
||||||
|
obj->pos.x += obj->vel.x * GetFrameTime();
|
||||||
|
obj->pos.y += obj->vel.y * GetFrameTime();
|
||||||
|
obj->pos.z += obj->vel.z * GetFrameTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void freeobjs()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < gl.obj_count; i++)
|
||||||
|
{
|
||||||
|
object *obj = gl.objs[i];
|
||||||
|
free(obj);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void applyforce()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < gl.obj_count; i++)
|
||||||
|
{
|
||||||
|
object *obj = gl.objs[i];
|
||||||
|
obj->vel.x += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
|
||||||
|
InitWindow(gl.scwidth, gl.scheight, "raylib engine");
|
||||||
|
|
||||||
|
SetTargetFPS(gl.fps);
|
||||||
|
addobj(newobj((Vector3){0, 30, 0}, 2, RED, 0.5));
|
||||||
|
addobj(newobj((Vector3){5, 2, 0}, 1, BLUE, 1));
|
||||||
|
Camera3D camera = {{0, 20, -30}, {0, 0, 0}, {0, 1, 0}, 45, CAMERA_PERSPECTIVE};
|
||||||
|
|
||||||
|
while(!WindowShouldClose())
|
||||||
|
{
|
||||||
|
|
||||||
|
UpdateCamera(&camera, CAMERA_FREE);
|
||||||
|
BeginDrawing();
|
||||||
|
{
|
||||||
|
ClearBackground(BLACK);
|
||||||
|
BeginMode3D(camera);
|
||||||
|
DrawPlane((Vector3){0, 0, 0}, (Vector2){100, 100}, GRAY);
|
||||||
|
updateobjs();
|
||||||
|
drawobjs();
|
||||||
|
|
||||||
|
if (IsKeyPressed(KEY_L)) {
|
||||||
|
applyforce();
|
||||||
|
}
|
||||||
|
|
||||||
|
EndMode3D();
|
||||||
|
}
|
||||||
|
EndDrawing();
|
||||||
|
}
|
||||||
|
|
||||||
|
freeobjs();
|
||||||
|
|
||||||
|
CloseWindow();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
22
objects.h
Normal file
22
objects.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <raylib.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
Vector3 pos;
|
||||||
|
Vector3 vel;
|
||||||
|
float r;
|
||||||
|
Color color;
|
||||||
|
float restitution;
|
||||||
|
} object;
|
||||||
|
|
||||||
|
object *newobj(Vector3 pos, float r, Color c, float rest)
|
||||||
|
{
|
||||||
|
object *obj = (object*)malloc(sizeof(object));
|
||||||
|
obj->r = r;
|
||||||
|
obj->pos = pos;
|
||||||
|
obj->color = c;
|
||||||
|
obj->vel = (Vector3){0, 0, 0};
|
||||||
|
obj->restitution = rest;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user