Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

5
votes
2answers
75 views

Simulation of spring-loaded inverted pendulum

I've finally committed to moving to Python 3 from MATLAB. I'm currently porting my MATLAB code, and I'm sure I'm missing a lot of common best-practices. For reference, I'm simulating a spring-loaded ...
6
votes
1answer
141 views
+50

Golf Physics “Game”

Continuation of this post 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 ...
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 ...
8
votes
2answers
884 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 ...
1
vote
2answers
106 views

Perturbed phase oscillator integration

I am integrating a system of perturbed phase oscillators. I define the system of equation and also the Jacobian matrix. I have to reshape the one dimensional vector of system state to a 2 dimensional ...
2
votes
0answers
86 views

Simple Flappy Bird Clone using Kivy (with very minimum physics)

This is a very simple clone of Flappy Bird using Kivy, to be used for programming class for teens. The game consists of 3 classes: Bird, ...
13
votes
2answers
675 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" ...
3
votes
1answer
79 views

Very slow frame rate in C++ N-body simulation using Barnes-Hutt and RK4

I am writing the code for my EPQ project and am aiming to produce a graphic showing the collapse of a particle cloud with around 10^5/6 particles. My current code will spawn a specified number of ...
2
votes
1answer
47 views

Calculate supporting forces of a simply supported beam

The program uses a very simple algorithm to calculate the supporting forces of a simply supported beam. I'd like to know if thorwables are good practice to handle semantic errors and if one should ...
1
vote
1answer
60 views

Vector addition and Pythagorean Theorem code

I would like to share my code that is meant to help me add 2 vectors together. It is very simple and I would like to kindly ask you whether you could review my code. Also, I decided to keep separate ...
0
votes
1answer
48 views

Modeling a capacitor with DC Bias

I am modeling a capacitor which has its capacitance varying according to the DC Bias. The DC Bias is computed by taking the mean of the voltage across the capacitance. ...
2
votes
2answers
95 views

Calculate next position and velocity after “bounce”

Intro and code I have the following function that calculates an objects "next" position and velocity (in 1 dimension) given a boundary [0, max]. ...
4
votes
0answers
43 views

Tweaking this Pong Game to embed a smarter AI

I am new to game development and took on the task to create a Pong replica due to my curiosity and interest in the field. I decided to write it in JavaScript because the game is not too labor ...
29
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. ...
3
votes
2answers
415 views

Simulating a system of moving points

This is an update of the code from my previous question. I'm simulating a system of particles which interact with each other and move and rotate due to the interaction. There are 2000 particles in ...
0
votes
2answers
293 views

Simulation of interacting particles [closed]

I'm simulating a system of particles which interact with each other and move and rotate due to the interaction. There are 2000 particles in total but each particle interacts with other particles and ...
2
votes
2answers
807 views

Simulating motion of particles

Description The code does particle simulation. In this code, some particles take steps with a certain distribution length. if particles go beyond a curve, the code put them back. Code ...
2
votes
3answers
2k views

Calculating work using dot product in C++

Write a C++ program fulfilling the following task: The user should enter the 3 dimensional force vector F and a 3 dimensional length vector s. The force is assumed to be constant. The program then ...
2
votes
1answer
255 views

Cross product to find Lorentz force in C++

User enters velocity vector \$v\$, charge \$q\$ of particle and magnetic field vector \$B\$ and the code prints the Lorentz force \$F = q * (v * B)\$ ...
11
votes
3answers
302 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 ...
1
vote
1answer
135 views

Closed particle system simulation in vanilla Javascript

I have ported my simulation from Java to vanilla Javascript. The most important aim was to keep the sum of all energies constant. I have this: simulation.js ...
4
votes
3answers
364 views

Simulating a closed particle system in Java

I have this program that simulates a closed particle system. Closed in this context means that the sum of all energies is constant. My primary concern is code itself, yet I would like to hear comments ...
2
votes
1answer
33 views

NodeJS module to calculate linear motion

