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

2017/3/19

Cartesian Coordinates?

Wasupmacuz Wasupmacuz

2017/3/19

#
I'm just wondering if there is any way to reprogram the world so that the (0,0) coordinates are in the middle of the world instead of in the top-left. I want to know this because I just made a 300 line method that won't work because of this (oops I'm used to scratch.mit.edu). I appreciate if anyone who knows anything about this!!
Super_Hippo Super_Hippo

2017/3/19

#
What exactly did you do? Did you create objects to a world which are now half size offset? Do you need getX and getY return different values? I don't think that it would be a good idea to change Greenfoot on your computer, but you can override the methods you want to use differently.
Wasupmacuz Wasupmacuz

2017/3/20

#
Super_Hippo wrote...
What exactly did you do? Did you create objects to a world which are now half size offset? Do you need getX and getY return different values? I don't think that it would be a good idea to change Greenfoot on your computer, but you can override the methods you want to use differently.
Well it's just that I was making a top-down, scrolling game but it is not working because I made everything so that it would happen if objects were in the middle and I did not want to have to go through all of the code again to fix it. If I was to change Greenfoot, how would I go about doing that? I can back everything up on a flash drive in case something were to go wrong...
Super_Hippo Super_Hippo

2017/3/20

#
Changing Greenfoot would require to change some of the .class files in Greenfoot. You can download the source code from [u]https://www.greenfoot.org/site/download_source[/u]. You can open .class files using for example Eclipse (if I am not mistaken). I am pretty sure that it is easier and faster to go through your code again though.
Wasupmacuz Wasupmacuz

2017/3/22

#
Super_Hippo wrote...
Changing Greenfoot would require to change some of the .class files in Greenfoot. You can download the source code from [u]https://www.greenfoot.org/site/download_source[/u]. You can open .class files using for example Eclipse (if I am not mistaken). I am pretty sure that it is easier and faster to go through your code again though.
Okay thank you!
MrBradley MrBradley

2017/3/24

#
Changing the source code is a drastic step for solving this problem. I have come across similar issues in class. You can map any coordinate system to/from Greenfoot's native (and normal for computer graphics) system. Implement you new coordinate system by inheriting from world and add methods to support the translation. Be sure to check if you can override existing ones as well setLocation, getX, getY etc. I use a scenario where the bottom left is 1, 1.
danpost danpost

2017/3/24

#
There is a way to superclass your subclasses of World and Actor to modify the coordinate system. It involves overriding various methods in both the World and Actor classes. If you are still interested, I can provide more info.
Wasupmacuz Wasupmacuz

2017/3/25

#
danpost wrote...
There is a way to superclass your subclasses of World and Actor to modify the coordinate system. It involves overriding various methods in both the World and Actor classes. If you are still interested, I can provide more info.
I'm still interested :P I come across this problem a lot in my scenarios. However, I already re-coded the class yesterday :/
danpost danpost

2017/3/25

#
Wasupmacuz wrote...
I'm still interested :P I come across this problem a lot in my scenarios. However, I already re-coded the class yesterday :/
Well, you will need both the following classes to extend everything from. The TransWorld class (for translating of world coordinates):
import greenfoot.*;
import java.awt.Color;

public abstract class TransWorld extends World
{
    public int offW = 0, dirW = 1, offH = 0, dirH = 1;
    
    public void setTranslation(int w, int dw, int h, int dh)
    {
        offW = w;
        dirW = dw;
        offH = h;
        dirH = dh;
    }
    
    public TransWorld(int w, int h, int c)
    {
        this(w, h, c, true);
    }
    
    public TransWorld(int w, int h, int c, boolean b)
    {
        super(w, h, c, b);
    }
    
    public void addObject(Actor actor, int x, int y)
    {
        super.addObject(actor, transX(x), transY(y));
    }
    
    public Color getColorAt(int x, int y)
    {
        return super.getColorAt(transX(x), transY(y));
    }
    
    public void showText(String text, int x, int y)
    {
        // super.showText(text, transX(x), transY(y));
    }
    
    public java.util.List<?> getObjectsAt(int x, int y, Class clss)
    {
        return super.getObjectsAt(transX(x), transY(y), clss);
    }
    
    public final int transX(int x)
    {
        return offW+dirW*x;
    }
    
    public final int transY(int y)
    {
        return offH+dirH*y;
    }
}
and the TransActor class (for translating actor locations and offsets):
import greenfoot.*;

public abstract class TransActor extends Actor
{
    public TransActor()
    {
    }

    public int getX()
    {
        return ((TransWorld)getWorld()).transX(super.getX());
    }
    
    public int getY()
    {
        return ((TransWorld)getWorld()).transY(super.getY());
    }
    
    protected java.util.List<Actor> getObjectsAtOffset(int dx, int dy, Class clss)
    {
        return super.getObjectsAtOffset(transDX(dx), transDY(dy), clss);
    }
    
    protected Actor getOneObjectAtOffset(int dx, int dy, Class clss)
    {
        return super.getOneObjectAtOffset(transDX(dx), transDY(dy), clss);
    }
    
    public void turnTowards(int x, int y)
    {
        super.turnTowards(((TransWorld)getWorld()).transX(x), ((TransWorld)getWorld()).transY(y));
    }    
    
    private int transDX(int dx)
    {
        return ((TransWorld)getWorld()).dirW*dx;
    }
    
    private int transDY(int dy)
    {
        return ((TransWorld)getWorld()).dirH*dy;
    }
}
In the constructor of your subclass of TransWorld, after the 'super' call, call the 'setTranslation' method to fix the origin and positive direction required. For standard Cartesian coordinates with origin in the center of the world, use:
setTranslation(getWidth()/2, 1, getHeight()/2, -1);
You may need to remove the 'java.awt.Color' import line in TransWorld class as well as uncommenting the code in the 'showText' method. I did not document anything here -- so, I hope you can understand it. If not, you can ask about any part of it for an explanation. Also, this was not tested thoroughly, and even so, there is a possibility that it behaves differently with your version of greenfoot than mine.
MrBradley MrBradley

2017/3/25

#
Excellent Dan.
Wasupmacuz Wasupmacuz

2017/3/27

#
danpost wrote...
Wasupmacuz wrote...
I'm still interested :P I come across this problem a lot in my scenarios. However, I already re-coded the class yesterday :/
Well, you will need both the following classes to extend everything from. The TransWorld class (for translating of world coordinates):
import greenfoot.*;
import java.awt.Color;

public abstract class TransWorld extends World
{
    public int offW = 0, dirW = 1, offH = 0, dirH = 1;
    
    public void setTranslation(int w, int dw, int h, int dh)
    {
        offW = w;
        dirW = dw;
        offH = h;
        dirH = dh;
    }
    
    public TransWorld(int w, int h, int c)
    {
        this(w, h, c, true);
    }
    
    public TransWorld(int w, int h, int c, boolean b)
    {
        super(w, h, c, b);
    }
    
    public void addObject(Actor actor, int x, int y)
    {
        super.addObject(actor, transX(x), transY(y));
    }
    
    public Color getColorAt(int x, int y)
    {
        return super.getColorAt(transX(x), transY(y));
    }
    
    public void showText(String text, int x, int y)
    {
        // super.showText(text, transX(x), transY(y));
    }
    
    public java.util.List<?> getObjectsAt(int x, int y, Class clss)
    {
        return super.getObjectsAt(transX(x), transY(y), clss);
    }
    
    public final int transX(int x)
    {
        return offW+dirW*x;
    }
    
    public final int transY(int y)
    {
        return offH+dirH*y;
    }
}
and the TransActor class (for translating actor locations and offsets):
import greenfoot.*;

public abstract class TransActor extends Actor
{
    public TransActor()
    {
    }

    public int getX()
    {
        return ((TransWorld)getWorld()).transX(super.getX());
    }
    
    public int getY()
    {
        return ((TransWorld)getWorld()).transY(super.getY());
    }
    
    protected java.util.List<Actor> getObjectsAtOffset(int dx, int dy, Class clss)
    {
        return super.getObjectsAtOffset(transDX(dx), transDY(dy), clss);
    }
    
    protected Actor getOneObjectAtOffset(int dx, int dy, Class clss)
    {
        return super.getOneObjectAtOffset(transDX(dx), transDY(dy), clss);
    }
    
    public void turnTowards(int x, int y)
    {
        super.turnTowards(((TransWorld)getWorld()).transX(x), ((TransWorld)getWorld()).transY(y));
    }    
    
    private int transDX(int dx)
    {
        return ((TransWorld)getWorld()).dirW*dx;
    }
    
    private int transDY(int dy)
    {
        return ((TransWorld)getWorld()).dirH*dy;
    }
}
In the constructor of your subclass of TransWorld, after the 'super' call, call the 'setTranslation' method to fix the origin and positive direction required. For standard Cartesian coordinates with origin in the center of the world, use:
setTranslation(getWidth()/2, 1, getHeight()/2, -1);
You may need to remove the 'java.awt.Color' import line in TransWorld class as well as uncommenting the code in the 'showText' method. I did not document anything here -- so, I hope you can understand it. If not, you can ask about any part of it for an explanation. Also, this was not tested thoroughly, and even so, there is a possibility that it behaves differently with your version of greenfoot than mine.
Very cool! I think I understand it and I'll put it to use!
You need to login to post a reply.