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  
  What's wrong with this isometric rendering code?  (Read 1226 times)
0 Members and 1 Guest are viewing this topic.
Offline Jacob_

Junior Member


Projects: 3



« Posted 2010-11-02 22:03:16 »

This is supposed to draw a heightmap to the screen using isometric 3D (has not yet been optimized).

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  
public class IsometricRenderer {
    private int scaleFactor = 10;
    private double xOrigin = 0;
    private double yOrigin = 0;
    private final double angle = 0.46365;
    private final double a = Math.cos(angle);
    private final double b = Math.sin(angle);
    private int worldXToScreenX(double x, double y, double z)
    {
        int xC = (int) Math.round((x * scaleFactor - z * scaleFactor) * a);
        int xS = (int) Math.round(xC + xOrigin * scaleFactor);
        return xS;
    }
    private int worldYToScreenY(double x, double y, double z)
    {
        int yC = (int) Math.round((x * scaleFactor + z * scaleFactor) * b + y * scaleFactor);
        int yS = (int) Math.round(-yC + yOrigin * scaleFactor);
        return yS;
    }
    public double getScaleFactor()
    {
        return scaleFactor;
    }
    public void translateCam(double tx, double ty)
    {
        xOrigin += tx;
        yOrigin += ty;
    }
    public void setCam(double x, double y)
    {
        xOrigin = x;
        yOrigin = y;
    }
    private Point getScreenPoint(double x, double y, double z)
    {
        return new Point(worldXToScreenX(x, y, z), worldYToScreenY(x, y, z));
    }
    public void drawWorld(World w, Graphics2D g2)
    {
        for(int x = 0; x < w.width; x ++)
        {
            for(int z = 0; z < w.height; z ++)
            {
                int h = w.heightmap[x][z];
                Point p = getScreenPoint(x, h, z);

                int rightX = x + 1;
                int frontZ = z - 1;
                if(x >= w.width - 1)
                    rightX = x;
                if(frontZ < 0)
                    frontZ = z;
               
                Point right = getScreenPoint(x + 1, w.heightmap[rightX][z], z);
                Point front = getScreenPoint(x, w.heightmap[x][frontZ], z - 1);
                Point frontRight = getScreenPoint(x, w.heightmap[rightX][frontZ], z - 1);

                g2.setColor(Color.gray);
                /*
                int[] xP = new int[]{p.x, right.x, frontRight.x, front.x};
                int[] yP = new int[]{p.y, right.y + 1, frontRight.y, front.y};
                g2.fillPolygon(xP, yP, xP.length);
                g2.setColor(Color.black);*/

                g2.drawLine(p.x, p.y, front.x, front.y);
                g2.drawLine(p.x, p.y, right.x, right.y);
            }
        }
    }
}


It works almost perfectly, but the lines are uneven:



Anyone spot the problem? According to some stuff I read online, the angle I used is supposed to make pretty lines.
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2010-11-02 23:08:09 »

Maybe use floor() instead of round()

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Jacob_

Junior Member


Projects: 3



« Reply #2 - Posted 2010-11-02 23:18:47 »

I replaced all the round() calls with floor(), but it still looks funky.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dbotha

Senior Newbie





« Reply #3 - Posted 2010-11-03 00:17:29 »

If it helps I used your code exactly and got the following grid. It appears correct to me (unless I'm missing something).
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2010-11-03 00:20:10 »

Could just be that you're looking at too low granularity. With higher resolution it might be fine. I always use a straight int cast for stuff like this (basically equivalent of floor).

See my work:
OTC Software
Online Riven
« League of Dukes »

JGO Overlord


Medals: 746
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-11-03 00:34:17 »

Maybe turn own the OpenGL pipeline

-Dsun.java2d.opengl=false

More at: http://download.oracle.com/javase/1.5.0/docs/guide/2d/flags.html

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline dbotha

Senior Newbie





« Reply #6 - Posted 2010-11-03 00:40:57 »

Yup Riven is spot on, I turned on the OpenGL pipeline and the lines match yours.
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 (19 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!