I am on the process of changing jobs, so I would like to get an idea of what I do wrong in order to improve. For this, I have created a small node module. It's very simple; it calculates linear ...
7
votes
1answer
544 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 ...
3
votes
1answer
1k views

2D Physics Simulation (game-like)

My program is meant to simulate 2D physics(hopefully 3D as well in the future), namely classical mechanics and eventually electromagnetism. Currently it simulates projectile motion by firing a ...
2
votes
2answers
3k views

My first Physics Engine C++

I have made my first physics engine for simulating circles in a plane. Let me know what you think about it. ...
2
votes
4answers
358 views

Calculating energy from mass and light speed

I'm learning Java JDK 1.8 since one day and I'm using Java's methods to the first time. I have never used Java before because I'm a Python/Django developer. I created a method which let to get energy ...
13
votes
2answers
360 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 ...
6
votes
3answers
394 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
769 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 ...
9
votes
2answers
3k 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 ...
17
votes
3answers
232 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 ...
3
votes
1answer
71 views

Accurately calculating gravity for large systems

I'm in the middle of a program to simulate gravity between particles for > 50000 particles. It works so far, but the way I calculate gravity seems to be a tad askew. Generally, it works. It fails when ...
5
votes
1answer
107 views

Calculating a table of deBroglie wavelengths for various electron energies

Here is the formula for the deBroglie wavelength of an electron versus its kinetic energy: $$ \lambda(E_k) = h\left/\sqrt{\frac{(E_k+m_eC^2)^2-m_e^2C^4}{C^2}}\right.$$ and here is simple script that ...
13
votes
4answers
321 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 ...
2
votes
0answers
101 views

didSimulatePhysics to animate a ball, taking 45% of CPU

According to my time profiler this is taking 45% of my cpu usage: have tried to optimize, and someone on stack overflow -- recommended to post here. Time profiler says 99% of the time is spent on the ...
4
votes
1answer
109 views

Implementation of Point, Velocity and Acceleration in Haskell

I'm trying to implement point, velocity and acceleration types. They should be connected by some derive function which: takes time and velocity and returns a ...
3
votes
1answer
109 views

Quickly applying gravity force between bodies

I have a function for applying gravity forces between every possible pair of bodies on my game. It is the most used function, and can run more than 100k times per frame so every minor improvement on ...
2
votes
0answers
357 views

Physical simulation of diffusion-limited aggregates

The following code generates Diffusion Limited Aggregates on a two-dimensional square lattice. Some of the code has been omitted (e.g. support for differing lattice types and dimensions) for code-...
1
vote
0answers
549 views

Snell's law using Zoeppritz equation by matrices

I have created the following code to calculate Snell's law angles, based on Zoeppritz equations on complex plane. The code works, seems it is returning valid values, but after all the code just looks ...
2
votes
2answers
3k views

Simulation of 2D elastic balls

Following this SO post and this Wikipedia article, I wrote a Python script to simulate physics of 2D elastic balls. I define the physical behaviour of each ball in the ...
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:...
0
votes
1answer
89 views

Calculating an object's stopping distance

I'm trying to smooth the movement of a cursor which has a set acceleration and max speed. I wrote this function to calculate the stopping distance of an object given its speed and acceleration, and it ...
3
votes
1answer
580 views

Explosion animation plugin for a game

I have been writing a small particle plugin since I want to learn how physics works for games. Now I have tried to get this as clean as I possible could but It feels something is missing. Am I ...
8
votes
3answers
797 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 ...
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 ...
3
votes
3answers
286 views

Creating and manipulating FITS files

I wrote a program that manipulated data from VLA observations and created FITS files to be used in creating spectral energy distributions of high redshift radio galaxies. I really tried to be as ...
6
votes
1answer
2k views

Java snow animation

I was inspired by this to write a graphic snow animation in Java/Processing. It works great, but it has some design issues. The full project is here on github; ...
6
votes
1answer
133 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, ...
2
votes
1answer
555 views

Velocity verlet implementation

My code is meant to update the positions and velocities of planets. There are a few methods in this class, but updatePosition and ...