2
\$\begingroup\$

I am trying to implement collision detection so I am drawing a red rectangle to see if it is working. When I put the code below into the update method to check if it is going to work the position is not in the right place. The red rectangle starts from the middle and not at the x and y point? So it draws it wrong.

I also have a getter method so nothing wrong there.

bullet.set(getX(), getY(), getOriginX(), getOriginY());

this is for the render

shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(Color.RED);
shapeRenderer.rect(bullet.getX(), bullet.getY(), bullet.getOriginX(), bullet.getOriginY(), 15, 5, bullet.getRotation());
shapeRenderer.end();

I have tried to do it with a circle but the circle draws in the middle and I want it to be at the tip of the bullet. At the front of the bullet. x, y point.

boundingCircle.set(getX() + getOriginX(), getY() + getOriginY(), 4.0f);

shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(Color.RED);
shapeRenderer.circle(bullet.getBoundingCircle().x, bullet.getBoundingCircle().y, bullet.getBoundingCircle().radius);
shapeRenderer.end();

I need it to be of the x and y as the bullet is in the middle of the sprite when drawn originally via paint.

\$\endgroup\$
3
  • \$\begingroup\$ You may have to minus half the width and height of the rectangle from the x and y positions… I'm assuming the "getOriginX()" and "getOriginY()" methods return the width and height for the rectangle… try replacing the "bullet.getX(), bullet.getY()" variables with these "bullet.getX() - (bullet.getOriginX() / 2), bullet.getY() - (bullet.getOriginY() / 2)" \$\endgroup\$
    – Savlon
    Commented Jun 9, 2014 at 1:34
  • \$\begingroup\$ thay is not working as originally the bullet was drawn facing LEFT and i used the flip method to flip it to face right. and hence the origin starts from the back \$\endgroup\$
    – johnny-b
    Commented Jun 9, 2014 at 3:48
  • \$\begingroup\$ log and compare your image/sprite origin with the origin you‘re drawing in the shapeRenderer \$\endgroup\$ Commented Jun 9, 2014 at 10:34

1 Answer 1

1
\$\begingroup\$

To change the origin of your bounds, you can add the width/height of the rectangle, or subtract.

If the origin of the rectangle is bottom left but you need it positioned from the centre, you would do:

rect.x + (width / 2), rect.y + (height / 2)

This will take the x coordinate, add the half width on which puts the x value at a midpoint along the rect.

Play around with this to fit your code.

\$\endgroup\$

You must log in to answer this question.

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