How to use AffineTransform and also a cordinate based system in Java? -
How to use AffineTransform and also a cordinate based system in Java? -
i have been working on game in java, has rotational based motion (meaning rotate turn, , can move forwards , backwards in direction rotated towards). have working, ran problem @ end. in order utilize affinetransform, utilize g2d.drawimage([image], [name of affinetransform], null); not allow input of coordinates of place character, meaning cannot move character on screen, rotate it.
how can transform player image (to rotate it), , place correctly on screen through xpos , ypos variables have set on screen?
if help, here code.
import java.awt.graphics; import java.awt.graphics2d; import java.awt.geom.affinetransform; import java.awt.image.bufferedimage; public class player { imageloader loader = new imageloader(); private bufferedimage player = loader.loadimage("/player.png"); private bufferedimage rotater = loader.loadimage("/player.png"); affinetransform transform = new affinetransform(); public int xpos = 200; public int ypos = 200; public double degrees = 90.0; public input input; public void render(graphics g) //somewhere in render method have rotate , draw player screen. { transform.settoidentity(); transform.translate(player.getwidth() / 2, player.getheight() / 2); transform.rotate(math.toradians(degrees)); transform.translate(-player.getwidth()/2, -player.getheight()/2); graphics2d g2d = (graphics2d) g; g2d.drawimage(player, transform, null); system.out.println("player_x = "+xpos+" | " + "player_y " + ypos + " | " + "rotation " + degrees); } public void moveforward() { xpos += (int) (math.sin(degrees * (math.pi/180)) * 4); ypos += (int) (math.cos(degrees * (math.pi/180)) * -4); } public void movebackward() { xpos -= (int) (math.sin(degrees * (math.pi/180)) * 4); ypos -= (int) (math.cos(degrees * (math.pi/180)) * -4); } public void rotateleft() { degrees = degrees - 4; } public void rotateright() { degrees = degrees + 4; } }
thank much, hope 1 of can help me.
ryan corkery
you have code rotate , translate (draw @ position) in method. original code drawing image @ coordinate -width/2, -height/2 (because image rotate center, not it's top-left corner)
so, it's matter of adding translation need draw @ xpos, ypos:
transform.translate(-xpos + player.getwidth() / 2, -ypos + player.getheight() / 2); transform.rotate(math.toradians(degrees)); transform.translate(xpos - player.getwidth()/2, ypos - player.getheight()/2);
java affinetransform
Comments
Post a Comment