Gravity Simulator: JavaScript & HTML Canvas Code Review

In summary, the speaker has been working on a gravity simulator using JavaScript and HTML Canvas. They are looking for feedback on their program and specifically, their methods for calculating acceleration and velocity for the planets. The program can be found on a website or saved as an HTML file. The speaker also mentions various keys and functions within the program. They admit that their coding skills are still developing and welcome any help or suggestions. The conversation ends with a thank you and a link to the program.
  • #1
Kaura
122
22
Recently I have been working on a gravity simulator using JavaScript and HTML Canvas
I know that is not the most practical method but it fits my current skill set at the moment
I have worked out the basics but due to my lack of experience in coding I was simply looking for people who better understand the language as well as the physics applied by my program to take a look at it and give feedback and what works well and more importantly what I could improve
I especially would like feedback on my way of calculating acceleration and velocity for the planets as I am almost positive that better methods are available
I also believe that since the canvas is updated based on frames and frame rate can vary this might cause an issue in the calculations so any help resolving that issue would be appreciated

The code the for program can be found here
http://pastebin.com/raw/CsAhEheN

As I stated it is currently using JavaScript and HTML Canvas so the program itself is HTML
To run the program one can either save the code as an HTML file or use a HTML simulating website such as http://www.onlinehtmleditor.net/ which will show the program in a preview box

In the program itself the gravitational constant can be altered using the Q and W keys, the size of newly created planets can be altered using the A and S keys, the space image background can be toggled using the B key, and the program can be paused using the space bar

The code right now is probably very sloppy at the moment since I am fairly new to coding therefore any help at all would be greatly appreciated
Thank you so much for helping me and good luck
 
  • Like
Likes CynicusRex
Technology news on Phys.org

1. What is a gravity simulator?

A gravity simulator is a computer program that uses mathematical equations to simulate the effects of gravity on objects. It allows users to visualize and study the behavior of objects in a gravitational field.

2. How does a gravity simulator work?

A gravity simulator works by using Newton's law of universal gravitation to calculate the force between two objects. The program then uses this force to update the position and velocity of each object in small time steps, creating a continuous simulation of the objects' movement.

3. What is the purpose of using JavaScript and HTML Canvas for a gravity simulator?

JavaScript is a programming language commonly used for creating interactive web applications, while HTML Canvas is a web technology that allows for dynamic and interactive graphics. Together, they provide a powerful platform for creating a realistic and visually appealing gravity simulator that can be accessed through a web browser.

4. Can a gravity simulator be used for educational purposes?

Yes, a gravity simulator can be a valuable educational tool for understanding the principles of gravity and how it affects the motion of objects. It can also be used to demonstrate the effects of different factors such as mass and distance on gravitational force.

5. Is it possible to customize the gravity simulator to simulate different scenarios?

Yes, the code for a gravity simulator can be customized to simulate various scenarios by changing the properties of the objects, such as their mass and initial position. This allows for a wide range of simulations, from simple two-body systems to more complex scenarios with multiple objects and gravitational interactions.

Similar threads

  • Programming and Computer Science
Replies
5
Views
389
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
999
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
879
  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Computing and Technology
Replies
3
Views
2K
Back
Top