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

2019/1/23

Help with collision system

TheSlorrow44 TheSlorrow44

2019/1/23

#
I'm making a bullet hell game , so I need to add simultaneously dozens of copies of the same bullet . I've chosen to make a 'base' bullet , than to clone it and add those clones in the world. I use this code to create a clone, which I've found in a discussions :
  public Object clone()
    {
        try{ Actor clone=(Actor)super.clone();
             return clone;}
        catch (CloneNotSupportedException cnse) // must be caught
        {
            System.out.println("Clone not supported");return null;
        }
         
    }
Those clones move as they should , but they don't interact with the player's hitbox Hitbox code :
public boolean checkHitbox()
    {
       Enemy_Bullet bullet = (Enemy_Bullet)getOneIntersectingObject(Enemy_Bullet.class);
       ENEMY enemy = (ENEMY)getOneIntersectingObject(ENEMY.class);
       if((bullet != null && getObjectsInRange(13,Enemy_Bullet.class).contains(bullet) 
          && bullet.checkHitSomething())  || 
          (enemy != null && getObjectsInRange(13,ENEMY.class).contains(enemy) 
          && enemy.checkHitAvatar()))
       {
               invulnerability = 150;
               bullet.setType(0);bullet.setImage();
               if(invulnerability==0)return true;
       }
       if(invulnerability!=0)
       {

          invulnerability--; 
          
        }
       return false; 
    }
protected boolean checkHitSomething()
    {
        Hitbox hitbox = (Hitbox)getOneIntersectingObject(Hitbox.class);
        switch(type)
        {
            case 0:if(getNeighbours(15,false,Hitbox.class).contains(hitbox)) return true;break;
            case 1:if(getObjectsInRange(15,Hitbox.class).contains(hitbox)) return true;break;
            case 2:if(getOneIntersectingObject(Hitbox.class)==hitbox) return true;break;
            case 3:if(getObjectsInRange(8,Hitbox.class).contains(hitbox)) return true;break;
            case 4:if(getObjectsInRange(28,Hitbox.class).contains(hitbox)) return true;break;
            case 5:if(getObjectsInRange(50,Hitbox.class).contains(hitbox)) return true;break;
        }
        return false;
    }
If I use bullets wich aren't clones everything works fine . Any tips?
TheSlorrow44 TheSlorrow44

2019/1/25

#
Ok...no reply . I didn't explain clearly , did I?
danpost danpost

2019/1/25

#
TheSlorrow44 wrote...
Ok...no reply . I didn't explain clearly , did I?
I think you did explain okay. If you are having trouble with clones, don't use them. Just create multiple bullets. No code help can be given as the code where bullets are called to be created is not given.
TheSlorrow44 TheSlorrow44

2019/1/25

#
danpost wrote...
TheSlorrow44 wrote...
Ok...no reply . I didn't explain clearly , did I?
I think you did explain okay. If you are having trouble with clones, don't use them. Just create multiple bullets. No code help can be given as the code where bullets are called to be created is not given.
I can't create multiple bullets because : 1. I have 4 types of bullet , and I think that will be more (they differ by how they move) 2. A single bullet needs a lot of parameters 3.I need different enemies which use the same pattern and bullet. That's why I thought that clones would be a good idea. That's how I make an enemy : code in MyWorld :(this adds 5 enemy which use the same pattern and bullet)
NonEasing_Bullet bul1 = new NonEasing_Bullet(2,0,5);
    double[] gen_var = {5,10,90,45,2,10};
    NonEasing_Generator gen1 = new NonEasing_Generator(gen_var);
    Easing_Enemy en1 = new Easing_Enemy(10000,1,1,200,100,gen1,400,300,600,1,1);

gen1.setBullet(bul1);
for(int i=1; i<=5; i++)
           addObject(new Easing_Enemy(10,1,1,i*50,0,gen1,i*100,i*100,600,1,1),i*100,-100);
code in BULLET_GENERATOR.class (is a abstract class for NonEasing_Generator.class):
protected void prepareBullet()
    {
        for(int i=0; i<=array; i++)
            cloneBullet[i]=(Enemy_Bullet)bullet.clone();
    }

protected void addBullet()
    {
        double x=0;
        if(array==1) angleArray=angle;
        else angleArray=angle/(array-1);
        for(int i=1; i<=array; i++)
        {
            cloneBullet[i].setRotation(getRotation()+(int)(x));
            getWorld().addObject(cloneBullet[i],getX(),getY());
            x+=angleArray;
        }
    }
In act method I use fisrt prepareBullet() and then addBullet(); ENEMY code (also abstract class for Easing_Enemy.class):
protected void setClone()
    {
         if(bulletGenerator!=null)
            clone_bulletGenerator= (BULLET_GENERATOR)bulletGenerator.clone();    
    }
