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
2
votes
1answer
82 views

Simplified three-body simulation

The code below simulates a given Newtonian 3 body system. Each row per slice is supposed to represent a Cartesian component for the initial trajectory vector and for the distance vectors to the other ...
2
votes
1answer
47 views

Trapezoidal rule for set of data

Here is the question from the book of Mark Newman-Computational Physics Exc 5.1 a) Read in the data and, using the trapezoidal rule, calculate from them the approximate distance traveled by the ...
4
votes
2answers
45 views

Simple spatial grid for particle system

I am going to simulate a particle system, the particles are infinitesimal points which apply forces on the neighbors and I need a fast way of making proximity checks, they are going to have a maximum ...
0
votes
0answers
39 views

Repelling particles from one another while attracting them to a location

I am trying to repel PNGs as particles from one another while attracting them to a location. I am not sure if I've done it correctly, I'd like to be sure before I continue further. ...
4
votes
1answer
58 views

Gravity simulation using Numpy and Pygame

This is a simple gravity simulator coded in Python 3.7 using Numpy and Pygame. I was wondering if it can be optimized further. Initially I had coded it using pure Python lists, using nested loops to ...
4
votes
0answers
62 views

Butter side up?

I've written a piece of code that determines if a piece toast with butter lands on the butter-side or not, depending on its initial velocity and the table height. The code is written in Mathematica ...
5
votes
2answers
134 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
165 views

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
907 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
109 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
185 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
1k 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
87 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
54 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
87 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
70 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. ...
4
votes
3answers
142 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
1answer
78 views

Pong Game in JS

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 ...
30
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
464 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 ...
1
vote
2answers
309 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
1k 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
271 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)\$ ...
10
votes
3answers
376 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
141 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
429 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
552 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 ...
2
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
407 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
362 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
467 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
845 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
238 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 ...
2
votes
1answer
75 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
111 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
338 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
105 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
113 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
111 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
382 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
645 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
91 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
612 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 ...