Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Lost with affine transform...  (Read 787 times)
0 Members and 1 Guest are viewing this topic.
Offline aufde

Senior Newbie





« Posted 2012-10-14 23:13:55 »

I am brand new to java game programming (started learning about a week ago). So far, I've taken someone else's Space Invader tutorial and started with a skeleton and modified a few things. Right now what I have is a blank screen with the ship in the center. Aliens spawn randomly and move in a random direction (supposed to be toward the player, but that bug is for another day). I've figured out how to take mouse clicks and turn it into a shot in the direction of the mouse click, but the shots that come out are always pointing up. I want to apply AffineTransform to rotate the shot on the angle that its traveling. That is where I get lost. Between the AffineTranform API and any sort of tutorial, I can't figure out how or where to put any of it.

here's what I have so far:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
package shooter;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;

public class Sprite {
   
   //create the tranform and image used for the sprite
  private AffineTransform transform = new AffineTransform();
   private Image image;
   private double angle;
   
   //creates the sprite with a scale and angle parameter
  public Sprite (Image image, double scale, double angle){
      this.angle = angle;
      //uses the scale to make the sprite the appropriate size
     this.image = image.getScaledInstance((int)(image.getWidth(null) * scale), (int)(image.getHeight(null) * scale), Image.SCALE_DEFAULT);
   }
   
   public int getWidth() {
      return image.getWidth(null);
   }
   
   public int getHeight() {
      return image.getHeight(null);
   }  
   
   public void draw(Graphics g, int x, int y) {
      //not sure if this is how you're supposed to do this,
     //but I'm trying to move the sprite to the passed x and y
     //then rotate it
     transform.translate(x, y);
      transform.getRotateInstance(Math.toRadians(angle), image.getWidth(null)/2, image.getHeight(null)/2);
      g.drawImage(image, x, y, null);
   }
}

the transform is never actually applied to the image how i have it here because affineTranform doesn't have any way to pass and image to it... I'm so lost...  Huh

This is just the sprite part of another class called Entity which is used to create the player, aliens, and shots. Hopefully this makes enough sense that someone can help me. If you do need anything else, let me know because I want to figure this out...
Offline StumpyStrust
« Reply #1 - Posted 2012-10-15 00:36:06 »

Yup this is how to use AffineTransform for rotation

1  
g2d.setTransform(AffineTransform.getRotateInstance(rotation.x, loc.x, loc.y));


Then just draw the image like normal.

Don't have it so each sprite as an AffineTransform as you really don't need it.

I should add this to my java2D guides.

http://www.java-gaming.org/topics/java2d-clearing-up-the-air/27506/view.html

Look at that for other tips on using java2D.
Offline aufde

Senior Newbie





« Reply #2 - Posted 2012-10-15 01:32:43 »

So, I got rid of the instanced AffineTransform and changed the draw method to:
1  
2  
3  
4  
public void draw(Graphics2D g, int x, int y) {
      g.setTransform(AffineTransform.getRotateInstance(Math.toRadians(angle), x, y));
      g.drawImage(image, x, y, null);
   }


it still doesn't work, could you please put that into context for me? Where exactly does that line need to go? I tried putting it in the game loop right before the entity is drawn and nothing works. Is there anything else that needs to be changed?
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Online Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #3 - Posted 2012-10-15 02:31:57 »

It's not that clear, but
   g2d.setTransform(AffineTransform)
should almost never be used, it overwrites the current transform completely.

Use
   g2d.transform(AffineTransform)
instead, as that will multiply the current transformation matrix with the specified one.

You can read the javadocs for more information on how both methods behave.



Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Offline aufde

Senior Newbie





« Reply #4 - Posted 2012-10-15 03:34:17 »

Update: the affine transform was working, it was an issue with my code that always passed 0 degrees to the transform, so it looked like it wasn't doing anything. You're explanation was plenty clear, I just messed up some other code.

And I will change that line so I don't mess with the default transform.

Thanks everyone!
Offline StumpyStrust
« Reply #5 - Posted 2012-10-15 06:13:40 »

My bad Riven I forgot that.

This is what I do when drawing stuff.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
Graphics2D g2d = (Graphics2D)g.create();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) (fade)));
        g2d.setTransform(AffineTransform.getRotateInstance(rotation.x, loc.x, loc.y));
      if(image != null)
      {
         if(alive)
         {
            int x1 = (int) (loc.x - (size/2));
            int y1 = (int) (loc.y - (size/2));
            g2d.drawImage(image, x1, y1, (int)size, (int)size, null);
         }
      }
      else if(alive)
      {
         int x1 = (int)size >> 1;
            int y1 = (int)size >> 1;
         g2d.setColor(color);
         g2d.fillRect((int)(loc.x - x1), (int)(loc.y - y1), (int)size, (int)size);
      }
      g2d.dispose();


Important part is the g.create();

I do not know if this is a memory leak or if it is slower but in testing it makes no difference for me.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Try the Free Demo of Revenge of the Titans

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (100 views)
2013-05-17 21:29:12

alaslipknot (108 views)
2013-05-16 21:24:48

gouessej (138 views)
2013-05-16 00:53:38

gouessej (133 views)
2013-05-16 00:17:58

theagentd (145 views)
2013-05-15 15:01:13

theagentd (131 views)
2013-05-15 15:00:54

StreetDoggy (174 views)
2013-05-14 15:56:26

kutucuk (196 views)
2013-05-12 17:10:36

kutucuk (198 views)
2013-05-12 15:36:09

UnluckyDevil (204 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.167 seconds with 20 queries.