And then an ENEMY adds the "clone_bulletGenerator" which adds basically "cloneBullet". Those bullet clones appear.They move as they should, but "getOneIntersectingObject" method doesn't work with them. But I think that "getObjects" works.
danpost danpost

2019/1/25

#
TheSlorrow44 wrote...
I can't create multiple bullets because : 1. I have 4 types of bullet , and I think that will be more (they differ by how they move) 2. A single bullet needs a lot of parameters 3.I need different enemies which use the same pattern and bullet.
(1) Okay, probably 4 subclasses of a (non-abstract) Bullet class; (2) No problem, what are the parameters for? (in detail, please) (3) "pattern"? elaborate, please.
TheSlorrow44 TheSlorrow44

2019/1/25

#
danpost wrote...
TheSlorrow44 wrote...
I can't create multiple bullets because : 1. I have 4 types of bullet , and I think that will be more (they differ by how they move) 2. A single bullet needs a lot of parameters 3.I need different enemies which use the same pattern and bullet.
(1) Okay, probably 4 subclasses of a (non-abstract) Bullet class; (2) No problem, what are the parameters for? (in detail, please) (3) "pattern"? elaborate, please.
1.BULLET class is abstract and has 2 subclass : Avatar_Bullet(also abstract) and Enemy_Bullet(also abstract).Enemy_Bullet has 4 subclass : Easing_Bullet,NonEasing_Bullet,Lissajous_Bullet,Accelerated_Bullet 2.a NonEasing_Bullet has all parameters from BULLET and Enemy_Bullet class ( speed, type,color,state of homing , etc) a Easing_Bullet has parameters for targetX,targetY,duration(number of moves),counter,etc. a Lissajous_Bullet it is a little weird. It has parameters for period,maxPeriod, coordinates , etc; a Accelerated_Bullet has parameters for acceleration , minSpeed,maxSpeed,timer; 3When I say pattern i reffer at how bullets are added to the World: how the Bullet_Generator moves + how the bullet moves. For example , a bullet_generator add 6 bullets , each one towards a point of a hexagon. Those bullets can move straight( patern1) or using a function(patern 2).Between patern1 and patern2 differs the type of bullet but the bullet_generator is the same.
danpost danpost

2019/1/25

#
You have the 4 different subclasses. You can create as many objects from each class as you need without having to clone any. Maybe you should show attempted non-cloning code and explain what issues you might have with it.
TheSlorrow44 TheSlorrow44

2019/1/25

#
danpost wrote...
You have the 4 different subclasses. You can create as many objects from each class as you need without having to clone any. Maybe you should show attempted non-cloning code and explain what issues you might have with it.
Yes , I can create as many objects I want . But I clone a bullet because it's easier regarding passing parameters. I create the bullet I need , and then I pass it as a parameter for bullet_generator . I used to use non-cloning code , but I ended up being lost in the number of parameters and my code started become messy.
TheSlorrow44 TheSlorrow44

2019/1/25

#
danpost wrote...
You have the 4 different subclasses. You can create as many objects from each class as you need without having to clone any. Maybe you should show attempted non-cloning code and explain what issues you might have with it.
But if I make a copy method , can it affect the execution time ? Parameters needed for each bullet : -NonEasing_Buulet : 11 -Easing_Bullet : 20 _Lissajous_Bullet : 20 -Accelerated_Bullet : 16 Can the number of parameters affect the execution time ? My project is also big , and I'm already afraid for an upcoming slow execution.
danpost danpost

2019/1/25

#
A parameter list is handled just like is sounds, as a list. So, it should not really affect execution time. It might be better to send the parameters to the generator and have the bullets created from there.
TheSlorrow44 TheSlorrow44

2019/1/25

#
danpost wrote...
A parameter list is handled just like is sounds, as a list. So, it should not really affect execution time. It might be better to send the parameters to the generator and have the bullets created from there.
Just curiosity : Why doesn't "getOneIntersectingObject" method work with clones?
danpost danpost

2019/1/25

#
TheSlorrow44 wrote...
Just curiosity : Why doesn't "getOneIntersectingObject" method work with clones?
I cannot, without in-depth investigating, say why -- and even then, I may not be able to say why.
nccb nccb

2019/1/26

#
As I understand it, clone() copies all object fields exactly. But Actor keeps an internal identifier number, so clone() copies that identifier. Using clone() is almost certain to mess Greenfoot up, as it assumes each actor has a unique identifier. Instead, write a copy-constructor that copies the state you want.
You need to login to post a reply.