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

2014/4/10

Reflecting off of objects(bounce)

Repsidy Repsidy

2014/4/10

#
I am currently attempting to work on a shield for an Asteroids game. The shield is supposed to reflect or "bounce" the Asteroids off it for a set period of acts. I have the code for the time and creation but *even after a few hours of looking at examples* that I cannot figure out how to write the code in the Asteroids class for it to bounce off the shield when it is hit. The Asteroids are a SmoothMover subclass as well as it is has a Vector class. Any help would be appreciated! Heres what I currently have for code
1
2
3
4
5
6
7
public void checkCollision()
    {
        List<Asteroid> a = (List<Asteroid>) getOneIntersectingObject(Shield.class);
        if (a != null) {
           //The Asteroids will bounce off the shield  when it intersects
        }
    }
I have tried another version of code for this but was soon stuck
1
2
3
4
5
6
7
public void checkCollision()
    {
        Asteroid a = (Asteroid) getOneIntersectingObject(Shield.class);
        if (a != null) {
           //The Asteroids will bounce off the shield  when it intersects
        }
    }
I tried using the same type of code as It would be for bullets hitting the asteroid but became stuck when I could not figure out how to change the direction of the asteroid.
Repsidy Repsidy

2014/4/10

#
I tried another way and started receiving this error: java.lang.ClassCastException: Shield cannot be cast to Asteroid at Asteroid.checkCollision(Asteroid.java:113) at Asteroid.act(Asteroid.java:35) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) And the changes I made to my checkCollision
1
2
3
4
5
6
7
8
9
10
11
public void checkCollision()
{
     
    Asteroid a = (Asteroid) getOneIntersectingObject(Shield.class);
    if (a != null) {
       //The Asteroids will bounce off the shield  when it intersects
       stop();
       setRotation(360-getRotation());
       move(getRotation());
    }
}
danpost danpost

2014/4/10

#
The ClassCastException is due to line 4 in your latest 'checkCollision' method. You are getting an intersecting Shield object and trying to cast it as an Asteroid object. Since this is in your Asteroid class, you will be looking for Shield objects that need to be cast to Shield type (actually, since 'getOneIntersectingObject' returns an object typed as an Actor, and you do not need to access any fields or methods in the Shield class here, typecasting is not necessary. All you need are the methods of the Actor class. Line 8 will only change the direction of the asteroid correctly if the shield surface was horizontal and flat. What you may need to do is get the angle of the surface of the shield at the point of impact and calculate the reflective angle from that. It is something like '540+2*shieldAngle-AsteroidRotation' where shieldAngle is the angle from its focal point to the point of contact (or the angle of the shield surface perpendicular at the point of contact). Line 9 just looks ridiculous. Speed determined by angle (strange!).
Repsidy Repsidy

2014/4/14

#
Line 9 was just a test to see if the objects were actually intersecting *as you pointed out and as I found out they were not* I found a different way of working with it though. Though this way is not point sensitive, it did what i needed it to do.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void checkCollision()
{
     
    Shield a = (Shield) getOneIntersectingObject(Shield.class);
    if (a != null) {
       //The asteroids hit the shield and bounce
      int r = getMovement().getDirection() + 180;
      double l = getMovement().getLength();
      Vector speed1 = new Vector(r, l * 2 );
      addForce(speed1);
    
       
    }
}
This is where I figured it out.
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
private void breakUp()
    {
        Greenfoot.playSound("Explosion.wav");
        Space space = (Space) getWorld();
        space.addScore(10);
        if(size <= 16)
        {
            getWorld().removeObject(this);
            space.AsteroidsDestroyed++;
            if (space.AsteroidsDestroyed == space.startAsteroids * 4)
            {
                 Greenfoot.playSound("levelup.wav");
                space.startAsteroids++;
                space.addAsteroids(space.startAsteroids);
                space.AsteroidsDestroyed = 0;
                space.Level();
                
            }
        }
        else
        {
            int r = getMovement().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getMovement().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);       
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());       
            a1.move();
            a2.move();
         
            getWorld().removeObject(this);
        }
ANyways. Thanks for the help!
You need to login to post a reply.