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  
  2D rendering, but with a 3D coordinate system?  (Read 5804 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2012-06-26 02:25:41 »

I want to make a game that has 2D based sprites, but with a 3 dimensional world that can be walked around in, sort of like Double Dragon.

I am looking for a simple way to draw 2D sprites in 3 dimensions, but I don't think I need something as high-grade as OpenGL. Is there some simple library or trick I can use to accomplish this?

Thanks a bunch,
 | Nathan

Check out my website!
Offline Cero
« Reply #1 - Posted 2012-06-26 02:43:30 »

you dont have a third dimension per se. just like in an isometric game, like sim city, you have a Z so to speak, telling the renderer in what order stuff is rendered

Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2012-06-26 02:52:48 »

Hmm. Okay. I'm gonna have to do some testing to figure this out Cheesy

Thanks,
 | Nathan

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2012-06-26 02:57:52 »

technically you can do this with opengl depth stuff or whatever

I would just give every sprite a Z value and sort sprites by that value, or have it calculated by the Y value and then render
should be straight forward

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2012-06-26 03:35:04 »

For this, I had the typical X/Y values and I also had a third depth value. I didn't call it Z just because it was kind of a fudged BS this. Pressing up on the keyboard increased depth and down decreased it. When jumping, Y increased or decreased.

Might be simpler to call X and Y what they are, and have like jumpHeight or something as your third value. In terms of draw position, it should be normal Y + jumpHeight. For collision detection, you need to look for collisions both along the normal X/Y, and also along the jumpHeight. Pretty easy.

See my work:
OTC Software
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2012-06-26 03:39:42 »

Meh. One of the problems with games like Double Dragon is that it's usually pretty difficult to tell the alignment on the y or z axis (depending on what system you use). So your kick might go right in front or behind the enemy.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #6 - Posted 2012-06-26 04:41:55 »

Here's what I went with (for now).

 | Nathan

Check out my website!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #7 - Posted 2012-06-26 04:45:08 »

Cool. But as I said, the alignment problem. Would it be possible to put a square on the ground where the character is walking, and where enemies walk?
Offline theagentd
« Reply #8 - Posted 2012-06-26 05:56:20 »

EDIT: Just tried the demo, and this isn't very relevant I guess...

It's pretty easy. Most 3D games want to render 2D sprites that always face the screen since that's how you render particles! You'll have to project your sprite positions into view space and then draw your quad there.

1. Read back (or calculate) your OpenGL modelview matrix into a Matrix4f.
2. Reset the modelview matrix with glLoadIdentity() so that vertices are only transformed by the projection matrix.

For each 3D sprite:
3. Load the sprite's position into a Vector4f.
4. Transform the sprite's position by the modelview matrix using
    Matrix4f.transform(modelviewMatrix, spritePosition, spritePosition);
5. Draw the sprite's quad relative to the transformed sprite position. The coordinates for the top left and bottom right corners:
    x1 = spritePosition.x - width/2
    y1 = spritePosition.y - height/2
    z1 = spritePosition.z

    x2 = spritePosition.x + width/2
    y2 = spritePosition.y + height/2
    z2 = spritePosition.z



To understand why this works you'll need to understand how OpenGL transforms 3D coordinates into screen coordinates. The modelview matrix takes coordinates from world space to view space. In view space the "camera" is always located at (0, 0, 0) looking towards -z (I think Tongue). By creating our quad here we can guarantee that it always faces the screen, but it will still be processed by the projection matrix so it also gets smaller the farther away it is, etc.

Myomyomyo.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #9 - Posted 2012-06-26 07:06:10 »

Here's my only real question, now:

How can I go about sorting a list based on a variable, z?

I want to render everything starting at the largest z value and ending with the smallest. What would be the best way to go about doing this?

 | Nathan

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kryel

Senior Newbie





« Reply #10 - Posted 2012-06-26 07:24:29 »

I'm new, but since I have also a "Z-axis", here's my solution (maybe wrong ! so people might correct me  Grin ) :

I just intend to make a Class that'll act as some sort of a pool (storing everything that need to be drawn) and add a <Comparable> option for the Z value in it.
So in the end I'll just have to "sort" the object before drawing.

Edit :
Huho, I didn't notice you were talking about a LIST.
If that's the case I'm pretty sure that this collection is already able to sort String & Integers just fine with a build-in method, right..?
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #11 - Posted 2012-06-26 07:33:31 »

I don't know how efficient this is, and I'm sure there's a better way, but here's what I came up with.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
      List<Entity> renderOrderList = new ArrayList<Entity>();
      List<Entity> backupList = new ArrayList<Entity>();
     
      for (Entity entity : entities) backupList.add(entity);
     
      for (int i = 0; i < entities.size(); i++) {
         Entity e = null;
         
         for (Entity entity2 : backupList) {
            if (e == null) e = entity2;
            if (entity2.z > e.z) e = entity2;
         }
         
         renderOrderList.add(e);
         backupList.remove(e);
      }
     
      for (Entity entity : renderOrderList) entity.render();


Which yields this result.

Feedback?

 | Nathan

Check out my website!
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #12 - Posted 2012-06-26 07:48:24 »

Cool. But as I said, the alignment problem. Would it be possible to put a square on the ground where the character is walking, and where enemies walk?

Yeah, I plan to do something like that.

 | Nathan

Check out my website!
Offline theagentd
« Reply #13 - Posted 2012-06-26 07:55:47 »

What's wrong with Arrays.sort() or Collections.sort()?

Myomyomyo.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #14 - Posted 2012-06-26 08:08:55 »

What's wrong with Arrays.sort() or Collections.sort()?

I don't quite understand how to use Collections.sort().

How do I tell it to sort by the z variable present in the Entity class?

 | Nathan

Check out my website!
Offline davedes
« Reply #15 - Posted 2012-06-26 08:12:57 »

Comparator/Comparable:
http://javarevisited.blogspot.ca/2011/06/comparator-and-comparable-in-java.html

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #16 - Posted 2012-06-26 22:27:25 »

What you're doing is fine. I doubt you'll ever have enough on screen to break your processor.

To make a bit more efficient, you can make it a binary sort, and you can only move entities around if they've changed Z.

See my work:
OTC Software
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #17 - Posted 2012-06-27 02:55:13 »

What you're doing is fine. I doubt you'll ever have enough on screen to break your processor.

To make a bit more efficient, you can make it a binary sort, and you can only move entities around if they've changed Z.

Ah, okay.

How about this: a 2D image, but rotated in the third dimension, like these windows.

I want to have 2D tiles, but I want them to be rotated so you can tell that you are in a semi-3d environment. How can I go about doing this?

 | Nathan

Check out my website!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #18 - Posted 2012-06-27 20:27:19 »

What you're doing is fine. I doubt you'll ever have enough on screen to break your processor.

To make a bit more efficient, you can make it a binary sort, and you can only move entities around if they've changed Z.

Ah, okay.

How about this: a 2D image, but rotated in the third dimension, like these windows.

I want to have 2D tiles, but I want them to be rotated so you can tell that you are in a semi-3d environment. How can I go about doing this?

 | Nathan
What, isometric tiling? Not really sure what you're asking.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #19 - Posted 2012-06-28 02:19:44 »

So, I have this grass tile



and instead of looking at it like that, which would be a top-down perspective, I want to tilt it away from the camera to give the illusion of a three dimensional world.

I'll use pictures from Minecraft as an example:

Instead of the grass looking like this



I want it to look this this



 | Nathan

EDIT: Do you think it's time for me to move on to OpenGL? I think I'm starting to get to the point where my ideas outrun the capabilities of Java2D.

Check out my website!
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #20 - Posted 2012-06-28 02:43:54 »

Yeah, you might want to go legit 3D. Unless you want to make custom textures, put them in Minecraft, and screenshot each one Grin
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #21 - Posted 2012-06-28 21:45:50 »

The easiest way to do fake-looking isometric 2D is to rotate everything 45ยบ, then scale its Y. Your example isn't exactly an isometric approach, but it's the same idea. If you look at those shapes, they're basically trapezoids. So if you draw your quad and skew the rear 2 vertices / squish the Y, you'll get the same effect.

But then you're kind of writing your own 3D camera, so you might as well go all 3D if you understand that reasonably well.

See my work:
OTC Software
Offline DrZoidberg

Senior Member


Medals: 15



« Reply #22 - Posted 2012-07-14 07:59:14 »

Writing your own 3D camera is not very complicated.
Look for example at this software renderer I wrote.
http://ubuntuone.com/5vRmp4xsKyO7h6DJZ6bEuJ

You can move around with w,a,s,d,q,e,space,c and look around by clicking and dragging the mouse.
Offline pitbuller
« Reply #23 - Posted 2012-07-14 12:42:38 »

O(n^2) sorting might be ok in this particular case but in general its just plain wrong. Just learn to use collections.sort to get faster, cleaner and better code. Algorithm level optimizations are something that everyone should know.
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.

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

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

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

E.R. Fleming (31 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 (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
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!