You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Showing how motion works using velocity, acceleration, and time.
#include "raylib.h"
#include "raymath.h"
int main(void)
{
const int screenWidth = 1024;
const int screenHeight = 1024;
// setup raylib
InitWindow(screenWidth, screenHeight, "thrust and friction");
SetTargetFPS(155);
Vector2 pos = { screenWidth * 0.5f, screenHeight * 0.5f }; // player position
Vector2 velocity = { 0,0 }; // player speed ( in x and y )
float friction = 200; // friction ( in pixels per second )
float maxSpeed = 400; // max speed (in pixels per second )
float thrust = 200; // thrust acceleration ( in pixels per second per second )
while (!WindowShouldClose())
{
Vector2 input = { 0 };
// see what directions we want to go
if (IsKeyDown(KEY_W))
input.y -= 1;
if (IsKeyDown(KEY_S))
input.y += 1;
if (IsKeyDown(KEY_A))
input.x -= 1;
if (IsKeyDown(KEY_D))
input.x += 1;
// add our input dir + thrust for this frame to our velocity
velocity = Vector2Add(velocity, Vector2Scale(input, thrust * GetFrameTime()));
// if we are not thrusting, we are applying friction
if (Vector2LengthSqr(input) == 0)
{
// see how fast we are going
float mag = Vector2Length(velocity);
if (mag > 0)
{
// if we are going over 0, then slow us down by the friction for this frame
mag -= friction * GetFrameTime();
if (mag < 0)
mag = 0; // we don't want to go backwards
// set our new velocity vector based on the slowed down value
velocity = Vector2Scale(Vector2Normalize(velocity), mag);
}
}
else if (Vector2LengthSqr(velocity) > maxSpeed * maxSpeed) // see if we are going too fast
{
// if we are, clamp the speed to our max
velocity = Vector2Scale(Vector2Normalize(velocity), maxSpeed);
}
// see see where we would go if we moved along the velocity for this frame
Vector2 newPos = Vector2Add(pos, Vector2Scale(velocity, GetFrameTime()));
// see if X is out of bounds
if (newPos.x < 0 || newPos.x > GetScreenWidth())
{
// if so, use the old position
newPos.x = pos.x;
// and bounce us backwards in this axis
velocity.x *= -1;
}
// see if Y is out of bounds
if (newPos.y < 0 || newPos.y > GetScreenHeight())
{
// if so, use the old position
newPos.y = pos.y;
// and bounce us backwards in this axis
velocity.y *= -1;
}
// update to our new position
pos = newPos;
// draw the scene
BeginDrawing();
ClearBackground(RAYWHITE);
// draw the rectangle
DrawRectangle(pos.x, pos.y, 25, 25, RED);
DrawFPS(0, 0);
DrawText(TextFormat("V x%f y%f", velocity.x, velocity.y), 0, 20, 20, BLACK);
EndDrawing();
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Showing how motion works using velocity, acceleration, and time.
Beta Was this translation helpful? Give feedback.
All reactions