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

2012/11/3

Help with trajectory

1
2
Tezuka Tezuka

2012/11/3

#
Hi hi need help wih firing the cannon using algorithm. here is my code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Cannon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Cannon extends Actor
{
    int power = 0;
    int angle = 0;
    private StatusBoard statusboard;
    /**
     * Act - do whatever the Cannon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Cannon( StatusBoard status)
    {
        statusboard = status;
        
    }
    
    public void act() 
    {
        if( Greenfoot.isKeyDown("up"))
        {
            power = power + 1;
        }
        
        if( Greenfoot.isKeyDown("down "))
        {
            power = power - 1;
        }
        
        if( Greenfoot.isKeyDown("right") && angle > 0)
        {
            angle = angle - 1;
        }
        
        if( Greenfoot.isKeyDown("left") && angle < 90)
        {
            angle = angle + 1;
        }
        
        if("space".equals(Greenfoot.getKey()))
        {
            fire();
        }
        setRotation(-angle);
        statusboard.setValues(angle,power);
        
    }    
    
    public void fire()
    {
        World myWorld = getWorld();
        Chicken chicken = new Chicken(angle,power);
        myWorld.addObject(chicken, getX(),getY());
    }
}
 cannon class
Gevater_Tod4711 Gevater_Tod4711

2012/11/3

#
I don't get where is your problem. What exactly do you need?
SPower SPower

2012/11/3

#
You're not really clear what you want, first explain further.
danpost danpost

2012/11/3

#
Also, the code for the trajectory should be in the Chicken class, which is not supplied.
Tezuka Tezuka

2012/11/3

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Chicken here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Chicken extends Actor
{
    int GRAVITY = 2;
    int xSpeed = 20;
    int ySpeed = 0;
    /**
     * Act - do whatever the Chicken wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX()+ xSpeed, getY() -ySpeed);
        
    } 
    
    public Chicken(int angle, int power)
    {
        
    }
}
sorry here is my chicken class. My question is how can i make so the chicken can fly like a curve.
danpost danpost

2012/11/3

#
You want gravity to be applied to the object as it flies through the air. Gravity effects the speed of the movement in the y direction. Just add GRAVITY to ySpeed each act cycle.
Tezuka Tezuka

2012/11/3

#
I must use an algorithm
SPower SPower

2012/11/3

#
Why? And what do you mean exactly with 'I need to use an algorithm'?
Tezuka Tezuka

2012/11/3

#
This is my task 1. You must have the following actors: cannon, cannon ball and some target. 2. The trajectory of the cannon ball must be calculated accordingly to some algorithm taking the gravity into consideration. 3. In the program we shall be able to set the starting velocity and the initial angle of the cannon ball. 4. A message shall tell the user if the target was hit or not.
Gevater_Tod4711 Gevater_Tod4711

2012/11/3

#
You could calculate the coordinates you have to move to in a method and write them down in an array. You could use the way danpost tould you. You just writh it down in an array (if this is what you need). Otherwhile maybe this scenario by Busch2207 could help you.
Tezuka Tezuka

2012/11/4

#
i looked at it and didnt understand, but I found a method called Math.toRadians(angle). How does this works?
SPower SPower

2012/11/4

#
Something like this:
public static double toRadians(double degrees)
{
     return degrees *(Math.PI /180);
}
it converts degrees into radians.
SPower SPower

2012/11/4

#
@Tezuka next time you don't understand a method, look it up in the documentation :)
Tezuka Tezuka

2012/11/8

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball extends Actor
{
    private double h0 = 1;
    private double h = h0;
    private double tStart;
    private double xStart;
    private double yStart;
    private double g = 9.82;
    private double SCALE = 3;

    private double v0y;
    private double v0x;

    private boolean first = true;
    private boolean atEdge;
    
    private int Wind = Greenfoot.getRandomNumber(50)+1;

    /**
     * v0x = v0 * cos(a)
     * v0y = v0 * sin(a)
     * x= v0 * t * cos(a) = v0x * t
     * y = v0 * t * sin(a) - 1/2gt^2 = v0y *t - 1/2gt^2 + h0
     * 
     */

    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Ball(int angle, int power)
    {
           v0y = power * Math.sin(Math.toRadians(angle));
           v0x = power * Math.cos(Math.toRadians(angle));
           setRotation(-angle);
    }

    public void act() 
    { 
       move();
       atEdge();
    }  

    public void move()
    {

        if(first)
        {
            first = !first;
            tStart = System.currentTimeMillis();
            xStart = getX();
            yStart = getY();

        }
        else 
        {
            double t = (System.currentTimeMillis()- tStart)/ 100;
            h = h0 + (v0y * t) - (g*t*t)/2;
            int xS = (int)(xStart + SCALE * (v0x * t));
            int yS = (int)(yStart - SCALE * h);
            setLocation(xS, yS);
        }
    }
    
    public boolean atEdge()
    {
        World ground = getWorld();
        if( getX() ==  ground.getWidth()- 1)
        {
            ground.removeObject(this);
            return true;
        }
        if( getY() ==  ground.getHeight()- 1)
        {
            ground.removeObject(this);
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public void addedToWorld(World world)
    {
        move(130);
    }
}
Hi I want to add wind force to my code how can I do it?
danpost danpost

2012/11/8

#
Wind would change the speed in the x direction just as gravity changes the speed in the y direction. Depending upon your intentions, the wind could be constant or variable. If constant, then code it for the x direction like gravity is coded for the y direction. If variable, then the coding is a bit more involved; but, I do not believe your scenario will be using variable wind (where the wind changes during the flight) of the object.
There are more replies on the next page.
1
2