Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
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 1156 times)
0 Members and 1 Guest are viewing this topic.
Offline aufde

Senior Newbie





« Posted 2012-10-14 21: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-14 22: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-14 23: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!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 800
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2012-10-15 00: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
Offline aufde

Senior Newbie





« Reply #4 - Posted 2012-10-15 01: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 04: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.

 

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

The first screenshot will be displayed as a thumbnail.

Grunnt (11 views)
2014-09-23 14:38:19

radar3301 (13 views)
2014-09-21 23:33:17

BurntPizza (31 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (29 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (75 views)
2014-09-10 13:57:51

TehJavaDev (105 views)
2014-09-10 06:39:09
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!