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

2021/5/18

Ray casting

1
2
3
Roshan123 Roshan123

2021/5/18

#
 
import greenfoot.*;
 
public class MiniRed extends Actor
 
{
 
    private int charge = 0;
 
    int removeTimer=0,count=0;
 
    Ray ray=new Ray();
 
    private int shotTime;
 
    public MiniRed(int speed, int rate)
 
    {
 
        charge = rate;
 
        getImage().scale(25, 25);
 
        ray.setImage(new GreenfootImage(1,1));
 
    }
 
    protected void addedToWorld(World world)
 
    {
 
      world.addObject(ray, 0,0);
 
    }
 
    public void act() 
 
    {
 
      MyWorld world1 = (MyWorld) getWorld();
 
      if(getWorld()!=null && getWorld().getObjects(B.class).size()>0)
 
      {
 
         B b = getWorld().getObjects(B.class).get(0);
 
         setLocation(b.getX(), b.getY()+75);
 
      }
 
      
 
      if(isTouching(MiniRed.class))
 
      setLocation(getX()-40, getY());
 
      
 
      if(isTouching(Stone.class) || isTouching(EnemyTank.class) || getWorld().getObjects(B.class).isEmpty() || isTouching(Crate.class))
 
      {
 
            getImage().setTransparency(0);
 
      }
 
      else if(!isTouching(EnemyTank.class) || !isTouching(Crate.class) 
 
      || !isTouching(Stone.class) || !getWorld().getObjects(B.class).isEmpty()) 
 
      getImage().setTransparency(255);
 
      
 
      shotTime=(shotTime+1)%40;
 
      if(shotTime!=0)
 
      return;
 
      
 
      if(getWorld()!=null && getWorld().getObjects(EnemyTank.class).size()>0  && getWorld().getObjects(B.class).size()>0)
 
      {
 
        Actor bot=(Actor)getWorld().getObjects(EnemyTank.class).get(0);
 
        B bb = getWorld().getObjects(B.class).get(0);
 
        if(ray.isUnobstructed(this,bot))
 
        {
 
          turnTowards(bot.getX(), bot.getY());
 
          Projectile1 projectile1=new Projectile1();
 
          if(!getWorld().getObjects(B.class).isEmpty()) 
 
          getWorld().addObject(projectile1, getX(),getY());
 
          projectile1.getImage().scale(17, 7);
 
          projectile1.setRotation(getRotation());
 
          /**
 
           * it doesn't work. it should turn toward the blue bot and shoot it unless 
 
           * and untill thier is no obstacle infront of it
 
           * it only works properly when blue bot is not in world
 
           */
 
        }else setRotation(bb.getRotation());//setRotation is not working(even if the bot is not in the world)...it should rotate as same as B class when their is no obstacle infornt of it
 
        //return;
 
      }
 
      ray.setRotation(0);
 
      ray.setLocation(0, 0);
 
    }
 
    public void Remove()
 
    {
 
      removeTimer++;
 
      if(removeTimer>380 && removeTimer<500)
 
      count++;
 
      if(count<10 && count>0)
 
      getImage().setTransparency(255);
 
      else if(count>10 && count<20)
 
      getImage().setTransparency(0);
 
      else if(count>20) count=0;
 
      
 
      if(removeTimer>500 || isTouching(MiniRed.class))
 
      getWorld().removeObject(this);
 
    }
 
    private class Ray extends Actor
 
    {
 
     public boolean isUnobstructed(Actor actorA,Actor actorB)
 
     {
 
        int ax=actorA.getX();
 
        int ay=actorA.getY();
 
        int bx=actorB.getX();
 
        int by=actorB.getY();
 
        int length=(int)Math.hypot(by-ay, bx-ax);
 
        setImage(new GreenfootImage(length,8));
 
        setLocation(ax,ay);
 
        turnTowards(bx,by);
 
        move(length/2);
 
        return getIntersectingObjects(Obstacle.class).size()==0;
 
     }
 
    }
 
}
 
Roshan123 Roshan123

2021/5/18

#
Plz download the scenario frm here and replace the above code with MiniRed class i request u to read from line number 107 to 117
danpost danpost

2021/5/18

#
Remove line 99.
Roshan123 Roshan123

2021/5/18

#
getIntersectingObjects(Obstacle.class).size()<=2;
Now its working aftr replacing this line with 183 and rectifying a little mistake
Roshan123 Roshan123

2021/5/19

#
Plz tell me the use of implements I don't know where u use it
danpost danpost

2021/5/19

#
Roshan123 wrote...
Plz tell me the use of implements I don't know where u use it
My use of it was just to add another kind of "type" to the obstacles. So, for example, a wall will not only be of type Wall, but also of type Obstacle. Then, all obstacle, regardless of class type, can be referred to as Obstacle type objects. For information on full use of an interface, please refer to here in the java tutorials.
Roshan123 Roshan123

2021/5/20

#
After how many hours will u be free I will be giving u the whole source code of the scenario If u download it then plz reply anything
Roshan123 Roshan123

2021/5/20

#
Roshan123 Roshan123

2021/5/20

#
Plz notify me
danpost danpost

2021/5/20

#
Roshan123 wrote...
Plz notify me
Got it. Done.
Roshan123 Roshan123

2021/5/20

#
Roshan123 wrote...
Hi
I hope u downloaded from here I did something like that inorder to overcome users from downloading it. plz note that i m not able to use ray class properly I want the blue EnemyTank to move towards the crate when their path is unobstructed. Its works but if the crate is again added to world then it moves horribly{note that when the crate is again added to world; the path between the blue bot(enemyTank or enemyTank1. I don't remember well) and the crate should be obstructed}......click solo to observe and rectify crate mechanism And same for shooting mechanism. They(EnemyCannon and EnemyCannon1) shoot main player(B and Tank class) even if the path between them is obstructed.......click duo to observe and rectify shooting mechanism
Roshan123 Roshan123

2021/5/20

#
1 doubt Let us take that i created 2 classes i.e. Circle class and Circle 2 class Circle doesn't implement any other class and it moves in a circular path And Circle2 class implements circle class Will Circle2 act as same as Circle class?
danpost danpost

2021/5/20

#
Roshan123 wrote...
Circle doesn't implement any other class and it moves in a circular path And Circle2 class implements circle class Will Circle2 act as same as Circle class?
I believe you mean one class does not extend another, which would (if one does) make it act the same (provided action is not overridden).
Roshan123 Roshan123

2021/5/21

#
Does the Ray mechanism needs a surplus amount of changes in program? Plz notify me if u r chaging the whole code of the scenario. It would be hard for me to bring further future updates. I hope u will not make changes in lot many classes. I m not as talented as u r, so it would be hard for me to understand for days or years
danpost danpost

2021/5/21

#
Roshan123 wrote...
Does the Ray mechanism needs a lot of changes? Plz notify me if u r chaging the whole code of the scenario. It would be hard for me to bring further future updates. I hope u will not make changes in lot many classes
It is going to take time to sort things out. The code is quite "bulky" and there is a lot to go over. I having changed anything yet with respect to the issues. Still assembling data in my head. The Ray mechanism, itself, is fine (at least, I believe so).
There are more replies on the next page.
1
2
3