Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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  
  Moving Player  (Read 1569 times)
0 Members and 1 Guest are viewing this topic.
Offline gfadmin

Senior Newbie





« Posted 2010-01-21 01:27:21 »

I am making a simple shooter game in Java for my Computer Science Final.

Currently Players can only move up and down. I was wondering how i could get them to move freely in the window.

i was thinking i might have forward and backwards with rotation.

If there is a certain function i should use please give an example of how to use it for example for using the simple drawImage

1  
drawImage(image, xPosition, yPosition, width, height, null);


if it matters i use booleans to control player movement
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2010-01-21 03:50:53 »

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  
public class MyThingy
{
    int x;
    int y;

    public void draw(Graphics g)
    {
        g.drawImage(myImage,x,y,null);
    }

    public void move(int xChange, int yChange)
    {
        x += xChange;
        y += yChange;
    }
}

public processInput(int keyCode)
{
    if (keyCode == KeyEvent.VK_LEFT)
    {
        myThing.move(-5,0);
    }
    else if (keyCode == KeyEvent.VK_RIGHT)
    {
        myThing.move(5,0);
    }
    if (keyCode == KeyEvent.VK_UP)
    {
        myThing.move(0,-5);
    }
    else if (keyCode == KeyEvent.VK_DOWN)
    {
        myThing.move(0,5);
    }
}


If you don't understand the above, it's time to go back to basics. Ask your teacher a lot more questions.

See my work:
OTC Software
Offline gfadmin

Senior Newbie





« Reply #2 - Posted 2010-01-21 04:17:04 »

ok i like that better for move the player but i want to know how to rotate the player.

so i can hold up and left and it will turn on a lets say 5 degree angle.

I found this for a rotate script but it doesnt rotate from the center point of the player.

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  
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

  public void paint(Graphics g) {

    g.fillRect(0, 0, 20, 20);

    Graphics2D g2 = (Graphics2D) g;

    g2.translate(50, 50);
    g2.rotate(30.0 * Math.PI / 180.0);

    g2.scale(2.0, 2.0);

    g.setColor(Color.red);

    g.fillRect(0, 0, 20, 20);

  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline gfadmin

Senior Newbie





« Reply #3 - Posted 2010-01-21 04:32:06 »

ok i figured it out but now im a bit confused.

heres my code to rotate

1  
2  
3  
4  
5  
6  
7  
public void rotatePlayer(Graphics g)
   {
      Graphics2D g2 = (Graphics2D) g;

       g2.translate(60, 45);
       g2.rotate(45.0 * Math.PI / 180.0);
   }


my question is what do i past to 'g' when i call upon 'rotatePlayer'
Offline zoto

Senior Member


Medals: 4



« Reply #4 - Posted 2010-01-21 14:46:19 »

Quote
my question is what do i past to 'g' when i call upon 'rotatePlayer'
You will need to use an AffineTransform.

1  
2  
import java.awt.geom.AffineTransform;
AffineTransform xForm = new AffineTransform();

1  
2  
3  
4  
xForm.setToIdentity();
xForm.translate(position.getX(), position.getY());
xForm.rotate(facing, width / 2, height / 2);
g.drawImage(img, xForm, null);
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2010-01-22 05:46:24 »

You will need to use an AffineTransform.

1  
2  
import java.awt.geom.AffineTransform;
AffineTransform xForm = new AffineTransform();

1  
2  
3  
4  
xForm.setToIdentity();
xForm.translate(position.getX(), position.getY());
xForm.rotate(facing, width / 2, height / 2);
g.drawImage(img, xForm, null);

He doesn't have to learn an AT, he can do exactly what he just did - rotate the graphics context.

gfadmin: translate to the center of your sprite, call rotate by the rotation amount, then translate back to the origin and finally draw the image.

See my work:
OTC Software
Offline broumbroum

Junior Member





« Reply #6 - Posted 2010-01-23 00:38:14 »

Quote
translate to the center of your sprite, call rotate by the rotation amount, then translate back to the origin and finally draw the image.
this is trans = (Graphics2D).getTransform() ; ....do g.translate g.rotate.... (Graphics2D).setTransform(trans)
 Smiley

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2010-01-23 03:38:54 »

Are you telling me that it ends up just calling that or that this is what you have to do?

Because you can definitely just call these:
http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html#translate(double,%20double)
http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html#rotate(double)

In fact, you can just call:
http://java.sun.com/javase/6/docs/api/java/awt/Graphics2D.html#rotate(double,%20double,%20double)

And combine both.

See my work:
OTC Software
Offline broumbroum

Junior Member





« Reply #8 - Posted 2010-02-06 23:08:55 »

Using Graphics2D direct transforms have issues that it can seriously damage the whole screen graphics if transforms are misused or in failure because it shares the peer-transform.
As in fact I don't have much to conceal between both methods, either choose Graphisc2D or AffineTransform to your fit. I DO prefer making AffineTransform on singular drawImage() callbacks.

What must be clear is the context of the matricial transform, that is if running from the peer graphics, (i.e. g.translate() , g.rot..) ensure to store the original context and translate correctly, because you have to restore the original state after making your painting as follows to getTransform() <-> setTransform(). Whenever an exception occurs between getTransform() <-> setTransform() you have to catch it and restore back with setTransform(toTheoriginalContextMatrixTransform). ... persecutioncomplex

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #9 - Posted 2010-02-07 01:48:55 »

Ah, well that makes sense. Coming from an OpenGL background, I'm used to worrying about not corrupting the matrix and being smart about glPushMatrix, glPopMatrix, and glLoadIdentity. So it doesn't seem unusual to me to have to transform back afterwards.

See my work:
OTC Software
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.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!