You are browsing as a guest. Sign up (or log in) to start making projects!

Physics Simulator

  • 7 Devlogs
  • 47 Total hours

Most projects i've built have been (i'd like to think so, at least) practical. This one is most likely not. Here's my attempt at a physics simulator.

Ship #1

I made a physics simulator; it was challenging to make because a lot of the concepts necessary were new to me, especially implementing the math. But I learned a lot from building it, and I hope it’s enjoyed or useful!

  • 7 devlogs
  • 47h
Try project → See source code →
Open comments for this post

5h 21m 14s logged

OKAY – LAST DEVLOG…

THIS ONE WAS A TRICKY ONE AND I’M excited to share what i’ve done!

What I did was add a restitution index to the balls so they bounce more, and so you can compare weight of the balls to the bounciness of the balls combined with the gravity of different planets. Fun, right?

What I also added was a little bit of an air resistance simulator which you can change for the whole environment (non-exclusive, the change above is exclusive)

Some issues I encountered while building was that I accidentally broke the gravity while trying to do the restitution index and so I had to do some major debugging in that department, and I also messed up my dropdowns, which was an easier fix.

I’m excited to ship this project!

i hope you’ve enjoyed following along, and next project will be made soon!

0
0
15
Open comments for this post

7h 56m 34s logged

Okay – Back from vacation… put a pretty hefty chunk of time in today! Forgot to do a halfway devlog again today, but I’ll make up for it later or tomorrow!

I added a live physics readout
And added the labels while making the gravity exclusive (like I said I would :D)

   ctx.font = "11px sans-serif";
ctx.fillStyle = "#ffffff";
ctx.textAlign = "center";
const label = `${ball.speed.toFixed(1)} m/s | ${ball.kinetic_energy.toFixed(1)} J`;
ctx.fillText(label, ball.x, ball.y - ball.radius - 6);
const line1 = `${ball.mass.toFixed(1)}kg @ ${ball.gravity.toFixed(1)}m/s²`;
const line2 = `${ball.speed.toFixed(1)} m/s | ${ball.kinetic_energy.toFixed(1)} J`;
ctx.fillText(line1, ball.x, ball.y - ball.radius - 18);
ctx.fillText(line2, ball.x, ball.y - ball.radius - 6);

}

if (dragStart && dragCurrent) {

some sample code for your liking..

no one commented on my last devlog, so if you have any ideas comment on this one.

I have a couple QoL upgrades i’m going to make but then ship.

Often feedback on my projects says that it doesn’t take the reviewer as long as it took me to code, and I completely understand that take, but personally, I am pretty new at coding, so I do take a lot of time debugging and understanding what I am coding, so just a little behind-the-scenes!

Oh yeah, also a sweet little speed indicator!

0
0
15
Open comments for this post

6h 3m 46s logged

Okay – I added a little fun feature where you can build walls and have the balls bounce off of it!

It’s late, so expect a more detailed devlog and more on-track stuff after I get back from vacation!

See ya!

0
0
7
Open comments for this post

8h 32m 1s logged

Okay – 8 and half hours of work done… (I forgot to post a devlog between when I stopped and restarted working, whoops)

I had fun with this one haha

What i added:

Slider to change the gravity (total, changes for all current objects)
Slider to change the ball weight/size/mass (local and exclusive)

Back on track.

This was fun because I’ve never done anything of the sort, so here’s a bit of what I wrote:

PX_PER_M = 100  # scale factor from real-world m/s^2 to our pixel-space gravityEARTH_GRAVITY = 9.8 * PX_PER_MPLANET_GRAVITY_MS2 = {    “mercury”: 3.7,    “venus”: 8.87,    “earth”: 9.8,    “moon”: 1.62,    “mars”: 3.71,    “jupiter”: 24.79,    “saturn”: 10.44,    “uranus”: 8.69,    “neptune”: 11.15,    “pluto”: 0.62,}RESTITUTION = 0.8  # energy kept on bounceMAX_BALLS = 8BASE_RADIUS = 15RADIUS_PER_MASS = 5

(i’ll add screenshots so the text is clearer)

class Ball:
def init(self, x, y, vx=0.0, vy=0.0, mass=1.0):
self.x = x
self.y = y
self.vx = vx
self.vy = vy
self.mass = mass
self.radius = BASE_RADIUS + RADIUS_PER_MASS * mass

def state(self):
    return {"x": self.x, "y": self.y, "radius": self.radius, "mass": self.mass}

class World:
def init(self, width, height, gravity=EARTH_GRAVITY):
self.width = width
self.height = height
self.gravity = gravity
self.balls = [Ball(x=width / 2, y=height / 4)]

def set_gravity(self, gravity):
    self.gravity = gravity

def launch(self, x, y, vx, vy, mass=1.0):
    if len(self.balls) >= MAX_BALLS:
        self.balls.pop(0)
    self.balls.append(Ball(x=x, y=y, vx=vx, vy=vy, mass=mass))

def step(self, dt):
    for ball in self.balls:
        ball.vy += self.gravity * dt
        ball.x += ball.vx * dt
        ball.y += ball.vy * dt

        floor = self.height - ball.radius
        ceiling = ball.radius
        if ball.y > floor:
            ball.y = floor
            ball.vy = -ball.vy * RESTITUTION
        elif ball.y < ceiling:
            ball.y = ceiling
            ball.vy = -ball.vy * RESTITUTION

        left = ball.radius
        right = self.width - ball.radius
        if ball.x < left:
            ball.x = left
            ball.vx = -ball.vx * RESTITUTION
        elif ball.x > right:
            ball.x = right
            ball.vx = -ball.vx * RESTITUTION

    self.resolve_collisions()

What I plan to do:

Add different shapes (not really important tho)
add backgrounds
add labels for the details of each ball whcih will bleed into making the gravitas exclusive as well.
Let me know if you have any ideas!

0
0
5
Open comments for this post

6h 10m 43s logged

Okay Devlog #3 i think…

Anyway, I’ve been on vacation (and still am) but started doign some work.

Here’s what i’ve done:

I’ve added a small little fun pool type feature where you can add different balls and have them bounce off of each other (a little off track, yes, but I found it fun to implement and use)!

Its great!

Short devlog because its late, but heres what I plan to implement:

Customizable gravity

Maybe different shapes and weights

Sliders

0
0
4
Open comments for this post

7h 52m 53s logged

Okay Devlog #2:

I’ve taken a little bit of a different fork, and I’ve implemented a feature where you can launch the ball around the screen so you can feel the physics for yourself.

This took quite a while to get right because it isn’t binary like the first version.

I added 360 deg collisions and a line drawing tool to adjust strength and direction, sort of like a pool game.

Going to work on customization in the future.

It’s late, so this isn’t as detailed as I’d like but screenshots are attached below.
Not sure if I can upload screen recordings, but I would for the next time.

0
0
45
Open comments for this post

4h 51m 58s logged

Uhh Devlog #1 FOR THIS NEW PROJECT1

Spent some good time on hit heres what I have so far.

Firstly, I got FAST API working. just ofr a little backend

I also got a simple bouncing ball working drawn using JavaScript.

Added SS below. Next I want to work on being able to change the gravity parameters of the ball, then adding different envs and items.

0
0
8

Delete project?

Are you sure you want to permanently delete this project? This action cannot be undone.

All devlogs, followers, and associated data will be removed.

Followers

Loading…