0
\$\begingroup\$

I'm going crazy with this problem. I'll explain:

I have a camera that falls down (flowing along the y-axis) which displays a TiledMap and declare the whole in this way:

In the create method:

map=new TmxMapLoader().load("Map.tmx");
this.renderer=new OrthogonalTiledMapRenderer(map);
this.camera=new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
this.camera.translate(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight()/2);
this.camera.update();

In the render method:

this.renderer.setView(camera);
this.renderer.render();
camera.position.y+=1;
camera.update();

Then I have also a sprite drawing by SpriteBatch in the render method:

batchM.begin();
sprite.draw(batchM);
batchM.end();

In the tiledmap there are MapObject and I have to check if this objects hit to sprite and I do this so in the render method:

MapObjects collisionObjects =  map.getLayers().get("objects").getObjects();
        for(MapObject object : collisionObjects) {

               if (object instanceof RectangleMapObject) {
                   RectangleMapObject rect = ((RectangleMapObject) object);
                   Vector3 coo=camera.project(new Vector3(rect.getRectangle().x,rect.getRectangle().y,0));
                    com.badlogic.gdx.math.Rectangle Sprite= sprite.getBoundingRectangle();
                    com.badlogic.gdx.math.Rectangle Map=new com.badlogic.gdx.math.Rectangle(coo.x,coo.y,32,32);
                    //TEST
                   System.out.println("PLAYER X: "+Sprite.x+" Y: "+Sprite.y);
                   System.out.println("Object X: "+Map.x+" Y: "+Map.y);
                   if(Intersector.intersectRectangles(Sprite, Map, Sprite)) System.out.println("HIT");
          } }

My problem is that when I do not change the window size(remain the size the starting), collisions are detected fine, but when I change the window size (for example, I enlarge) collisions are not detected and I can not explain why.

Could you help me to fix this bug. Thank you for your time

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

The SpriteBatch has its own internal camera. Are you also setting the batcher to use the camera's combined projection matrix by calling:

       batchM.setProjectionMatrix(camera.combined);

The problems you could be experiencing, could perhaps be explained by the fact that collisions only appear to be happening on screen, but actually aren't as the map and the sprites are using two different coordinate systems which have been overlayed over one another. The coordinate systems just happen to correspond when the screen is its initial size, but when resized, diverge from the model. Then again, perhaps not. Without more code, I can't really tell.

\$\endgroup\$
8
  • \$\begingroup\$ Hi,thank you for answer. I tried to do like you said about the Batch but then the sprite goes down with the camera.What information you need in more to try solve this problem? I thank you again for the availability \$\endgroup\$ Commented Aug 27, 2014 at 12:01
  • \$\begingroup\$ Hi @programmer_dna, now you need to change the direction the sprite moves in. So if you used to move the sprite in the +ve direction, now you need to move it in the negative direction, but only for the y-axis \$\endgroup\$
    – RabtFt
    Commented Aug 27, 2014 at 13:24
  • \$\begingroup\$ I'm sorry I did not understand, I could give an example? I do move the sprite based on the directional buttons (for example, if is pressed the right arrow the sprite will have to go to the right and so on). so if you do not press any button the sprite should stand firm in the direction in which it is. \$\endgroup\$ Commented Aug 27, 2014 at 14:03
  • \$\begingroup\$ I'm assuming that you're trying to move the terrain around the character? With a camera system you shouldn't think of it from that perspective. Think of it more as the character is falling, and the terrain is standing still. So make sure you're moving the character downward each frame at the same rate as the camera is moving upward/downward. \$\endgroup\$
    – RabtFt
    Commented Aug 27, 2014 at 14:12
  • \$\begingroup\$ I'm sorry but I do "fall" only the sprite down the background will never change. I do not understand this \$\endgroup\$ Commented Aug 27, 2014 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .