Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 971 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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
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!
Try the Free Demo of Revenge of the Titans
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
« League of Dukes »

JGO Kernel


Medals: 39
Projects: 12


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
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


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
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (76 views)
2013-05-17 21:29:12

alaslipknot (89 views)
2013-05-16 21:24:48

gouessej (117 views)
2013-05-16 00:53:38

gouessej (113 views)
2013-05-16 00:17:58

theagentd (125 views)
2013-05-15 15:01:13

theagentd (112 views)
2013-05-15 15:00:54

StreetDoggy (156 views)
2013-05-14 15:56:26

kutucuk (178 views)
2013-05-12 17:10:36

kutucuk (178 views)
2013-05-12 15:36:09

UnluckyDevil (186 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.189 seconds with 21 queries.