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

2014/11/27

trying to make a car move depending on the rotation

UNDEAD_DC UNDEAD_DC

2014/11/27

#
I am making a game with a car and that car has to drive on a field and u can control the car. when you press the left arrow it turns left with turn(-1); and right turn(1); But my car keeps going to the right horizontaly the picture does change but the direction not untill it reaches a rotation of 30.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tank extends Actor
{
    /**
     * Act - do whatever the Tank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        move(1);
        int x = getX();  
        int y = getY();  
        int ww = getWorld().getWidth();  
        int wh = getWorld().getHeight();  
        if (x%(ww-1) == 0)
        {
            setLocation((1-x/(ww-1))*(ww-3)+1, y);  
        }
            if (y%(wh-1) == 0)
        {
            setLocation(x, (1-y/(wh-1))*(wh-3)+1);
        }
            if (Greenfoot.isKeyDown("left"))
        {
            turn(-1);
        }    
        if (Greenfoot.isKeyDown("right"))
        {
            turn(1);
        }    
        if (Greenfoot.isKeyDown("up"))
        {
            move(5);
        }
        //checkSide();
    }
    public void checkSide()
    {
        if(atWorldEdge()){  
            World world;  
            world = getWorld();  
            world.removeObject(this);
        }  
    }
    public boolean atWorldEdge()    
    {    
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)    
            return true;    
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)    
            return true;    
        else    
            return false;    
    }    
}
anybody can help me how i can let the car drive more precisely? EDIT: Humvee now is called Tank
davmac davmac

2014/11/27

#
Your problem is a variation of this one, I think. The discussion there also applies here.
UNDEAD_DC UNDEAD_DC

2014/11/27

#
thanks for the link couldn't find anything about this and with this link you double helped me cuz im also going to let things shoot:) EDIT: I now have the steering problem fixed and I am facing another problem: when my Tank hits a wall it starts to lag inside the wall teleporting from the left side to the right side and over and over again. how do i fix this? this is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tank extends voertuig

{
    /**
     * Act - do whatever the Tank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private double a, b;
    private int speed = 1;
    public void act() 
    {
        // Add your action code here.
        //move(1);
        int x = getX();  
        int y = getY();  
        int ww = getWorld().getWidth();  
        int wh = getWorld().getHeight();
        if (Greenfoot.isKeyDown("up")) //for moving forward 
            {
                a += Math.cos(Math.toRadians(getRotation()))*speed;
                b += Math.sin(Math.toRadians(getRotation())) * speed;
                setLocation((int)a, (int)b);
            }
        if (x%(ww-1) == 0)
        {
            setLocation((1-x/(ww-1))*(ww-3)+1, y);  
        }
            if (y%(wh-1) == 0)
        {
            setLocation(x, (1-y/(wh-1))*(wh-3)+1);
        }
            if (Greenfoot.isKeyDown("left"))
        {
            turn(-1);
        }    
        if (Greenfoot.isKeyDown("right"))
        {
            turn(1);
        }    
        //if (Greenfoot.isKeyDown("up"))
        //{
        //    move(5);
        //}
        //checkSide();
    }
    public void addedToWorld(World world) { 
        a = getX(); 
        b = getY(); 
    }
    public void checkSide()
    {
        if(atWorldEdge()){  
            World world;  
            world = getWorld();  
            world.removeObject(this);
        }  
    }
    public boolean atWorldEdge()    
    {    
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)    
            return true;    
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)    
            return true;    
        else    
            return false;    
    }    

}
danpost danpost

2014/11/27

#
I do not see any code that checks for walls here. But, it sounds like either the tank is not backed far enough off the wall when contacted or your direction field is not being set properly. I usually use two condition when reversing direction upon reaching a limit (whether the edge of the world or an obstacle). The current direction of movement and the location of the limit together tells the object whether to reverse directions or not. That is, something like this would be for the left edge of the world:
if (direction == WEST && getX() < getImage().getWidth()/2) turn(180);
Without the direction check, the actor could end up wiggling near the edge (turning around and around).
UNDEAD_DC UNDEAD_DC

2014/11/27

#
this part is looking for edge detection
        int ww = getWorld().getWidth();    
        int wh = getWorld().getHeight(); 

if (x%(ww-1) == 0)  
        {  
            setLocation((1-x/(ww-1))*(ww-3)+1, y);    
        }  
            if (y%(wh-1) == 0)  
        {  
and your code wouldnt work because when it hits the wall i want it to disapear and then apear at the other side of the screen
danpost danpost

2014/11/27

#
Oh. You want your tank to wrap around the screen. The phrase you used, "lag inside the wall", particularly the word "wall", threw me off. The edges of the world are not walls -- they are the world boundaries or limits. An actor is used for walls (usually). Anyway, you do not need to ask if the edge is contacted in world wrapping; you can just set the location proper (whether it is or not at the edge). You have 'a' and 'b' for the new location coordinates. You can now just do this:
a = (a+ww)%ww;
b = (b+wh)%wh;
setLocation((int)a, (int)b);
This should be able to replace lines 33 through 40 in you previous post; and you can remove lines 23 and 24 in that post also. Actually, the following can replace lines 27 through 40 (still remove lines 23 and 24):
if (Greenfoot.isKeyDown("up")) //for moving forward 
{
    a += Math.cos(Math.toRadians(getRotation()))*speed;
    b += Math.sin(Math.toRadians(getRotation())) * speed;
    a = (a+ww)%ww;
    b = (b+wh)%wh;
    setLocation((int)a, (int)b);
}
UNDEAD_DC UNDEAD_DC

2014/11/27

#
tthank you SO much for this now it works fine, working on the shooting now trying to use the same piece of code u pasted to make it go in the right direction. thanks for this anyway will continue tomorrow and maybe upload my first version of the game EDIT: when i drive backwards towards the edge of the world it does not go through the edge of the world but is just stops at the edge and when u press forward it pops up in the middle of the screen. this is how my Tank code looks like now:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Tank here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tank extends Actor

{
    /**
     * Act - do whatever the Tank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    private double a, b;
    private int speed = 2;
    public void act() 
    {
        // Add your action code here.
        //move(1);
        int x = getX();  
        int y = getY();  
        int ww = getWorld().getWidth();  
        int wh = getWorld().getHeight();
        if (Greenfoot.isKeyDown("up")|| Greenfoot.isKeyDown("w")) //for moving forward   
        {  
            a += Math.cos(Math.toRadians(getRotation()))*speed;  
            b += Math.sin(Math.toRadians(getRotation())) * speed;  
            a = (a+ww)%ww;  
            b = (b+wh)%wh;  
            setLocation((int)a, (int)b);  
        }  
        if (Greenfoot.isKeyDown("down")|| Greenfoot.isKeyDown("s")) //for moving forward 
            {
                a -= Math.cos(Math.toRadians(getRotation()))*speed;
                b -= Math.sin(Math.toRadians(getRotation())) * speed;
                setLocation((int)a, (int)b);
            }
        if (Greenfoot.isKeyDown("left")|| Greenfoot.isKeyDown("a"))
            {
                turn(-1);
            }    
        if (Greenfoot.isKeyDown("right")|| Greenfoot.isKeyDown("d"))
            {
                turn(1);
            }  
            }
    /*public void addedToWorld(World world) { 
        a = getX(); 
        b = getY(); 
    }
    */
}
EDIT: fixed the backwards driving issue by adding
                a = (a+ww)%ww;  
                b = (b+wh)%wh;
to the "back" onkey event but now when i press the back button it jumps to the left upper corner after the 1st time pressing back and that jump it keeps moving normal even after moving forward steering and then backwards again.
danpost danpost

2014/11/28

#
Oh -- dog-nabit. What I gave above only works in an unbounded world. The following should work for you:
a = 1+(a+(ww-3))%(ww-2);
b = 1+(b+(wh-3))%(wh-2);
I think I did that right.
UNDEAD_DC UNDEAD_DC

2014/11/28

#
now it jumps to the left lower corner when i press forward on start right lower corner when i press back on start i now made it like this:
    private double a = 300;
    private double b = 200;
so when i call a or b its not empty so it wont go to the corner of the screen TY for help
You need to login to post a reply.