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

2013/7/23

Physics Engine - Confusion With getY/X() via Multiple Objects

1
2
danpost danpost

2013/7/29

#
I modified my Cube class (made the 'left/right' movement more precise when intersecting walls:
import greenfoot.*;
import java.awt.Color;

public class Cube extends Actor
{
    private int gravity;

    public Cube()
    {
        GreenfootImage image = new GreenfootImage(49, 49);
        image.setColor(Color.gray);
        image.fill();
        setImage(image);
    }

    public void act()
    {
        // gravity and jump
        gravity += 2; // add gravity
        turn(90); // face 'down'
        move(gravity); // fall faster (or rise slower)
        boolean hitWall = false;
        while (getOneIntersectingObject(Wall.class) != null)
        { // move off any intersecting wall
            hitWall = true;
            move(-(int)Math.signum(gravity));
        }
        if (hitWall)
        { // add buffer space and stop fall (or rise)
            move(-(int)Math.signum(gravity));
            gravity = 0;
            // jump on command
            if (Greenfoot.isKeyDown("w") || Greenfoot.isKeyDown("up")) gravity -= 30;
        }
        turn(-90); // re-face 'forward'
        // left and right
        int d = 0; // to hold direction input by user
        if (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("left")) d--;
        if (Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("right")) d++;
        if (d != 0)
        {
            move(4*d); // move in given direction
            hitWall = false;
            while (getOneIntersectingObject(Wall.class) != null)
            { // move off any intersecting wall
                hitWall = true;
                move(-d);
            }
            if (hitWall)
            { // turn and add buffer space from side wall
                turn(-90*d); // new gravity direction
                move(d); // add buffer from old ground wall
                gravity = 0;
            }
        }
    }
}
8bitcarrotjuice 8bitcarrotjuice

2013/7/29

#
Hey Dan, is there a way to apply 'while (getOneIntersectingObject(Wall.class) != null)' but make it detect when the object is not intersecting it, something like this: while (getOneIntersectingObject(Wall.class) = null) (that does not work.) Thanks in advance By the way, the code works fine, it's just the detection does not work on the vertical axes(it detects ground, but does not switch gravity direction)
danpost danpost

2013/7/29

#
For conditional equality, you need to use two equal signs:
while (getOneIntersectingObject(Wall.class) == null)
8bitcarrotjuice 8bitcarrotjuice

2013/7/29

#
Thank you so much!
8bitcarrotjuice 8bitcarrotjuice

2013/7/29

#
Dan, can you look at my source-code so far? Sorry, but I couldn't implement your code into my scenario; I couldn't make it work... But as of the problem: the game freezes when I implement this code into the cube class:
while(getOneIntersectingObject(S1.class)!=null||getOneIntersectingObject(S2.class)!=null||getOneIntersectingObject(S3.class)!=null||getOneIntersectingObject(S4.class)!=null)
        {
            intercepting=true;
        } 
        while(getOneIntersectingObject(S1.class)==null||getOneIntersectingObject(S2.class)==null||getOneIntersectingObject(S3.class)==null||getOneIntersectingObject(S4.class)==null)
        {
            intercepting=false;
        }
        while(intercepting==false) fall=true;
        while(intercepting==true) fall=false;
here is the link http://www.greenfoot.org/scenarios/9046
Zamoht Zamoht

2013/7/29

#
Use "if" instead of "while". The reason it freezes is because it gets stuck in the while loop because the conditions never change (you do not change anything inside the while loop that will ever make the statement false). And your code doesn't really make sense. You set the intercepting boolean and then you change it to the opposite right afterwards.
8bitcarrotjuice 8bitcarrotjuice

2013/7/29

#
Zamoht how would I change the 'while(getOneIntersectingObject(S1.class)==null||getOneIntersectingObject(S2.class)==null||getOneIntersectingObject(S3.class)==null||getOneIntersectingObject(S4.class)==null) ' into detecting when it is not intersecting with the ground?
Zamoht Zamoht

2013/7/29

#
Add it as an if-statement to the act method.
public void act()
{
     if(getOneIntersectingObject(S1.class)==null ||
getOneIntersectingObject(S2.class)==null ||
getOneIntersectingObject(S3.class)==null ||
getOneIntersectingObject(S4.class)==null)
     {
          //the object is not intersecting S1, S2, S3 nor S4.
     }
     //the rest of the act method. The if-statement does not have to be first since this is just an example.
}
Put your code instead of the comment.
8bitcarrotjuice 8bitcarrotjuice

2013/7/29

#
Can someone look at my game and help me? The problem is that it stays in the air even when not intercepting the platform when you move around(a & d). Thanks in advance. Link: http://www.greenfoot.org/scenarios/9046
Zamoht Zamoht

2013/7/29

#
I have no idea if this is what you want or if it f***s anything up, but I made it work by modifying a part of the vector method a little.
        if(fall==true&&plat==1)
        {
            ygravity=-0.15;
            ydefiner -= ygravity;
            setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
        }
        if(fall==true&&plat==2)
        {
            xgravity=0.15;
            xdefiner -= xgravity;
            setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
        }
        if(fall==true&&plat==3)
        {
            xgravity=-0.15;
            ydefiner -= ygravity;
            setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
        }
        if(fall==true&&plat==4)
        {
            ygravity=0.15;
            xdefiner += xgravity;
            setLocation(getexactX()+xdefiner, getexactY()+ydefiner);
        }
8bitcarrotjuice 8bitcarrotjuice

2013/7/29

#
Thank you so much!
You need to login to post a reply.
1
2