Questions tagged [physics]

Physics is a science that studies the interactions of energy and matter. The physics tag should be used for questions about how physics can be simulated or implemented in programming.

Filter by
Sorted by
Tagged with
112
votes
4answers
21k views

Celebratory fireworks animation

To celebrate an important event, I hastily cobbled together an HTML canvas-based animation. A few concerns I have include: Performance: Does it run reasonably smoothly on most modern machines? How ...
31
votes
7answers
6k views

Car crash reaction time

In physics class we are doing an investigation thing. Some of the numbers might seem plucked out of thin air but they aren't. The maths is right(I think) I just want some feedback on the code. ...
26
votes
9answers
4k views

Simple n-body class in C++

As part of my training, I implemented a n-body class in C++ to simulate gravitational interaction of bodies and to get more familiar with features that C++ offers such as object oriented programming. ...
20
votes
2answers
5k views

Barnes-Hut N-body simulator

I've written an n-body simulator, implementing the Barnes-Hut algorithm. Please comment on anything you can see wrong with this. Wikipedia Barnes-Hut page This is a screen shot of the simulation 20 ...
20
votes
1answer
3k views

Asteroids game clone

I have recently been teaching a JavaScript module on a games programming undergraduate course and I made this game as a learning exercise and as an example for my students to study. I have made games ...
19
votes
4answers
420 views

Ising Model - Atom Vibration Simulation in Scala

I am an experienced Java developer (12+ years) and have recently switched to Scala and I love it. However I feel not comfy yet and I have a feeling that I might use to many paradigms from the good old ...
17
votes
6answers
6k views

Vector (physics) implementation

I recently started learning Java, and I decided to implement a basic vector system for another particle system I was building. ...
17
votes
2answers
2k views

Simulating a two-body collision problem to find digits of Pi

I came across a nice video on 3Blue1Brown's channel that highlights a very indirect way to find the digits in Pi. I'd suggest watching the whole video, but briefly: The setup is as above. A "small" ...
17
votes
3answers
243 views

“Do I understand the gravity of the situation?”

