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

2013/3/29

are there physics in greenfoot?

RobTheRipper RobTheRipper

2013/3/29

#
i was wondering if there are any built in physics, say like i want a catapult to launch things, or will i have to program every step, manipulate the acceleration, velocity, and direction? if i have to program all the moment if anyone would be nice enough to give me a explanation of how i would do an arced path of an object? like if something is catapulted, how do i calculate how many x cells and y cells to move as he moves across the screen, to form an arc like pathway? im new to this so please explain :P
danpost danpost

2013/3/29

#
There are quite a few scenarios on the site with open source that you can use as example of how to accomplish that. My Flight Trajectories Demo may be of some help.
Gevater_Tod4711 Gevater_Tod4711

2013/3/29

#
Unfortunatly there is no physics engine in greenfoot but this doesn't mean that you have to program everything on your own. There are many physics that someone else has already programmed and you can use them. For example for your catapult you could use this physics:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class Player extends Actor
{
    double exactX;//the exact X location of your object (double instead of int);
    double exactY;//the exact Y location of your object (double instead of int);
    double velX;//the movement (in X direction) of your object;
    double velY;//the movement (in Y direction) of your object;
    
    public void setLocation(int x, int y) {//overwrites the set location method in actor.
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }
    public void setLocation(double x, double y) {//overwrites the set location method in actor.
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }
    
    public double getExactX(){//returns the exact X coordinate of an object.
        return exactX;
    }
    public double getExactY(){//returns the exact Y coordinate of an object.
        return exactY;
    }
}
If you declare your catapult as a subclass of this class called Player there will be some variables and methods that are passed on to your class. For your catapult this might be not that interesting but for the projectil it is. If you want to shoot a projectil with your catapult you just have to create a new projectil (or how ever you want to call it) and tell it which movement it has. The movement if this object are the velX and velY variables that are declared in Player. Then you can easily create a physical correct movement for your projectil like this:
public class Projectil extends Player 
{
    
    public Projectil(double velX, double velY) {
        this.velX = velX;
        this.velY = velY;
    }
    
    public void act() {
        fly();
    }
    
    public void fly() {
        velY -= CatapultWorld.GRAVITY;//If your worldsubclass is called somehow else you have to change this line; Instead of CatapultWorld use the name of your World class;
        setLocation(getExactX() + velX, getExactY() - velY);
    }
}


public class CatapultWorld extends World
{
    //here you need to declare a static final variable called GRAVITY to simulate the worlds graviatation:
    public static final double GRAVITY = 0.1;//also any other value is possible;
}
If you create a new projectil you have to give it the values for the movement in x and y direction. Then the projectil is able to fly. Using this you can make a catapult that throws some projectils with a real physik.
You need to login to post a reply.