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

2015/10/24

Help with my code?

iCodeAsian iCodeAsian

2015/10/24

#
Okay so I have a comet the code is as follows: import greenfoot.*; import java.awt.Color; import java.util.List; /** * Write a description of class Comet here. * * @author (your name) * @version (a version number or a date) */ public class Comet extends SmoothMover { // constants private static final double GRAVITY = 1.9; private static final Color defaultColor = new Color(255, 216, 0); // fields private double mass; private Color color; /** * Construct a comet with default size, mass, movement and color. */ public Comet() { this (20, 300, new Vector(0, 1.0), defaultColor); } /** * Construct a comet with a specified size, mass, movement and color. */ public Comet(int size, double mass, Vector movement, Color color) { this.mass = mass; addForce(movement); GreenfootImage image = new GreenfootImage (size, size); image.setColor (color); image.fillOval (0, 0, size-1, size-1); setImage (image); this.color = color; } /** * Act. That is: apply the gravitation forces from * all other bodies around, and then move. */ public void act() { applyForces(); move();// To be done - not yet implemented getWorld().getBackground().setColorAt(getX(), getY(), color); checkCollision(); } /** * Apple the forces of gravity from all other celestial bodies in * this universe. */ private void applyForces() { List<Comet> comets = getWorld().getObjects(Comet.class); for (Comet comet : comets) { if (comet != this) { applyGravity (comet); } } } /** * Apply the gravity force of a given comet to this one. */ private void applyGravity(Comet other) { double dx = other.getExactX() - this.getExactX(); double dy = other.getExactY() - this.getExactY(); Vector force = new Vector (dx, dy); double distance = Math.sqrt (dx * dx + dy * dy); double strength = GRAVITY * this.mass * other.mass / (distance * distance); double acceleration = strength / this.mass; force.setLength (acceleration); addForce (force); } /** * Return the mass of this comet. */ public double getMass() { return mass; } /** * Check whether we are colliding with another planet */ private void checkCollision() { Comet a = (Comet) getOneIntersectingObject(Comet.class); if(a != null) { getWorld().addObject(new Explosion(), getX(), getY()); getWorld().removeObject(this); } } } When it is in the Space it doesn't collide or react with other objects like my sun and planets. Help please!
danpost danpost

2015/10/24

#
From what I can tell from what is given is that comets will only react with comet objects. All the 'get...Object's method calls have 'Comet.class' as type parameters.
iCodeAsian iCodeAsian

2015/10/25

#
@danpost Okay so what I have done now is put it as a "subclass" of Body. So now it reacts with everything but the gravity of the Comet pulls everything to it.
danpost danpost

2015/10/25

#
iCodeAsian wrote...
@danpost Okay so what I have done now is put it as a "subclass" of Body. So now it reacts with everything but the gravity of the Comet pulls everything to it.
How does the mass of the comet compare to the mass of the other objects (sun, planets, etc.)?
iCodeAsian iCodeAsian

2015/10/26

#
@danpost Okay so I changed my code to put it in Space public void act. public void act() { if (timer == null) { timer = new Timer(); timer.start(); addObject(timer, 50, 20); } if ( Greenfoot.getRandomNumber (500) < 3) { addObject( new Body (10, .0000000000000001, new Vector(215, 6.2), new Color(239, 0, 255)), 1134, 726); } } So now I need to change the vector (215) to a random vector so it shoots at different angles. *Thank you so much for helping me so far and dealing with me*. I've tried math.get so and so but it doesnt recognize math.
danpost danpost

2015/10/26

#
iCodeAsian wrote...
I've tried math.get so and so but it doesnt recognize math.
The Math class will always be recognized. It does not even have to imported. It is available automatically as it is considered as a basic part of the language. An example of its use follows:
1
int mass = 100+(int)(Math.random()*200);
I am not sure what you were trying with "math.get". The only methods of the Math class that start with 'get' are 'getExponent' methods. What were you trying to accomplish by using "math.get so and so"?
iCodeAsian iCodeAsian

2015/10/26

#
@Danpost A senior who took AP Computer Science told me to put math.getRandomNumber, so and so. I figured out eventually he was wrong about an hour or two ago. Now will this code int mass = 100+(int)(Math.random()*200); work for my case? How would I implement it into the part ? 215 is the angle at which it shoots at and I just need it to be a random number every time it shoots out
danpost danpost

2015/10/26

#
First off, the name of the class is 'Math' (with an uppercase 'm'), not 'math'. Secondly, there is no 'getRandomNumber' method in the Math class; however, there is one in the Greenfoot class:
1
int angle = Greenfoot.getRandomNumber(360);
will set the 'angle' variable to an integer value between zero, inclusive, and 360, exclusive. The code I gave in my previous post was just an example of using a method of the Math class and was not intended specifically for your use.
iCodeAsian iCodeAsian

2015/10/27

#
@DanPost Yeah I figured out the math and Math. during school. So it would go something like this? if ( Greenfoot.getRandomNumber (500) < 3) { addObject( new Body (10, .0000000000000001, new Vector(int angle = Greenfoot.getRandomNumber(270), 6.2), new Color(239, 0, 255)), 1134, 726);
danpost danpost

2015/10/27

#
You would either set a variable to its value beforehand or just use the value itself:
1
2
3
4
5
// either
int angle = Greenfoot.getRandomNumber(360);
addObject(new Body(10, .0000000000000001, new Vector(angle, 6.2), new Color(239, 0, 255)), 1134, 726);
// or
addObject( new Body (10, .0000000000000001, new Vector(Greenfoot.getRandomNumber(360), 6.2), new Color(239, 0, 255)), 1134, 726);
Declaring the variable inside the method call does not help in any way (its value cannot be used later) and it probably would compile anyway.
iCodeAsian iCodeAsian

2015/10/27

#
Oh my god it makes sense! Okay, thank you so much @danpost for dealing with my idiocy and stupidity. You've helped me a lot!
danpost danpost

2015/10/27

#
danpost wrote...
Declaring the variable inside the method call does not help in any way (its value cannot be used later) and it probably would compile anyway.
Correction: "... it probably would not compile anyway."
iCodeAsian iCodeAsian

2015/10/27

#
@danpost Sorry about replying late. School and all, but it did compile?
danpost danpost

2015/10/27

#
iCodeAsian wrote...
@danpost Sorry about replying late. School and all, but it did compile?
Are you saying that it compiled when you used the following:
1
addObject( new Body (10, .0000000000000001, new Vector(int angle = Greenfoot.getRandomNumber(270), 6.2), new Color(239, 0, 255)), 1134, 726);
(with the 'int angle =' part?) EDIT: I tested it and got a ".class expected' error message during compilation.
iCodeAsian iCodeAsian

2015/10/29

#
Err no the one you said or for. addObject( new Body (10, .0000000000000001, new Vector(Greenfoot.getRandomNumber(360), 6.2), new Color(239, 0, 255)), 1134, 726);
You need to login to post a reply.