This site requires JavaScript, please enable it in your browser!
Greenfoot back
CharBun
CharBun wrote ...

2018/12/17

Splitting up objects?

CharBun CharBun

2018/12/17

#
I'm working on a variation of the Newton's lab from the book senarios, and I already made it so that if planets touch, they morph into one big planet. Now I'm trying to make it so that if the planet size becomes over 25, it splits into tinier planets who go their own way, but I'm kinda stuck, I tried modifying the code from the astroids (where the asteroids go half the size if they get hit by a bullet) but I got completely lost. Now I'm just stuck with this and I really can't figure out what I should do...
        private void breakUp() 
    {
        if(size > 25) 
        {
            
        }
    }
danpost danpost

2018/12/17

#
CharBun wrote...
if the planet size becomes over 25, it splits into tinier planets who go their own way. I tried modifying the code from the astroids (where the asteroids go half the size if they get hit by a bullet) but I got completely lost.
Just create two new Body objects, add them into the world at the over-sized body's location, set their vectors to that of the over-sized body and maybe turn them a bit in opposite directions; then remove the over-sized one. Well, it should be as simple as that, except for the fact that you cannot currently get the direction of a body -- you can only get the speed (which is the length of the vector; the angle of the vector is the direction). Add the following method to the SmoothMover class:
public Vector getVelocity()
{
    return velocity;
}
Then you can do this to split a body:
Vector velocity = getVelocity;
int direction = velocity.getDirection();
int length = velocity.getLength();
Body b1 = new Body(20, 300, new Vector((direction+30)%360, length), getImage().getColorAt(12, 12));
Body b2 = new Body(20, 300, new Vector((direction+330)%360, length), getImage().getColorAt(12, 12));
getWorld().addObject(b1, getX(), getY());
getWorld().addObject(b2, getX(), getY());
getWorld().removeObject(this);
You need to login to post a reply.