Sign in to follow this  
Zenryo

Orbiting problem

Recommended Posts

I believe there's currently an issue with pivots in pixi.js (Phaser's underlying rendering engine) that is causing problems with the position of the body. I had a similar issue and had to abandon rotation of the sprite, instead resorting to extending the sprite and rotating a child image instead.

Share this post


Link to post
Share on other sites

To be fair my solution wouldn't be the right solution in your case, as presumably you need an orbiting mechanic and the ability to detect collisions on that orbiting item. In your case I may do something like this:

// Create our solar system groupvar solarSystem = game.add.group();// Create our sun and use the group's 0, 0 position as the center of the solar systemvar sun = game.add.sprite(0, 0, 'sun', 0, solarSystem);// Set the sun's anchor to 0.5, 0.5 so it is completely centeredsun.anchor.set(0.5);// Create a group to manage the orbit of our planet, and place it inside the solar systemvar planet1Orbit = game.add.group(solarSystem);// Create our planet and place it off-centre by 50 pixelsvar planet1 = game.add.sprite(50, 0, 'planet', 0, planet1Orbit);// Also set the planet's anchor to 0.5 so we don't get any weird offsetsplanet1.anchor.set(0.5);// Finally add a body to the planet so we can detect collisionsgame.physics.enable(planet1, Phaser.Physics.ARCADE);// And now the magic part - rotate the planet1Orbit group instead of the planetfunction update() {  planet1Orbit.angle += 1;  // you can also make the planet spin separately  planet1.angle += 2;  // or you could make it spin in the opposite direction at the same rate, and the planet will appear to move around the sun without rotating itself  // planet.angle -= 1;}

This is a very simple way to get an orbit-like mechanic just by using groups. Because the scene graph allows objects to be placed inside other objects and inherit their transforms, you end up with the same effect. It does however have limitations - you will only be able to create circular orbits with this method; if you want to create more advanced orbits you'll have to do it with carefully balanced physics and maths.

Share this post


Link to post
Share on other sites

Hopefully pivot will be fixed soon and you won't have to do it this way; however one of the quite a nice side effects of doing it this way is you could technically add more orbiting moons to any of the planets by recursively applying this technique, and get quite complex orrery-style systems going.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.