Apologies because this is a long post and I am inexperienced. I would really appreciate the input from some experienced developers.
These are my two goals for this project:
To learn the best method of managing the game loop and flow of the program so that it is easily understandable, clean, and reusable.
To learn about how to efficiently and cleanly handle the physics calculations of a game. I would like to learn the method that is implementable on a scale that can accommodate a lot of collisions.
The project will be written in using Javascript/HTML 5.
The way I was thinking about it is to include attach implement an physics object constructor that attaches a physics object to each class object that will have physics done to it.
For example:
var playerBullets = [];
playerBullets.push(Bullets()); //Bullets() is the constructor
//inside the Bullets() constructor I would have the following:
this.physics = Physics(); //where Physics() is the constructor for a physics obj
The physics object would be set up like the following:
{
x: 0,
y: 0,
xVelocity: 0,
yVelocity: 0,
xAccel: 0,
yAccel: 0,
mass: 0,
restitution: 0,
angularVelocity: 0,
linearDampening: 0,
}
Then from the gameLoop:
Function gameLoop() {
...
update();
draw();
}
Function update() {
...
//code here to iterate through objects to update them individually
handleCollisions();
handlePhysics();
}
Function handleCollisions() {
...
//detect collisions between objects and mark which ones have collided with each other
}
Function handlePhysics() {
...
//code to iterate through objects and update the velocities
handleCollisions();
}
Questions
What might be some problems I would run into by doing it this way?
Should I include any methods in the physics object or should I handle all of my physics operations in a special function inside the gameloop?
Is it better to handle the physics together, or handle the physics of each object separately in its own update function?
What is the best practice way to handle physics in a game?
EDIT I learn better by seeing examples so it would be very helpful, and I'd really appreciate it, if I could see some accompanying pseudo-code.