I have a mystery in my code here
in
Programming Questions
•
1 years ago
Hi guys;
I was just doing fiction solar system, the code did OK until the last new object in my code. For example : body_b.cx=body_a.x but when I did a println() the variable, the data is simply NOT equal... What the ???
Can somebody explain why the data ( romulus.x and moon_b.cx ) don't match ?
Go ahead, try it... the last object draw is going across the sceen instead of orbiting.
PS: I am sorry that sound like a re-post.
Here the results:
output.println(imaginary_point.x+" "+romulus.x+" "+moon_b.cx+" "+moon_a.x+" "+moon_b.x);
799.95435 769.9726 506.2827 794.93835 481.31696
799.81726 769.8903 512.5625 794.75336 487.69946
799.58887 769.75323 518.8367 794.44543 494.14447
799.2692 769.56116 525.1021 794.01483 500.64844
798.8584 769.31415 531.3562 793.4623 507.20807
798.35657 769.01215 537.5959 792.7886 513.81946
797.76385 768.65497 543.8184 791.9945 520.4789
797.08044 768.2426 550.02106 791.08124 527.18243
796.3065 767.77484 556.20087 790.05 533.9257
795.4423 767.2515 562.3551 788.90216 540.70447
794.48816 766.67267 568.4809 787.6394 547.51416
793.4443 766.0379 574.5756 786.2633 554.3502
792.31104 765.3472 580.6365 784.7759 561.2078
791.08875 764.60034 586.66077 783.17896 568.08215
789.7777 763.79694 592.64575 781.4746 574.9681
788.37854 762.9371 598.5888 779.66534 581.86053
786.8914 762.02026 604.48737 777.7533 588.75433
785.31696 761.04645 610.3386 775.7411 595.644
783.6556 760.01526 616.14026 773.6312 602.5243
781.9078 758.92645 621.88965 771.42645 609.38965
780.0741 757.7797 627.5843 769.1295 616.2345
778.15515 756.57495 633.2217 766.74335 623.0533
776.1515 755.31177 638.79956 764.271 629.84033
774.06366 753.98975 644.3153 761.71515 636.5899
771.89233 752.6087 649.7668 759.07916 643.2963
769.6382 751.16833 655.1517 756.36615 649.95386
767.302 749.66846 660.46765 753.57935 656.55676
764.8843 748.10846 665.7125 750.7217 663.0993
762.3859 746.48834 670.88434 747.79675 669.5759
799.81726 769.8903 512.5625 794.75336 487.69946
799.58887 769.75323 518.8367 794.44543 494.14447
799.2692 769.56116 525.1021 794.01483 500.64844
798.8584 769.31415 531.3562 793.4623 507.20807
798.35657 769.01215 537.5959 792.7886 513.81946
797.76385 768.65497 543.8184 791.9945 520.4789
797.08044 768.2426 550.02106 791.08124 527.18243
796.3065 767.77484 556.20087 790.05 533.9257
795.4423 767.2515 562.3551 788.90216 540.70447
794.48816 766.67267 568.4809 787.6394 547.51416
793.4443 766.0379 574.5756 786.2633 554.3502
792.31104 765.3472 580.6365 784.7759 561.2078
791.08875 764.60034 586.66077 783.17896 568.08215
789.7777 763.79694 592.64575 781.4746 574.9681
788.37854 762.9371 598.5888 779.66534 581.86053
786.8914 762.02026 604.48737 777.7533 588.75433
785.31696 761.04645 610.3386 775.7411 595.644
783.6556 760.01526 616.14026 773.6312 602.5243
781.9078 758.92645 621.88965 771.42645 609.38965
780.0741 757.7797 627.5843 769.1295 616.2345
778.15515 756.57495 633.2217 766.74335 623.0533
776.1515 755.31177 638.79956 764.271 629.84033
774.06366 753.98975 644.3153 761.71515 636.5899
771.89233 752.6087 649.7668 759.07916 643.2963
769.6382 751.16833 655.1517 756.36615 649.95386
767.302 749.66846 660.46765 753.57935 656.55676
764.8843 748.10846 665.7125 750.7217 663.0993
762.3859 746.48834 670.88434 747.79675 669.5759
Here my code:
- PrintWriter output;
- Body sun_a;
- Body sun_b;
- Body planet_a;
- Body imaginary_point;
- Body remus;
- Body romulus;
- Body moon_a;
- Body moon_b;
- void setup()
- {
- size(1000,1000);
- frameRate(30);
- ellipseMode(CENTER);
- output = createWriter("result.txt");
- sun_a=new Body(width/2,height/2,50,50,1,1);
- sun_b=new Body(width/2,height/2,50,50,1,1);
- planet_a=new Body(width/2,height/2,150,10,2,-1);
- imaginary_point=new Body(width/2,height/2,300,1,1,1);
- remus=new Body(width/2,height/2,30,20,2,-1);
- romulus=new Body(width/2,height/2,30,20,2,-1);
- moon_a=new Body(width/2,height/2,25,8,3,1);
- moon_b=new Body(width/2,height/2,25,8,3,1);
- }
- void draw()
- {
- background(255);
- sun_a.domove();
- sun_a.render();
- sun_b.domove();
- sun_b.x=sun_b.cx+(sun_b.cx-sun_b.x);
- sun_b.y=sun_b.cy+(sun_b.cy-sun_b.y);
- sun_b.render();
- planet_a.domove();
- planet_a.render();
- imaginary_point.domove();
- remus.cx=imaginary_point.x;
- remus.cy=imaginary_point.y;
- remus.domove();
- remus.render();
- romulus.cx=imaginary_point.x;
- romulus.cy=imaginary_point.y;
- romulus.domove();
- romulus.x=romulus.cx+(romulus.cx-romulus.x);
- romulus.y=romulus.cy+(romulus.cy-romulus.y);
- romulus.render();
- moon_a.cx=romulus.x;
- moon_a.cy=romulus.y;
- moon_a.domove();
- moon_a.render();
- // That next parts is not working
- moon_b.cx=romulus.x;
- moon_b.cx=romulus.y;
- moon_b.domove();
- moon_b.x=moon_b.cx+(moon_b.cx-moon_b.x);
- moon_b.y=moon_b.cy+(moon_b.cy-moon_b.y);
- moon_b.render();
- // notice moon_b.cx and romulus.x have to be equal but THEY ARE NOT - see results
- output.println(imaginary_point.x+" "+romulus.x+" "+moon_b.cx+" "+moon_a.x+" "+moon_b.x);
- }
- void keyPressed()
- {
- output.flush(); // Writes the remaining data to the file
- output.close(); // Finishes the file
- exit(); // Stops the program
- }
- // The class came from jbum, I modify to add a direction
- class Body
{ - float cx,cy,diam;
- float orad;
- float orbitSpeed;
- float ang;
- float x,y;
- int direction;
- Body(float _cx, float _cy, float _orad, float _diam, float _orbitSpeed, int _direction)
- {
- cx = _cx; cy = _cy;
- diam = _diam;
- orad = _orad;
- x = 0; y = 0;
- ang = 0;
- orbitSpeed = _orbitSpeed;
- direction = _direction;
- }
- void render()
- {
- ellipse(x,y,diam,diam);
- }
- void domove()
- {
- if (direction==1)
- {
- ang += radians(orbitSpeed);
- }
- if (direction==-1)
- {
- ang-= radians(orbitSpeed);
- }
- x = cx + cos(ang)*orad;
- y = cy + sin(ang)*orad;
- }
- }
1