0
\$\begingroup\$

In the process of developing a 2D game and am having some issues with translating coordinates on rotation. The object being rotated is a sword that is attached to the player. The player currently points towards a Point (mouse pointer). I am having no problems drawing the rotation, but am a bit confused on how to update the local coordinates so that the bounding box will follow also, around the player.

The weapon (coordinates) is just stuck on the left side of the player(invisible), but the drawing of the weapon is rotating around the player. The bounding box is working fine, but who wants invisible collision detection. It basically just isn't following the draw rotation.

Here is my basic draw implementation for rotating this weapon around the player.

WEAPON CLASS:

/**
* Updates child weapon
*/
public void Update(Point mp)
{
    this.boundingBox = new BoundingBox(startX,startY,width,height);
}

/**
 * Draws weapon to player
 */
public void DrawTo(Graphics2D g2d,Player p)
{
   AffineTransform backup = g2d.getTransform();
   AffineTransform trans = new AffineTransform();
   trans.rotate(Math.toRadians(getRotation()), p.getCenterX(), p.getCenterY() );
   g2d.transform( trans );
   g2d.drawImage(this.getImg(), startX, startY,null);
   g2d.setTransform( backup );
}

GAME LOOP:

/**
 * Player collision with weapons
 */
for(int l = 0; l < weaponManager.getDroppedWeapons().size(); l++)
{
    Weapon w  = this.weaponManager.getDroppedWeapons().get(l);

    if(player.Collision(w))
    {
        //If player already has a weapon, update that weapons position, add others to list that are picked up later
        if(player.getWeaponEquipStatus() == true)
        {
            //Sets the picked up weapon to the left side of the player
            player.getCurrentWeapon().setX(player.getX()-player.getCurrentWeapon().getWidth());
            player.getCurrentWeapon().setY(player.getY()+((player.getHeight()-player.getCurrentWeapon().getHeight())/2));
        }
        else if(player.getWeaponEquipStatus() == false)
        {
            player.setCurrentWeapon(w);
            w.isPickedUp = true;
        }
    }
}

I am aware of the AfflineTransform's getTranslateX and Y() methods. I have tried assigning these returned values to my x and y weapon values in the update method, but that is definitely not the solution.

Main issue: weapon coordinates aren't rotating, but the drawing is successful.

If anyone has any advice on where to start, based on my problem, it would be greatly appreciated.

\$\endgroup\$
1
  • \$\begingroup\$ Im not sure if I understand what is your problem. Could you like post a picture or elaborate on what you were expecting and what the coding is doing? \$\endgroup\$
    – user55564
    Dec 5, 2014 at 1:39

1 Answer 1

1
\$\begingroup\$

It looks like you are using a matrix to rotate the image when it is drawn. This does not change the position values you have stored in your class, but it does affect how the image is drawn. If you want to know where the 4 corners of the image are drawn you will have to keep track of them yourself. Same is true of the bounding box. If the bounding box is to rotate along with the image, you will need to transform the 4 corners of the bounding box just like you are transforming the image when you draw it.

This brings up another issue. If you are rotating your bounding box, then you need to use a collision detection method that can properly handle non-axis aligned bounding boxes. An example would be separate axis theorem (SAT).

\$\endgroup\$
4
  • \$\begingroup\$ That makes total sense. So would this all be done in the update method because i'm not dealing with drawing objs anymore? I will give it a shot. Thanks a lot. \$\endgroup\$
    – Kamakazi
    Dec 5, 2014 at 3:09
  • \$\begingroup\$ The best place probably depends on how you have things set up. Going off of your update method listed, it might make more sense to do this in a setRotation method. This way you are only calculating it when the rotation changes, and not every time update is called \$\endgroup\$
    – Aholio
    Dec 5, 2014 at 4:23
  • \$\begingroup\$ It worked very well. I kept track of the coordinates with Point2D and just passed in those updated coordinates to the bounding box. Thanks for the assistance. \$\endgroup\$
    – Kamakazi
    Dec 6, 2014 at 6:56
  • \$\begingroup\$ Excellent, glad I could help. If you mark this question as answered then others will know that you found a solution, and someone searching for a similar question can see an acceptable answer \$\endgroup\$
    – Aholio
    Dec 6, 2014 at 18:13

You must log in to answer this question.

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