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

2014/7/14

Can't work out acceleration issues.

My objects accelerate in the positive x direction and I'm not sure why. The objects should accelerate towards each other their respect to their mass This is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public void act()//in mass1 object class
    {
        setLocation(getX()+velocity.getX(),getY()+velocity.getY());
        myWorld world = (myWorld)getWorld();
        Mass2 mass2 = world.getMass2();
        vector grav = gravitateTo(mass2, mass2.mass);
        velocity.addTo(grav);
    }
 
public double angleTo(Actor a)//in mover class now
    {
        return Math.atan2(a.getY()-this.getY(), a.getX()-this.getY());
    }
 
    public double distanceTo(Actor a)
    {
        double dx = a.getX()-this.getX();
        double dy = a.getY()-this.getX();
        return Math.sqrt(dx*dx+dy*dy);
    }
 
    public vector gravitateTo(Actor a, double a_mass)
    {
        vector grav = new vector(1,0);
        double dist = this.distanceTo(a);
        double angle = angleTo(a);
        double length = getFG(a_mass, dist);
        grav.setX(length*Math.cos(angle));
        grav.setY(length*Math.sin(angle));
 
        return grav;
    }
 
    public double getFG(double m1, double r)
    {
        double FG = G*((m1)/(r*r));
        return FG;
    }
Edit: Link to my scenario: http://www.greenfoot.org/scenarios/11828
danpost danpost

2014/7/14

#
I have been looking into your situation. Have not yet figured out why the acceleration tends toward positive x. However, I have found a couple of issues in the Mover class. Your angleTo and distanceTo methods have mixed up getX and getY coding. Also, it would probably be more accurate to use exactX and exactY in those methods and have getExactX and getExactY methods that you call from the act methods of the mover objects to maintain the accuracy of their states. One other thing I noticed what the unnecessary setting of fields in the setAngle and setLength methods of the vector class (dx and dy are set in the call to updateXY). Finally, as long as you keep the value pairs updated, there is no need to compute the values in the get methods that return them. That is, for example, getY can just return dx and getAngle can just return angle. There should be no need to re-compute the values.
danpost danpost

2014/7/14

#
Actually, fixing the two getX/getY mix-ups does stop the acceleration toward positive x.
Can you share your fix? I've been trying to create a getExactX() methods but my scenario won't compile and returns cannot find symbol - method getExactX(). This is my code
1
2
3
4
public double getExactX()// in mover class
    {
        return exactX;
    }
danpost danpost

2014/7/14

#
The fix was changing the last 'getY' in the angleTo method of the Mover class to 'getX' and changing the last 'getX' in the distanceTo method to 'getY'. You do not have to add the 'getExactX' and 'getExactY' methods if you do not want to. The exactX and exactY fields have protected access from within the Mover class meaning that can be directly referred to in its subclasses. You should, however, change the 'getX()'s and 'getY()'s within the first statements of the act methods of your sub-classes of Mover (Mass1 and Mass2) to 'exactX' and 'exactY' (or 'getExactX()' and 'getExactY()' if you decide you use the 'get' methods). The 'getExactX' method you have shown above looks correct. Maybe you were trying to call it on an Actor object instead of a Mover, Mass1 or Mass2 object (cast to such types).
You need to login to post a reply.