alpha_x's Profile
Reputation: 6
Worker
- Group:
- Active Members
- Active Posts:
- 50 (0.07 per day)
- Joined:
- 03-May 11
- Profile Views:
- 922
- Last Active:
Oct 10 2012 07:08 AM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Zombie AI - not animating independantly
Posted 7 Oct 2012
Just to add my ZombieAI() is called in my GameUpdate method which is run in the GameLoop thread:
public void run() { Thread t = Thread.currentThread(); while (t == gameloop) { try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } // poll keyboard - check if keys were pressed keyboard.poll(); gameUpdate(); } } public void gameUpdate() { // process keys that are held down handleInput(); // draw background // g2d.setColor(Color.WHITE); g2d.fill(new Rectangle(0, 0, ScreenWidth - 1, ScreenHeight - 1)); drawBackground(); // player.transform(); checkPlayerBoundaries(); player.setBoundsEnabled(true); drawPlayer(player.getBoundsEnabled()); zombieAI(); checkZombieBoundaries(); for (int k = 0; k < MAX_ZOMBIES; k++) { zombie[k].drawBounds(Color.BLUE); zombie[k].draw(); } drawBullets(); checkBulletStatus(); // JUST FOR DEBUG PURPOSES, REMOVE UPON COMPLETION drawHealthBar(); g2d.setColor(Color.BLACK); g2d.drawString("Position: " + player.x + "," + player.y, 10, 40); g2d.drawString("Velocity: " + player.x + "," + player.y, 10, 60); g2d.drawString("Animation: " + player.getCurrentFrame(), 10, 80); g2d.drawString("Face Angle: " + player.getFaceAngle(), 10, 100); g2d.drawString("Bullets Left: " + (MAX_BULLETS - currentShot), 10, 120); g2d.drawString("Bullets Alive: " + getBulletsAlive(), 10, 140); repaint(); } -
In Topic: Applying a transform when selectively drawing parts of a source image
Posted 6 Oct 2012
Oops silly me, thanks that worked! -
In Topic: Applying a transform when selectively drawing parts of a source image
Posted 6 Oct 2012
Sorry I just now realised that this may not be the right place for this question, if that is the case then can a moderator please move to the Game Programming sub-forum. -
In Topic: Applying a transform when selectively drawing parts of a source image
Posted 6 Oct 2012
I now am able to draw my animation whilst applying a transform to the image. It required a rework of the AnimatedSprite class whereby I extract the images into a BufferedImage array and then in my draw method, I draw the current image with a transformation like so:
g2d.drawImage(animation[getCurrentFrame()], at, getJFrame());
However the image itself is still not rotating around its center despite using the following rotate method which I thought would definitely work
at.rotate(Math.toRadians(getFaceAngle()), getCenterX(), getCenterY());
Below is my updated AnimatedSprite.java class:
import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import javax.imageio.ImageIO; import javax.swing.*; import java.io.IOException; import java.net.*; public class AnimatedSprite extends Entity { private JFrame frame; private Graphics2D g2d; private BufferedImage spriteSheet; private BufferedImage[] animation; private AffineTransform at; private int animDirection; private int frameCount, frameDelay; private int currentFrame, totalFrames; private int frameWidth, frameHeight, columns; private int frameX, frameY; AnimatedSprite(JFrame frame, Graphics2D graphics) { super(); // call Entity constructor and set defaults this.frame = frame; g2d = graphics; spriteSheet = null; alive = true; currentState = State.ALIVE; currentFrame = 0; totalFrames = 1; animDirection = 1; frameCount = 0; frameDelay = 0; frameWidth = 0; frameHeight = 0; columns = 1; } public void setAnimDirection(int animDirection) { this.animDirection = animDirection; } public void setFrameCount(int frameCount) { this.frameCount = frameCount; } public void setFrameDelay(int frameDelay) { this.frameDelay = frameDelay; } public void setCurrentFrame(int currentFrame) { this.currentFrame = currentFrame; } public void setTotalFrames(int totalFrames) { this.totalFrames = totalFrames; } public int getAnimDirection() { return animDirection; } public int getFrameCount() { return frameCount; } public int getFrameDelay() { return frameDelay; } public int getTotalFrames() { return totalFrames; } public int getCurrentFrame() { return currentFrame; } public JFrame getJFrame() { return frame; } public Graphics2D getGraphicsObject() { return g2d; } public void setGraphicsObject(Graphics2D graphics) { g2d = graphics; } public void setImage(BufferedImage image) { this.spriteSheet = image; double x = frame.getSize().width / 2 - getImageWidth() / 2; double y = frame.getSize().height / 2 - getImageHeight() / 2; at = AffineTransform.getTranslateInstance(x, y); } public int getImageWidth() { if (spriteSheet != null) return spriteSheet.getWidth(frame); else return 0; } public int getImageHeight() { if (spriteSheet != null) return spriteSheet.getHeight(frame); else return 0; } public double getCenterX() { return this.x + getImageWidth() / 2; } public double getCenterY() { return this.y + getImageHeight() / 2; } public Rectangle getBounds() { Rectangle bounds = new Rectangle((int) this.x, (int) this.y, frameWidth, frameHeight); return bounds; } public void load(String filename, int columns, int totalFrames, int width, int height) { Toolkit tk = Toolkit.getDefaultToolkit(); try { spriteSheet = ImageIO.read(getURL("players.png")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } while (spriteSheet.getHeight(frame) <= 0) ; double x = frame.getSize().width / 2 - getImageWidth() / 2; double y = frame.getSize().height / 2 - getImageHeight() / 2; at = AffineTransform.getTranslateInstance(x, y); this.columns = columns; this.totalFrames = totalFrames; this.frameWidth = width; this.frameHeight = height; animation = new BufferedImage[this.totalFrames]; // reserve space getAnimation(); } public void transform() { at.setToIdentity(); at.translate((int) getX() + getImageWidth() / 2, (int) getY() + getImageHeight() / 2); at.rotate(Math.toRadians(getFaceAngle()), getCenterX(), getCenterY()); at.translate(-getImageWidth() / 2, -getImageHeight() / 2); } public AffineTransform getTransform() { return at; } public void updateSprite() { // update position x += velX; y += velY; // update transform transform(); // update rotation // faceAngle +=5; this.setFaceAngle(faceAngle); if (faceAngle < 0) faceAngle = 360 - 5; else if (faceAngle > 360) faceAngle = 5; // update animation if (totalFrames > 1) { frameCount++; if (frameCount > frameDelay) { frameCount = 0; currentFrame += animDirection; if (currentFrame > totalFrames - 1) { currentFrame = 0; // first frame } else if (currentFrame < 0) { currentFrame = totalFrames - 1; // last frame } } } } public Image getImage() { return spriteSheet; } // check for collision with a rectangular shape public boolean collidesWith(Rectangle rectangle) { return (rectangle.intersects(getBounds())); } // check for collision with another sprite public boolean collidesWith(AnimatedSprite sprite) { return (getBounds().intersects(sprite.getBounds())); } private URL getURL(String filename) { URL url = null; try { url = this.getClass().getResource(filename); } catch (Exception e) { } return url; } public BufferedImage grabTile(int xpos, int ypos, int width, int height) { return spriteSheet.getSubimage(xpos, ypos, width, height); } public void getAnimation() { // load animation and store in BufferedImage // array for (int i = 0; i < totalFrames; i++) { BufferedImage tile = grabTile(frameX, frameY, frameWidth, // grab // needed // tile frameHeight); getFrame(); currentFrame++; animation[i] = tile; System.out.println(animation[i]); } currentFrame = 0; } public void getFrame() { frameX = (currentFrame % columns) * frameWidth; frameY = (currentFrame / columns) * frameHeight; } public void drawBounds(Color c) { g2d.setColor(c); g2d.draw(getBounds()); } public void draw() { updateSprite(); getFrame(); // draw frame g2d.drawImage(animation[getCurrentFrame()], at, getJFrame()); } }
I will keep working on this but any help would be awesome thanks! -
In Topic: Applying a transform when selectively drawing parts of a source image
Posted 5 Oct 2012
Ok I made some changes to AnimatedSprite.java, I now rotate the player around the centerX and centerY of the image and I set the graphics transform of the frame to the player before drawing it and then reset to default once the image is drawn. Here are the changes:
public void transform() { at.setToIdentity(); //at.translate((int)getX() + getImageWidth()/2, (int)getY() + getImageHeight()/2); //at.translate(at.getTranslateX(), at.getTranslateY()); at.rotate(Math.toRadians(getFaceAngle()),getCenterX(),getCenterY()); // at.translate(-getImageWidth()/2, -getImageHeight()/2); } public void draw() { AffineTransform original = g2d.getTransform(); updateSprite(); getFrame(); //draw frame g2d.setTransform(getTransform()); //g2d.drawImage(image,at,getJFrame()); g2d.drawImage(image,(int)this.x,(int) this.y,(int) this.x+frameWidth,(int) this.y + frameHeight, frameX, frameY, frameX+frameWidth, frameY+frameHeight, getJFrame()); g2d.setTransform(original); }
And now the player is rotating which is good however it is not rotating and standing still but rotating in a wide arc, any ideas? Thanks!
Also, somewhat of a sidebug is that the boundaries when I use drawBound(Color c) does not update? I cannot ascertain if I am making a mistake with my rotation or?
My Information
- Member Title:
- D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Private
Friends
|
|


Find Topics
Find Posts
View Reputation Given


|
Comments
alpha_x has no profile comments yet. Why not say hello?