What is this? This is a two-dimensional physics simulator that models gravity and collisions between circular objects (though it doesn't model rotation). How do you use it? You can tweak the ...
17
votes
1answer
4k views

Bouncing stacking boxes

I'm not sure if it would be better to make the envelope into an extension of the Rectangle constructor. The physics seem a bit dodgy, it works well most of the time, but sometimes the boxes overlap ...
16
votes
1answer
857 views

Fluid Simulation with SDL

I have always wanted to write a fluid simulation, and with the help of a paper and some StackOverflow users I've got something that works. My goal is to have a program that someone can run right away ...
15
votes
3answers
1k views

Simulating gravitational attraction between bodies

I'm currently working on a simple simulator in Unity3D that involves planets orbiting each other. I wrote the following C# script to do that. It uses the classic gravitational formula, $$F=G\frac{...
15
votes
3answers
4k views

Orbital Trajectory simulator

I have written a simple program to do trajectory simulation in the Earth-Moon system, it still has a long way to go I am working on making it more class oriented and am looking into implementing a ...
13
votes
4answers
359 views

Lines, intersections and terrible unit tests

I needed Line and LineF for the next stage of a project I'm working on, so I developed them. I also needed to determine if two ...
13
votes
2answers
366 views

Complex scientific formula for nuclear magnetic resonance

I've been reading "clean code" tutorials, and I definitely see the value of using good names that "document themselves" to make intent clear. However, what can be done about complex formulas where ...
13
votes
1answer
1k views

Haskell Particle Simulation

I recently started learning Haskell and as my first project I decided to port a particle simulation I had written in C. The simulation is pretty simple. We have a cubic box with particles (spheres) ...
12
votes
2answers
1k views

Gravitational Brute Force N-body Algorithm

I've just started dabbling with code, and as a learning exercise I've written a simple algorithm for solving gravitational n-body problems numerically in JavaScript. I would be very grateful for ...
11
votes
1answer
292 views

Basketball Collisions with Box2d

I'm working on a new prototype that is a simple drag and shoot arcade basketball game. For this project I am working with LibGDX and the Box2d physics library. You can play the game here: Play ...
10
votes
5answers
2k views

Ising model simulation

I have written this code to simulate Ising Model at one particular temperature in presence of magnetic field to observe hysteresis effect using the metropolis algorithm. While the code runs and gave ...
10
votes
2answers
3k views

Solar System model

I have a model that works, as far as I know, but it's so messy! I am very new to Java, so I'd really appreciate some help tidying up. In particular, a lot of my constructors are empty which is ...
10
votes
1answer
489 views

Simulating a polygon

This code simulates a polygon (say a triangle) with a person at each vertex, every person is looking at the next adjacent vertex/person. They all start moving towards each other with a constant ...
10
votes
3answers
474 views

Feynmann lifeguard riddle graphical representation

Physicist Richard Feynman though of a very interesting riddle: Imagine you are a lifeguard at the side of the beach and there is a child drowning in the water. (You must cross both sand and water ...
10
votes
1answer
281 views

Processing.js particle system

After about half an hour of fooling around with Processing.js/JavaScript, I got this fairly decent particle system set up: ...
9
votes
2answers
4k views

Kinematic Equations

I'm learning the kinematic equations in Concepts of Engineering and so here are the equations I am learning: To complete the lesson, I was told to create a Python program that outputs the horizontal ...
9
votes
1answer
3k views

Particle class for physics simulation

This is a very lengthy class called Particle, it relies on two other headers, one of them is a simple struct which contains an x and y (...
8
votes
4answers
7k views

Brute Force N Body Implementation in C++

I have wrote the following code in C++ for the n-body problem. This code is sequential as later on I am planning to parallelize it using OpenMP. I want to know whether I have wrote the correct ...
8
votes
2answers
927 views

Golf game boilerplate

I wrote a program in pygame that basically acts as a physics engine for a ball. You can hit the ball around and your strokes are counted, as well as an extra stroke for going out of bounds. If I do ...
8
votes
3answers
956 views

1D shock tube problem written in Fortran

I have written a simple Euler solver for the 1D shock tube problem. Eventually, I plan to extend this code to solve the full 3D compressible Navier-Stokes equations. Therefore, I want to start with ...
8
votes
3answers
689 views

Python differential analysis of heat loss across a pipe

I made a differential equation solver to forecast heat loss through a pipe with heat loss coefficient contours. It does what I want it to do but I had to use global variables to change the outputs of ...
8
votes
2answers
801 views

Roll-a-ball controller

I've been toying around with Unity 5 lately, and in an effort to start making an actual game, I've built a ball controller similar to the one found in the Unity tutorial Roll-a-ball. In essence, the ...
8
votes
2answers
140 views

Particle swarm optimization - follow-up

This is a followup post to Particle Swarm Optimization. I wrote a script in Python for particle swarm optimization and I posted it here to get comments on the design. I was told that encapsulating ...
8
votes
1answer
1k views

2D colliding disks in JavaScript (ideal billiard balls)

I am implementing a simulation of colliding disks (ideal 2D billiard balls) in JavaScript. I follow an event-driven algorithm that avoids discretizing time; the algorithm goes as follows at each step:...
7
votes
4answers
2k views

Artillery game C++ practice project

I've completed a program as a learning experience to make sure I've got the concepts down. I found a practice project online and decided to try it out. The program compiles and runs exactly how it ...
7
votes
1answer
140 views

Visual solution of the Newtonial differential equation with Python

I wrote this Python code that plots the movement of an object under the effect of a given force function in 2D by solving the Newton's movement equation numerically. One can add other force functions, ...
7
votes
1answer
577 views

Metaballs and Gravity game;

For past two days I made my first OpenGL project, I know, It's fixed pipeline, but my focus was on gravity algorithm, so, tell what do you think, and how can I make it faster? main.cpp ...
7
votes
3answers
179 views

Solving for every variable in a number of physics formulas

I'm working on making an iOS app to solve physics problems and I'm starting by just having it solve basic kinematics problems with the following formulas (I've coded it in C instead of Swift to start ...
7
votes
1answer
255 views

Processing.js physics simulation

I've been fooling around in JavaScript again, and I've come up with this simple physics simulation. I think it has a pretty simple setup, here's "psuedocode" of sorts to explain it. Constructor ...
7
votes
1answer
811 views

Quad Tree and Collision Detection Implementation

I have an implementation of a quad tree and abstract collision detection classes. The code works but looks very ugly, but I don't know what I could change to make it better. The reason I used two ...
7
votes
1answer
113 views

Animating a 2D particle “web”

I have written a small class to generate and animate one of those trendy particle webs. I plan to use this as the main background of my personal website, so naturally I'm concerned about the ...
6
votes
4answers
3k views

Rocket controller for a Unity game

I'm currently learning C# so I can make real games with Unity. This is the first C# program I've built in Unity. Essentially what is does is control the thrust, and rotation of a ...
6
votes
3answers
1k views

Bouncing ball simulation

I was fooling around with some maths and realized that I could use a parabola to represent a bouncing ball. So, naturally, I decided to make it in code. ...
6
votes
2answers
820 views

Math, Geometry, & Physics Library

I am making a Python program that does math and physics calculations for you. This is my first programming project and is a work in progress. The SymPy library was used for some portion of it. ...
6
votes
2answers
319 views

Simulating a Solar System Using Command-Line Graphics

After a relative hiatus of around two years, I've decided to start delving back into the programming world; to start, I've appropriated one of my older projects, CLIGL, and have made a solar system ...
6
votes
1answer
1k views

Calculators for gas law equations

I've made a C++ program that calculates a missing variable in an equation for one of the five following laws of gas: Boyle's Law Charles' Law Gay-Lussac's Law Avogadro's Principle Ideal Gas Law The ...
6
votes
1answer
1k views

Kinematic Equations Calculator

The program allows user to enter values for 3 of the following initial velocity final velocity acceleration displacement time and to specify an unknown (one of the previously mentioned variables)....
6
votes
2answers
256 views

Physics particle collisions

I have a large file that I want to refactor. My idea is to make helper classes with helper methods so that I can modularize methods. Is that a good idea? For instance before refactoring: ...
6
votes
2answers
241 views

Simulating gravitational attraction between bodies - follow up

I've decided to revisit a chunk of code I wrote a while back, and see what I could do to make some changes. As a result, I know have slow, but working piece of code which is able to simulate ...
6
votes
3answers
178 views

Calculate next position and velocity after “bounce”

Intro and code I have the following function that calculates an object's "next" position and velocity (in 1 dimension) given a boundary [0, max]. ...
6
votes
3answers
535 views

Distance and force calculation for a molecular dynamics simulation

I'm writing an MD-like simulation and I'm having some difficulties in making this code run faster. I profiled the code using callgrind and kcachegrind, and it appears I'm using about 30% of my time on ...
6
votes
1answer
344 views

N-body sim with Barnes-Hut - Follow up

Link to previous question: Barnes-Hut N-body simulator It still has issues I'm sure. Node.h ...