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/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(100)+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)-Wind);
            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);
    }
}
I put it liked this but it didnt work. I want the wind power applied when i shot the cannon in the air ,but this code did it at the beginning of the cannon
danpost danpost

2012/11/9

#
I now realize that not only did I mislead you, but you also, did not follow what I said. Let us start over. Wind does not act in the x direction like gravity acts in the y direction. Gravity adds a constant change in the velocity, where wind does not. The change in the x direction is determined by the difference between the speed of the object in the x direction and the speed of the wind; and you probably only want to add a portion of the difference. Let us say: one-tenth of the difference, wind-speed minus object-speed. Since the result each cycle varies, you will need another 'double' field to track the total change in distance due to wind and add that plus the new change in distance to 'xS' each act cycle.
Tezuka Tezuka

2012/11/9

#
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 CannonBall extends Actor
{
    int GRAVITY = 2;
    int Wind = Greenfoot.getRandomNumber(4);
    int xSpeed;
    int ySpeed;
    boolean atEdge;
    /**
     * 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);
        ySpeed = ySpeed - GRAVITY;
        xSpeed = xSpeed- Wind;
        atEdge(); 
    } 

    public CannonBall(int angle, int power)
    {
        xSpeed = (int) ( power * Math.cos(Math.toRadians(angle)));
        ySpeed = (int) (power * Math.sin(Math.toRadians(angle)));
    }

    public boolean atEdge()
    {
        World myWorld = getWorld();
        if( getX() == myWorld.getWidth()-1)
        {
            myWorld.removeObject(this);
            return true;
        }
        if (getY() == myWorld.getHeight()-1)
        {
            myWorld.removeObject(this);
            return true;
        }

        else
        {
            return false;
        }
    }
}
I changed my code a bit can I add the wind like this code?
danpost danpost

2012/11/10

#
That will not be appear realistic. Like I said, wind does not act like gravity. The change in speed of the cannonball due to the wind is related to the difference in the speeds of the wind and the cannonball. The way you coded it, the cannonball will increase speed up to and beyond the speed of the wind (in the direction of the wind).
Tezuka Tezuka

2012/11/10

#
Iam not sure if i get that could u give me an expamle?
danpost danpost

2012/11/10

#
I added wind to my Flight Trajectories Demo. It is not yet implemented within the 'Distance method' calculations (I will add it shortly). Select the 'Speed method' by clicking the method switch and the wind bar will appear; adjust as desired. Send a few balls off and look at the way it changes the trajectories. Then, take a look at the code. Notice how the change is related to the difference in the speeds of the wind and the object.
Tezuka Tezuka

2012/11/10

#
thhx
You need to login to post a reply.
1
2