ruzi Posted June 25, 2014 Share Posted June 25, 2014 What better use Arcade or P2? I want make game close to the realistic physics, gravity, object, mass. Link to comment Share on other sites More sharing options...
lewster32 Posted June 25, 2014 Share Posted June 25, 2014 P2 is more realistic and will do the things you require reasonably well. If you're absolutely hell-bent on, for instance, physically correct gravity and mass simulations, such as in the case of celestial physics simulations, you may want to do a bit more research and implement this yourself, or via a third party library, as P2 is still a rough approximation with an emphasis on speed. Link to comment Share on other sites More sharing options...
SeanEBaby Posted June 25, 2014 Share Posted June 25, 2014 Speaking as a computational modelling engineer... (so forgive me if I go crazy with little details) We use arcade in Slingshot and it does celestial physics fine (for a game). Everything in our game is simulated realistically, the game calculates orbital velocities of the planets which orbit the sun and units which orbit those planets. All you need is acceleration at the end of the day.... assuming the time-stepping is stable enough. I've run our simulation on arcade physics for a good 10 minutes and it remains accurate. I did find there was an upper limit on the mass of the sun, which makes sense because that's what dictates your force which dictates your acceleration which dictates your timestep. Slingshot has lots of bodies which all get put into the gravity simulation, bullets, debris particles, asteroids, item drops, ships. I'm talking hundreds maybe thousands, so I figured the speed of arcade was more important than the realism of P2. The main limitation you'll come across is collision detecting using arcade, because most likely you'll want to rotate stuff and all collisions in arcade are non-rotated rectangles. We get around it by using effects to hide the collisions, but don't tell anyone I keep meaning to write a blog post explaining all the orbital theory - watch this space Edit: Just to be clear, the game calculates the orbital velocities on creation, drops units in at that velocity then just updates using an N-body Newtonian gravity simulation. The fact stuff stays in orbit shows us that it's accurate Link to comment Share on other sites More sharing options...
lewster32 Posted June 25, 2014 Share Posted June 25, 2014 That's really interesting Sean - so you've modeled large scale gravity with Arcade? How did you approach this given the gravity calculation in Arcade is linear? Presumably this meant adding considerably to Arcade's standard set of features? Link to comment Share on other sites More sharing options...
SeanEBaby Posted June 25, 2014 Share Posted June 25, 2014 I just used accelerate to point instead of the gravity function... here is the function I used to update each body... edit: glad you find it interesting function updateBody(ast_) { ast_.body.acceleration.setTo(0, 0); //Loop around all my planets planets.forEach(function(item, ast) { var ax = ast.body.acceleration.x; var ay = ast.body.acceleration.y; var dx = ast.x - item.x; var dy = ast.y - item.y; var r = dx * dx + dy * dy; if (r > planetRadius * planetRadius) { //The force on a body is (body.mass)/r^2 so taking every non planet body with // mass=1 the acceleration = force, (r is already squared save us a sqrt) game.physics.arcade.accelerateToObject(ast, item, (((item.body.mass) / r))); ax = ax + ast.body.acceleration.x; ay = ay + ast.body.acceleration.y; ast.body.acceleration.setTo(ax, ay); } }, this, false, ast_); //Acceltate to star var ax = ast_.body.acceleration.x; var ay = ast_.body.acceleration.y; var dx = ast_.x - star.x; var dy = ast_.y - star.y; var r = dx * dx + dy * dy; if ((r < starRadius * starRadius) || (r > killRadius * killRadius)) { explosion(ast_.body.x, ast_.body.y, ast_.body.velocity.x - inaccuracy, ast_.body.velocity.x + inaccuracy, ast_.body.velocity.y - inaccuracy, ast_.body.velocity.y + inaccuracy); ast_.kill(); } else { game.physics.arcade.accelerateToObject(ast_, star, (starMass / r)); ax = ax + ast_.body.acceleration.x; ay = ay + ast_.body.acceleration.y; ast_.body.acceleration.setTo(ax, ay); } } Link to comment Share on other sites More sharing options...
lewster32 Posted June 25, 2014 Share Posted June 25, 2014 Impressive! And this is why I'm clearly not a computational modelling engineer Link to comment Share on other sites More sharing options...
SeanEBaby Posted June 25, 2014 Share Posted June 25, 2014 Thanks... ...I hope my admission to that didn't come off as pretentious.... ....but it's all phaser really, I've just have trained to see the world with little force arrows everywhere and convert those into accelerations.... The fact I can do a stable real time N-body (ish) simulation in a browser is freakin nuts! Link to comment Share on other sites More sharing options...
lewster32 Posted June 25, 2014 Share Posted June 25, 2014 No not at all, I'm always impressed by anything like this, mainly because I'm pretty much dyscalculic; my approach to maths being based primarily on brute force. Link to comment Share on other sites More sharing options...
ruzi Posted June 25, 2014 Author Share Posted June 25, 2014 Speaking as a computational modelling engineer... (so forgive me if I go crazy with little details) We use arcade in Slingshot and it does celestial physics fine (for a game). Everything in our game is simulated realistically, the game calculates orbital velocities of the planets which orbit the sun and units which orbit those planets. All you need is acceleration at the end of the day.... assuming the time-stepping is stable enough. I've run our simulation on arcade physics for a good 10 minutes and it remains accurate. I did find there was an upper limit on the mass of the sun, which makes sense because that's what dictates your force which dictates your acceleration which dictates your timestep. Slingshot has lots of bodies which all get put into the gravity simulation, bullets, debris particles, asteroids, item drops, ships. I'm talking hundreds maybe thousands, so I figured the speed of arcade was more important than the realism of P2. The main limitation you'll come across is collision detecting using arcade, because most likely you'll want to rotate stuff and all collisions in arcade are non-rotated rectangles. We get around it by using effects to hide the collisions, but don't tell anyone I keep meaning to write a blog post explaining all the orbital theory - watch this space Edit: Just to be clear, the game calculates the orbital velocities on creation, drops units in at that velocity then just updates using an N-body Newtonian gravity simulation. The fact stuff stays in orbit shows us that it's accurate yes, i have problem, in Physics.ARCADE use rectangles for collisions, but in Physics.P2JS use sterling circle thanks for you post! Link to comment Share on other sites More sharing options...
Recommended Posts