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

2016/12/7

Turning by a double

Tritos Tritos

2016/12/7

#
I just have a quick question. Is it in some way possible to turn by degrees that are not whole numbers (i.e. 1,2,3...360). ? Instead I would like to turn in smaller intervals (i.e. 0.1, 0.2, 0.3... 360).
danpost danpost

2016/12/7

#
My QActor class applies smooth moving AND rotation to actors. It can be found here. If you want to apply gravity and drag coefficients as well, my GQActor class, which can be found here, can be used.
Tritos Tritos

2016/12/7

#
Okay, I had a look at both, but they dont really help me. I cant look at the code for QActor class. The Player in my project alrerady turns quite smoothly with this code.
 
public void act() 
    {
       if (Greenfoot.isKeyDown("left"))
        {
            turnLeft();
        }

        if (Greenfoot.isKeyDown("right"))
        {
            turnRight();
        }
}
public void turnLeft()
    {
        turn(-turnSpeed);
    }

    public void turnRight()
    {
        turn(turnSpeed);
    }
but what Im trying to do is making my game "3D". Im doing that by looking at how far the wall in front of me is away from me and then drawing a rectangle acoordingly. This is my code to do that.
public void drawSightV2()
    {
        GreenfootImage drawerImg = getWorld().getObjects(Drawer.class).get(0).getImage();
        drawerImg.clear();
        int distance = 0;
        int xSave = getX();
        int ySave = getY();
        double tileMaxHeight = 800.0;
        int tileWidth = 10;
        int colourD = 4;
        int h1;
        int maxSight = ((1400/tileWidth)/2);

        for(int n = 0; n <= maxSight; n++)
        {
            turn(n);
            while(!isTouching(Wall.class)) 
            {
                distance = distance + 10;
                move(10);
            }

            h1 = (int)(tileMaxHeight*((1400.0-distance)/(1400.0)));
            
            if(h1/colourD > 255 || h1 == 0)
                drawerImg.setColor(new Color(255, 255, 255));
            else
                drawerImg.setColor(new Color(h1/colourD, h1/colourD, h1/colourD));
                
            drawerImg.fillRect(690+(n*tileWidth), 450 - (h1/2), tileWidth, h1);
            
            setLocation(xSave, ySave);           
            distance = 0;
            
            turn(-(2*n));
            while(!isTouching(Wall.class)) 
            {
                distance = distance + 10;
                move(10);
            }

            h1 = (int)(tileMaxHeight*((1400.0-distance)/(1400.0)));
            
            if(h1/colourD > 255 || h1 == 0)
                drawerImg.setColor(new Color(255, 255, 255));
            else
                drawerImg.setColor(new Color(h1/colourD, h1/colourD, h1/colourD));
                
            drawerImg.fillRect(690-(n*tileWidth), 450 - (h1/2), tileWidth, h1);
            
            setLocation(xSave, ySave);            
            distance = 0;
            turn(n);
        }
    }
This already works, but the different walls are hard to differentiate from each other. I am trying to fix that by making the different rectangles not so wide, but then the FOV gets very messed up and very high. Therefor I want to make the steps I turn in smaller. I hope you can understand my problem better now.
danpost danpost

2016/12/7

#
Tritos wrote...
Im trying to do is making my game "3D". Im doing that by looking at how far the wall in front of me is away from me and then drawing a rectangle acoordingly ... the different walls are hard to differentiate from each other. I am trying to fix that by making the different rectangles not so wide, but then the FOV gets very messed up and very high. Therefor I want to make the steps I turn in smaller.
By tracking rotation and location is more precise terms, whether by using double values or by using fractional units instead of whole, you should be able to accomplish what you trying to do. Just do not rely on 'getX', 'getY' and 'getRotation' for the values in your calculations -- use, for example 'getQX', 'getQY' and 'getQR'. You should only need to set 'xSave' and 'ySave' to the values returned by 'getQX' and 'getQY' and use 'setQLocation' to reset the position of the actor. The 'QVAL' field holds the number of sub-units a degree or cell is broken up into. As is, the class divides each cell and degree into one-hundred sub-units. So, for example, 'move(100)' is equivalent to the normal 'move(1)'; and 'turn(100)' is equivalent to the normal 'turn(1)'; and 'turn(1)' is equivalent to the hardly ever used 'turn(1/100)'.
Tritos Tritos

2016/12/7

#
Okay, now I understand... and my game actually kind of works. It just lags extremely. Thank you very much
You need to login to post a reply.