Gravity Simulator: JavaScript & HTML Canvas Code Review

Click For Summary
SUMMARY

The discussion centers on a gravity simulator developed using JavaScript and HTML Canvas. The creator seeks feedback on their methods for calculating acceleration and velocity of planets, expressing concern over potential inaccuracies due to frame rate variability. They provide a link to their code hosted on Pastebin and mention the use of specific keys for interactive features within the simulation. The creator acknowledges their inexperience and requests constructive criticism to improve their coding skills.

PREREQUISITES
  • JavaScript programming fundamentals
  • HTML Canvas API knowledge
  • Basic physics concepts related to gravity and motion
  • Understanding of frame rate and its impact on animations
NEXT STEPS
  • Research advanced methods for calculating gravitational acceleration in simulations
  • Explore JavaScript performance optimization techniques for smoother animations
  • Learn about using requestAnimationFrame for frame rate management in HTML Canvas
  • Investigate physics engines like Matter.js for more accurate simulations
USEFUL FOR

JavaScript developers, physics enthusiasts, and anyone interested in creating interactive simulations using HTML Canvas.

Kaura
Messages
122
Reaction score
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   Reactions: CynicusRex
Technology news on Phys.org

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K