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

2013/4/2

Help with physics

henrYoda henrYoda

2013/4/2

#
Hi guys so this is my first physics game, and right now I am having a little problem with my player. For some reason he can move left, but not right. Also, there is a delay of a second or two before he starts moving, does anyone know how to fix this? Here is the code for the Player.
import greenfoot.*;

public class player extends Actor
{
    int ySpeed;
    double xSpeed;
    double power = 3.5;
    int angle = 45;
    /**
     * Ball2 Constructor: calculates and sets the initial values for the x and y speeds
     * 
     * @param angle: the initial direction of travel for the ball
     * @param power: the initial speed of the ball in the direction of travel
     */

    /**
     * Method act: calculates and sets new location of the ball and removes it upon reaching the right end of the world
     */
    public void act()
    {
        world gameWorld = (world) getWorld(); 
        Platform platform = gameWorld.getPlatformFriction();
        int newX = getX() + (int) Math.round(xSpeed); // calculates the new x-coordinate value
        int newY = getY() - ySpeed; // calculates the new y-coordinate value 
        setLocation(newX, newY);
        xSpeed += ((double) platform.friction - xSpeed) / 100;
        jump();
        movement();
        reduceYSpeed();
        collisionWithPlatform();
    }

    public void collisionWithPlatform()
    {
        Actor coll = (Platform) getOneObjectAtOffset(0, 30, Platform.class);
        if (coll == null)
        {

            reduceYSpeed();

        }
        else
        {
            ySpeed = 0;
        }
    }

    public void movement()
    {
        world gameWorld = (world) getWorld(); 
        Platform platform = gameWorld.getPlatformFriction();
        if (Greenfoot.isKeyDown("right"))
        {
            xSpeed += ((double) platform.friction - xSpeed) / 50;
        }
        else
        {

            if (!Greenfoot.isKeyDown("left"))
            {
                xSpeed = 0;
            }
        }
        if (Greenfoot.isKeyDown("left"))
        {
            xSpeed += ((double) platform.friction - xSpeed) / -50;
        }

    }

    public void jump()
    {

    }

    public void reduceYSpeed()
    {
        ySpeed--;
    }
}
henrYoda henrYoda

2013/4/2

#
By the way this is based on the flight trajectories physics engine
henrYoda henrYoda

2013/4/2

#
I think the problem is that I didn't use things like cos and sin, but I just don't understand how they work. Could someone (If you can) please correct my physics, using cos and sin, and explain to me what is happening? I really just don't understand physics, what I did above was kinda random
You need to login to post a reply.