Slick Forums

Discuss the Slick 2D Library
It is currently Wed Jun 19, 2013 6:40 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: Mon Sep 15, 2008 4:49 am 
Offline
Oldbie

Joined: Tue Jun 17, 2008 5:11 pm
Posts: 336
I am using Transform.translate to move my objects around (is that expensive? Should I use setX/Y instead?) and came across the following bug:

Code:
package com.shade;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Transform;

public class TestCase extends BasicGame {
   
    Shape player;
   
    public TestCase() {
        super("Translate test case");
    }

    @Override
    public void init(GameContainer container) throws SlickException {
        player = new Circle(300, 240, 20);
    }

    @Override
    public void update(GameContainer container, int delta)
            throws SlickException {
        if (container.getInput().isKeyDown(Input.KEY_LEFT)) {
            Transform t = Transform.createTranslateTransform(-2, 0);
            player = player.transform(t);
        }
        if (container.getInput().isKeyDown(Input.KEY_RIGHT)) {
            Transform t = Transform.createTranslateTransform(2, 0);
            player = player.transform(t);
        }
    }

    public void render(GameContainer container, Graphics g)
            throws SlickException {
        g.drawString("Player x: " + player.getX(), 10, 20);
        g.drawString("Player y: " + player.getY(), 10, 30);
        g.drawString("Player center x: " + player.getCenterX(), 10, 40);
        g.drawString("Player center y: " + player.getCenterY(), 10, 50);
        g.draw(player);
    }
   
    public static void main(String[] args) {
        try {
            AppGameContainer container = new AppGameContainer(new TestCase(), 800, 600, false);
            container.setTargetFrameRate(60);
            container.setVSync(true);
            container.start();
        } catch (SlickException e) {
            e.printStackTrace();
        }
    }

}


1 Run the test
2 Observe the correct x, y values
3 Move left or right with the arrow keys
4 Observe that x, y are set to zero

_________________
My website about game development http://anotherearlymorning.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 22, 2008 11:56 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
This is because the new shape you have is a polygon and it's origin is at 0,0. I agree it's counter-intuitive, however transforming a shape can have no prior knowledge about the type of transform thats being applied and since transforms can modify the verticies in any fashion it has to be something generic (like a Polygon) returned.

It'd also be really expensive to move shapes this way, use something like this instead:

Code:

        if (container.getInput().isKeyDown(Input.KEY_LEFT)) {
           player.setX(player.getX()-2);
        }
        if (container.getInput().isKeyDown(Input.KEY_RIGHT)) {
           player.setX(player.getX()+2);
        }


and you'll get the results you're looking for I think.

Alternatively you could use the calculated minx/miny values:

Code:
        g.drawString("Player x: " + player.getMinX(), 10, 20);
        g.drawString("Player y: " + player.getMinY(), 10, 30);


Kev


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 11:09 pm 
Offline
Oldbie

Joined: Tue Jun 17, 2008 5:11 pm
Posts: 336
It appears that the min x/y values are not calculated correctly after rotating an object. I started with the following code:

Code:
public float getWidth() {
  return shape.getMaxX() - shape.getX();
}


which works when the shape hasn't been rotated but breaks for the reasons described above.

I tried changing it to:

Code:
public float getWidth() {
  return shape.getMaxX() - shape.getMinX();
}


However this doesn't seem to work. Looking at things in the debugger the min-x value is way off (I'm talking it's off by hundreds of points). In my case, I am rotating a rectangle along one side (as if it were a door swinging). Any idea what I can do to work around this?

_________________
My website about game development http://anotherearlymorning.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 12:03 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Should be resolved in SVN. Let me know if you run into anything else in this area, it looks like there's some general flaws in the geom stuff.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 12:25 pm 
Offline
Game Developer
User avatar

Joined: Sun May 25, 2008 9:45 am
Posts: 578
Check in 1120 to Polygon caused my game to fail. The x and y were very large negative values, so when I called setX it was moving all my points by a huge amount. To "fix" it I changed Polygon(float[]) by removing the minus signs, so the code becomes:
Code:
        maxX = Float.MIN_VALUE;
        maxY = Float.MIN_VALUE;
        minX = Float.MAX_VALUE;
        minY = Float.MAX_VALUE;
        x = Float.MAX_VALUE;
        y = Float.MAX_VALUE;

I don't know how this change affects everybody else though.

_________________
SingSong Karaoke - http://singthegame.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 2:25 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
K, I'll revert tonight.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 9:00 pm 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
Changed it SVN. Can you confirm.

Kev


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 9:35 pm 
Offline
Game Developer
User avatar

Joined: Sun May 25, 2008 9:45 am
Posts: 578
My game now works with the SVN codebase, thanks! Mine is simple usage though, so definitely not much of an end to end test.

_________________
SingSong Karaoke - http://singthegame.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 15, 2009 9:47 pm 
Offline
Oldbie

Joined: Tue Jun 17, 2008 5:11 pm
Posts: 336
So what is the status of things now? Are we back where we started (i.e. you reverted) or are we have we fixed the issues w/ min/max x/y? I will write some code to test things if there's a fix in svn.

_________________
My website about game development http://anotherearlymorning.com


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 16, 2009 8:35 am 
Offline
Site Admin
User avatar

Joined: Thu Jan 01, 1970 12:00 am
Posts: 3143
I fixed Nates case, but the original fixes should still be in place. I got a little over zealous when I was fixing it :)

So the original bug should still resolved.

Kev


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group