Skip to content

Commit

Permalink
Vector2LengthSqr instead of Vector2Length
Browse files Browse the repository at this point in the history
  • Loading branch information
thegtproject committed Feb 14, 2022
1 parent be82165 commit 43832a1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/body.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ void UpdateBodies(BodyVector* vec)

Vector2 Attract(Body* b1, Body* b2)
{
// Vector2 force = Vector2Subtract(b2->position, b1->position);
// float distance = Vector2Length(force);
// distance = Clamp(distance, 1, 25);
// force = Vector2Normalize(force);
// float strength = (GravityConstant * b2->mass * b1->mass) / (distance * distance);

// a bit faster... id like to measure these two approaches more officially
Vector2 force = Vector2Subtract(b2->position, b1->position);
float distance = Vector2Length(force);
distance = Clamp(distance, 1, 25);
float distance = Vector2LengthSqr(force);
distance = Clamp(distance, 1, 25*25);
force = Vector2Normalize(force);
float strength = (GravityConstant * b2->mass * b1->mass) / (distance * distance);
float strength = (GravityConstant * b2->mass * b1->mass) / distance;

force.x = force.x * strength;
force.y = force.y * strength;

Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <stdio.h>

static const int initialBodyCount = 128;
static const int initialBodyCount = 512;

static const int screenWidth = 1200;
static const int screenHeight = 650;
Expand Down
1 change: 0 additions & 1 deletion src/nbody.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ extern void add_random_body(void);
extern void add_random_body_fn(Vector2(*positionFunc)(void));
extern void add_random_body_at_mouse(void);
extern void remove_last_body(void);

extern Vector2 get_random_circlepos(void);
extern Vector2 get_random_worldpos(void);

0 comments on commit 43832a1

Please sign in to comment.