Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
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  
  face/turn the character in the direction the mouse is facing?  (Read 1412 times)
0 Members and 1 Guest are viewing this topic.
Offline nethious

Senior Newbie





« Posted 2012-05-09 17:25:46 »

hello! im working on my first ever game, and i want the player to be able to turn freely by pointing the mouse in a certain direction. I am definately not advanced with java yet, so ignore the dumb questions please  Tongue


another thing im trying to do is have the camera follow the player, so that the player is always in the middle of the screen, unless if there comming to the edge of the map.

any help is much appreciated, thanks!


EDIT: forgot to post any code, whoops

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  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
package game;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

public class Play extends BasicGameState{
   
   private Input input;
   
   TiledMap tm;
   int mapHeight, mapWidth;
   float playerX;
   float playerY;
   float playerCenterX;
   float playerCenterY;
   float playerRotation;
   float speed = 0.2f;
   int cameraX = 0;
   int cameraY = 0;
   private Image player;
   static int WIDTH = 800;
   static int HEIGHT = 600;
   Rectangle camera;
   
    public Play(int state){
         
    }
     
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
       camera = new Rectangle(0, 0, 800, 600);
      gc.setVSync(true);
      tm = new TiledMap("res/map1.tmx");
      player = new Image("res/character01.png");
      mapWidth = tm.getWidth()*tm.getTileWidth();
      mapHeight = tm.getHeight()*tm.getTileHeight();
      playerX = 400 - player.getWidth() / 2;
       playerY = 300 - player.getHeight() / 2;
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
       tm.render(0, 0);
       playerCenterX = playerX + player.getWidth() / 2;
       playerCenterY = playerY + player.getHeight() / 2;
       player.draw(cameraX + playerX, cameraY + playerY);
    }

   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
       input = gc.getInput();

       if (input.isKeyDown(Input.KEY_W)) {
            playerY -= speed * delta;
       }
       
       if (input.isKeyDown(Input.KEY_A)) {
            playerX -= speed * delta;
       }

       if (input.isKeyDown(Input.KEY_S)) {
            playerY += speed * delta;
       }
         
       if (input.isKeyDown(Input.KEY_D)) {
            playerX += speed * delta;
       }        
   }

   public int getID() {
        return 1;
         
    }
   
}
Offline jonjava
« Reply #1 - Posted 2012-05-09 18:12:31 »

I swear this question was asked and answered like yesterday.. but I can't find the thread.. maybe I'm crazy??

You want to figure out an offset based on the player and or the screen and draw the map at that offset.

As for the direction, you can use vectors! Vectors give you awesome superpowers!

Offline Rorkien
« Reply #2 - Posted 2012-05-09 18:47:01 »

I swear this question was asked and answered like yesterday.. but I can't find the thread.. maybe I'm crazy??

You want to figure out an offset based on the player and or the screen and draw the map at that offset.

As for the direction, you can use vectors! Vectors give you awesome superpowers!

No, you ain't crazy  Smiley

http://www.java-gaming.org/topics/2d-isometric-diamond-several-question-into-one-thread/26412/view.html
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sammyster

Senior Newbie


Medals: 1



« Reply #3 - Posted 2012-05-09 18:55:14 »

This is a good post aswell Smiley

http://www.java-gaming.org/topics/solved-rotation-following-mouse/24763/msg/210279/view.html
Offline Axeman

Senior Member


Medals: 7



« Reply #4 - Posted 2012-05-10 23:48:07 »

As for the direction, you can use vectors! Vectors give you awesome superpowers!

Hahaha true, true! Cheesy
Offline Roquen
« Reply #5 - Posted 2012-05-11 03:08:52 »

In 2D if vectors give you superpowers, then what do complex numbers give you...since vectors are effectively complex numbers with the interesting parts removed?
Offline sproingie

JGO Kernel


Medals: 202



« Reply #6 - Posted 2012-05-11 03:23:51 »

In 2D if vectors give you superpowers, then what do complex numbers give you

A complex.   Grin
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.

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

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

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

TehJavaDev (90 views)
2014-09-10 06:39:09

Tekkerue (44 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (48 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (37 views)
2014-09-07 01:10:19
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!