Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Help discover a more elegant way to calculate "edge perspective"  (Read 1972 times)
0 Members and 1 Guest are viewing this topic.
Offline joeyismusic

Junior Devvie





« Posted 2012-04-18 15:16:05 »

Ok typically in a tile engine, you have a number of tiles, a character, and a viewport. You'll usually draw only the tiles that are needed (the amount that can fit within the viewport, plus 1 tile edge around to prevent "black out" tiles on the edges)

Most of the time when you get this up and going, and you locate your character to position 0,0... 25% of your screen will be the tiles in the tile map, and the other 75% will be blackness.

Now most rpg's on SNES would never let you get to an edge of a map, but if you did, there was a "edge perspective" algorithm going on, to stop the tile map from scrolling and start the player sprite scrolling.
So if you were at the top left corner of a map, your sprite would appear at (0, 0) and the map would start drawing at pixel 0,0. You move right one position, your sprite is now 1, 0, and the map is being drawn still at 0,0 (top left corner of viewport).

Hopefully that makes sense... That said, here's my drawing function which calculates this. I am hoping to find someone to offer a more elegant way of doing this. I'm not that great with the math, so I have some conditional branching to make things easier.

Let me know your thoughts!

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  
   public void draw_edge_persepctive(Graphics2D g, Sprite sprite, int x, int y, int offsetx, int offsety, int width, int height) {
      //x and y are in tile coordinate system
      //offsetx and offsety are in pixel coordinate system
      //width and height are in tiles coordinate system (how many tiles wide and high is the viewport)
      //hw is "half width" offset by 1
      //hh is "half height" offset by 1
      //our drawing area of tiles to draw is a rectangle
      //xa is the left edge of that rectangle of tiles to draw
      //xb is the right edge
      //ya is the top edge
      //yb is the bottom edge
     
      int hw = (width - 1) / 2;
      int hh = (height - 1) / 2;
      int xa = x - hw;
      int xb = x + hw;
      int ya = y - hh;
      int yb = y + hh;
     
      //sx, sy is the sprite's or player's position that we will calculate to draw on the screen
      //this will change based on how far away we are from the edges of the map
     
      int sx = 0;
      int sy = 0;
      if(x < 255 - hw) {
         sx = Math.min((x * 32) - offsetx, hw * 32);
      }
      else {
         sx = Math.max(hw * 32,-offsetx + ((width - 1) - (255 - x)) * 32);
      }
     
      if(y < 255 - hh) {
         sy = Math.min((y * 32) - offsety, hh * 32);
      }
      else {
         sy = Math.max(hh * 32,-offsety + ((height - 1) - (255 - y)) * 32);
      }
     
      //rx, ry is how many pixels to subtract from drawing location on the screen
      //we need this because when we are close to an edge, we want to "lock" the tiles to the viewport
      //at least until we are far enough away from an edge to draw "normally"
     
      int rx = 0;
      int ry = 0;
      if(x < hw) rx = -offsetx + (x - hw) * 32;
      if(y < hh) ry = -offsety + (y - hh) * 32;
      if(x == hw && offsetx > 0) rx = -offsetx;
      if(y == hh && offsety > 0) ry = -offsety;
     
      if(x > 255 - hw) rx = -offsetx + (hw - (255 - x)) * 32;
      if(y > 255 - hh) ry = -offsety + (hh - (255 - y)) * 32;
      if(x == 255 - hw && offsetx < 0) rx = -offsetx;
      if(y == 255 - hh && offsety < 0) ry = -offsety;
     
      if(rx < 0) xb += hw;
      if(ry < 0) yb += hh;
      if(rx > 0) xa += -hw;
      if(ry > 0) ya += -hh;
     
     
      for(int ix = xa - 1; ix <= xb + 1; ix++) {
         for(int iy = ya - 1; iy <= yb + 1; iy++) {
            if(ix < 0 || ix >= 256 || iy < 0 || iy >= 256) continue;
            TileType tile = TileType.tile_types[tiles[ix][iy][0]];
           
            int dx = offsetx + (32 * (ix - x + hw));
            int dy = offsety + (32 * (iy - y + hh));
           
            tile.draw(g, rx + dx, ry + dy, 2);
         }
      }
     
      sprite.draw(g, sx, sy, 2);
   }
Offline joeyismusic

Junior Devvie





« Reply #1 - Posted 2012-04-18 15:34:36 »

Here's what it looks like, in case my explanation was poor...

http://youtu.be/tqpaGvkySe4
Offline sproingie

JGO Kernel


Medals: 202



« Reply #2 - Posted 2012-04-18 15:39:33 »

Seems to me it's a matter of "don't scroll the map if there's no more map".  If your character is represented with a map position and not simply glued to the center, it should automatically Do The Right Thing as far as moving the character on the screen when the map freezes.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline joeyismusic

Junior Devvie





« Reply #3 - Posted 2012-04-18 15:44:28 »

I should probably mention that this is tile based movement.
Offline DrZoidberg

Senior Devvie


Medals: 17



« Reply #4 - Posted 2012-04-18 16:48:40 »

Try out this program

http://pastebin.com/v0nV5Vsr

edit: corrected a small error in the program
Offline joeyismusic

Junior Devvie





« Reply #5 - Posted 2012-04-18 17:07:27 »

Hmm, the problem I see with what you're doing is you're relying on being able to draw the entire map, which won't be possible in my case.

Perhaps I can still use this example, somehow.
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.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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
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!