2
\$\begingroup\$

i am trying to get collision dectection working properly but when i do it, the collision area keeps moving. no matter what i tried. there are 2 objects and the objects are moving and i cannot get the area of the collision to follow the enemy/object.

i am using the method below to see if the collision will work and it does not follow the enemy properly.

enemy class extends sprite {

the above is an example of how i am using the sprite extension.

the object/enemy is a sprite from libgdx the class is extended by sprite.

shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(Color.RED);

shapeRenderer.rect(rect1.getX() + (rect1.getWidth() / 2), rect1.getY()  + (rect1.getHeight() / 2) - 2, rect1.getOriginX() - 1, rect1.getOriginY() - 4, 1.0f, 1.0f, rect1.getRotation());

shapeRenderer.rect(rect2.getX() - 10, rect2.getY() - 10, rect2.getWidth() - 10, rect2.getHeight() - 10, rect2.getOriginX(), rect2.getOriginY(), rect2.getRotation());

shapeRenderer.end();

this is in the enemy class collision check

rect.set(getX() + (getWidth() / 2), getY()  + (getHeight() / 2) - 2, getOriginX() - 1, getOriginY() - 4);

this is to see if the enemy is colliding

public boolean collides(Ball ball) {
 if (getX() < ball.getX()) {
 return (Intersector.overlaps(ball.getRect1(), rect));
  }
 return false;

 }

Any help will be really nice. thank you

\$\endgroup\$
5
  • \$\begingroup\$ Give more details about your collision detection implementation, please. \$\endgroup\$ Jun 17, 2014 at 15:55
  • \$\begingroup\$ @GustavoMaciel i have edited the code \$\endgroup\$
    – johnny-b
    Jun 17, 2014 at 19:32
  • \$\begingroup\$ Anyone anything???? \$\endgroup\$
    – johnny-b
    Jun 19, 2014 at 0:56
  • \$\begingroup\$ Why do people close off your questions if they think it is off topic??? Is it because they are pros and don't care about newbies. This question is relevant as i need help. Sorry this is unfair! Just remember every pro was one day a newbie and did not have a clue how to program!!!]] \$\endgroup\$
    – johnny-b
    Jun 24, 2014 at 18:11
  • \$\begingroup\$ Agreed with @johnny-b. This question shouldn't be closed. \$\endgroup\$
    – Nilanchala
    Jul 10, 2014 at 23:17

1 Answer 1

0
\$\begingroup\$

Is it possible that you're tracking location in two different places? I noticed in your code

shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(Color.RED);

shapeRenderer.rect(rect1.getX() + (rect1.getWidth() / 2), rect1.getY()  + (rect1.getHeight() / 2) - 2, rect1.getOriginX() - 1, rect1.getOriginY() - 4, 1.0f, 1.0f, rect1.getRotation());

shapeRenderer.rect(rect2.getX() - 10, rect2.getY() - 10, rect2.getWidth() - 10, rect2.getHeight() - 10, rect2.getOriginX(), rect2.getOriginY(), rect2.getRotation());

shapeRenderer.end();

that you use a rect1 and rect2. Do these correspond to the sprites location as well?

\$\endgroup\$
3
  • \$\begingroup\$ no i have two enemies hence the rect1 and rect2 \$\endgroup\$
    – johnny-b
    Jun 19, 2014 at 17:13
  • \$\begingroup\$ @johnny-b and those are both your sprite type objects? \$\endgroup\$ Jun 19, 2014 at 17:15
  • \$\begingroup\$ yes both are sprites as in the enemy class i extend sprite and i made them as sprites in the assetloader class, due to them rotating they need to be sprites. i managed to do it for just normal enemies moving left and right as that was easy but when sprites are moving and have a originx and originy position it is harder to get the collision area to stay within the character. the enemies have round parts so cannot use the whole sprite as collision area. \$\endgroup\$
    – johnny-b
    Jun 20, 2014 at 16:55

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