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

2024/12/26

how to move objects towards each other using setLocation

Skye_B Skye_B

2024/12/26

#
I'm trying to make a little physics engine and want to move one planet towards another planet using setLocation(). basically, what i've been doing is using turnTowards() to turn one planet towards another planet, then the move() method to move the planet to the other planet. this does work technically, but I basically want to add angular momentum so the planet doesn't go straight towards the other objects. I don't know if that made any sense, but hopefully yall can help. This is all my code for the Planet1 class. any help regarding making a physics engine or my specific problem would be appreciated!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class Planet1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Planet1 extends Planets
{
    private final int gravityConstant = 3;
    private int mass;
    private int radius;
    private int gravity;
    
    public Planet1(int mass)
    {
        this.mass = mass;    
    }
    
    public void act()
    {
        calculateGravity();
        detectPlanet();
    }
    
    public void detectPlanet()
    {
        List<Planet1> planets = getObjectsInRange(600, Planet1.class);
        if(!planets.isEmpty())
        {
            Actor planet = (Actor)planets.get(0);
            
            planet.turnTowards(getX(), getY());
            planet.move(gravity);
        }
    }
    
    public void calculateGravity()
    {
        radius = getImage().getWidth()/2;
        gravity = (gravityConstant * mass) / (radius * radius);
    }
}
danpost danpost

2024/12/27

#
Skye_B wrote...
I'm trying to make a little physics engine and want to move one planet towards another planet using setLocation(). basically, what i've been doing is using turnTowards() to turn one planet towards another planet, then the move() method to move the planet to the other planet. this does work technically, but I basically want to add angular momentum so the planet doesn't go straight towards the other objects. << Code Omitted >>
Well, you could track position coordinates and speed along both axes with double values. Then all you need do is adjust speed values depending on gravity effect. That way, you can use setLocation(int, int) just by typesetting the coordinates to int values:
// with fields for position and speed as
private double xCoord, yCoord, xVel, yVel;
// then the statement would look like this
setLocation((int)xCoord, (int)yCoord);
// after adjusting velocities for all gravity effects, then adding those adjusted velocities to the coordinates
Skye_B Skye_B

2024/12/27

#
danpost wrote...
Skye_B wrote...
I'm trying to make a little physics engine and want to move one planet towards another planet using setLocation(). basically, what i've been doing is using turnTowards() to turn one planet towards another planet, then the move() method to move the planet to the other planet. this does work technically, but I basically want to add angular momentum so the planet doesn't go straight towards the other objects. << Code Omitted >>
Well, you could track position coordinates and speed along both axes with double values. Then all you need do is adjust speed values depending on gravity effect. That way, you can use setLocation(int, int) just by typesetting the coordinates to int values:
// with fields for position and speed as
private double xCoord, yCoord, xVel, yVel;
// then the statement would look like this
setLocation((int)xCoord, (int)yCoord);
// after adjusting velocities for all gravity effects, then adding those adjusted velocities to the coordinates
Okay I see thank you, however, my main problem right now is the actual calculation of the velocities based on the gravity affects. some sources say I need to do fancy math with trigonometric functions, some say I have to use square roots and exponents, So I'm a bit stuck on the actual math part there. thank you for this however! but yes my main problem is the actual math calculations at the moment lol
danpost danpost

2024/12/27

#
Skye_B wrote...
my main problem right now is the actual calculation of the velocities based on the gravity affects. some sources say I need to do fancy math with trigonometric functions, some say I have to use square roots and exponents, So I'm a bit stuck on the actual math
Well, in one of my scenarios, I used something like this:
public void applyGravity()
{
    int dist = (int)Math.hypot(other.getQX()-getQX(), other.getQY()-getQY())0; // distance to other object
    int dir = (int)(Math.atan2(other.getQY()-getQY(), other.getQX()-getQX())*180/Math.PI); // angle of force
    addForce(gravityConstant/(other.mass*dist*dist), dir);
}
I made some adjustments to the code since the "other" object was always in the middle of the screen; also, it had a smooth moving superclass. The actual force calculation may not, at this point, now be "tuned" properly. QX and QY are position coordinates and correspond to the position of the object in a more precise form than pixel units. The addForce method is found in my smooth moving/smooth rotating support class called QActor. It can be found in my Asteroids w/Improved QActor SuperClass. The method itself looks something like this:
public void addForce(int amount, int direction)
{
    vX += Math.cos((double)direction*Math.PI/180)*(double)amount; // new horizontal speed
    vY += Math.sin((double)direction*Math.PI/180)*(double)amount; // new vertical speed
}
Again, I over-simplified the method (removing some things that the QActor class needed). You should get the gist of it, at least.
You need to login to post a reply.