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

2013/2/17

hanldeOrbCollision

CHERNANDEZ95 CHERNANDEZ95

2013/2/17

#
Can someone kindly look at my code and tell me what I have done wrong? When I compile the program, two of the Orbs will at times stick to the roof or the floor if they start out there. I am just starting out and would really appreciate any help. The Assignment: Create a method called handleOrbCollision(). It should store the intersecting Orb identified in Step 1(c) above in a temporary variable called collider. If a collision has occurred, the Velocity of the collider object should be assigned to a placeholder Velocity object. Use the methods you wrote in Steps 1(a) and 1(b) to exchange the Velocity objects associated with the colliding Orbs. Code: public class Orb extends Actor { private Velocity vel; private int exactX; private int exactY; private int xSpeed; private int ySpeed; private GreenfootImage getImage; private Orb getIntersectingOrb; private Orb collider; public Orb(int xSpeed,int ySpeed) { vel = new Velocity(xSpeed, ySpeed); vel = vel; getImage=new GreenfootImage("gold-ball.png"); } public void act() { move(); turnAtFloor(); turnAtLeftWall(); turnAtRoof(); turnAtRightWall(); } public void addOrbs() { List <Orb> orbs = getWorld().getObjects(Orb.class); for(Orb orb : orbs) { setLocation(getX(), getY()); } } public void move() { exactX = exactX + vel.getXSpeed(); exactY = exactY + vel.getYSpeed(); setLocation( (int) exactX, (int) exactY ); } public void turnAtFloor() { if (getY() + getImage().getHeight()/2 >= getWorld().getHeight()) { Greenfoot.playSound("tock.wav"); vel.reverseY(); } } public void turnAtLeftWall() { if(getX() - getImage().getWidth()/2 <= 0) { Greenfoot.playSound("tock.wav"); vel.reverseX(); } } public void turnAtRoof() { if (getY() - getImage().getHeight()/2 <= 0) { Greenfoot.playSound("tock.wav"); vel.reverseY(); } } public void turnAtRightWall() { if(getX()+ getImage().getWidth()/2 >= getWorld().getWidth()) { Greenfoot.playSound("tock.wav"); vel.reverseX(); } } public void addedToWorld(World world) { exactX = getX(); exactY = getY(); } public int getXSpeed() { return xSpeed; } public int getYSpeed() { return ySpeed; } public Velocity getVelocity() { return vel; } public void setVelocity(Velocity newvel) { vel = newvel; } private Orb getIntersectingOrb() { return (Orb) getOneIntersectingObject(Orb.class); } public void handleOrbCollision() { collider = getIntersectingOrb; if (collider == getIntersectingOrb) { vel.reverseY(); } else if(collider == getIntersectingOrb) { vel.reverseX(); } }
danpost danpost

2013/2/17

#
If the problem is due to the initial placement of the Orb objects then maybe you should show your world class code that places those object in the world. Chances are, however, that you are using something like:
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(new Orb(), x, y);
This allows the orbs to be placed outside the turn-around point along the edges causing them to just wiggle in place. By making the allowable area of placement smaller, we can avoid placing them in these wiggle zones.
int x = 50 + Greenfoot.getRandomNumber(getWidth() - 100);
int y = 50 + Greenfoot.getRandomNumber(getHeight() - 100);
This gives a buffer on each edge of 50 pixels. This value can be adjusted depending on the size of your largest orb. The 50 would refer to half the orb's diameter plus two. The 100 is twice this amount.
CHERNANDEZ95 CHERNANDEZ95

2013/2/17

#
Hi danpost. This is what I have for the OrbWorld public class OrbWorld extends World { private int xSpeed; private int ySpeed; public OrbWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); randomOrbs(); } public void randomOrbs() { int population = 5; for(int i = 0; i < population; i++) { xSpeed = Greenfoot.getRandomNumber(10); ySpeed = Greenfoot.getRandomNumber(10); addObject(new Orb(xSpeed, ySpeed), Greenfoot.getRandomNumber (550), Greenfoot.getRandomNumber (250)); } } }
CHERNANDEZ95 CHERNANDEZ95

2013/2/17

#
Ok, I see what you did. I am using almost the entire world, but just instructing the computer to select random positions, which could also be close to the walls and cause problems. Thank you again for explaining.
danpost danpost

2013/2/17

#
Using 550 and 250 avoids placing the orbs on the right and bottom wiggle zones, but does nothing to prevent them from being placed in the top or left wiggle zones. Replace your random number parameters in the last line with the parameters in the code I provided.
CHERNANDEZ95 CHERNANDEZ95

2013/2/17

#
It worked like a charm. I see it is better to account for the pixels the image uses to determine where to place it. Where could I find out the size of any image that Greenfoot uses. I know that the size of the piano keys in chapter 5 were given in the book; but, where can I find that out say if I wanted to use some other image?
CHERNANDEZ95 CHERNANDEZ95

2013/2/17

#
Ok, I think I found the answer to my question. When creating a new subclass, I clicked on settings option and edit after selecting the image I wanted. Then a window with the image popped up and at the lower left hand corner the pixels were given for the object. Is this correct?
danpost danpost

2013/2/17

#
If you know the filename that holds the image (call it 'imageName.png'):
GreenfootImage image = new GreenfootImage("imageName.png");
int imageX = image.getWidth();
int imageY = image.getHeight();
If you are creating random-sized orbs, you could use (in the world class)
Orb orb = new Orb();
int orbWidth = orb.getImage().getWidth();
int orbHeight = orb.getImage().getHeight();
int x = (2+orbWidth/2) + Greenfoot.getRandomNumber(getWidth()-(4+orbWidth));
int y = (2+orbHeight/2) + Greenfoot.getRandomNumber(getHeight()-(4+orbHeight));
addObject(orb, x, y);
danpost danpost

2013/2/17

#
CHERNANDEZ95 wrote...
Ok, I think I found the answer to my question. When creating a new subclass, I clicked on settings option and edit after selecting the image I wanted. Then a window with the image popped up and at the lower left hand corner the pixels were given for the object. Is this correct?
Hard-coding numbers in a program can become problematic; if you decide to scale (re-size) the image later, you would have to find all occurances of those hard-coded value and change each one. Using the proper methods to acquire the current state is much better programming.
CHERNANDEZ95 CHERNANDEZ95

2013/2/17

#
Ok, I see what you are saying. Makes sense. Again, thank you for your patience and sharing your knowledge with me.
You need to login to post a reply.