Simple Math Problem

What’s wrong with the distance formula? It’s extremely helpful and very simple. I think it’s great.

I don’t understand why it isn’t rendering. On line 606 of your pastebin console, the X location of the enemy is within range and the renderMe is set to true. On line 4812, it is set to false again. Just like it should.

This might sound REALLY stupid, but have you tried comparing one int against a another, larger int, just to see if the compare function works? Like:
It IS stupid. Look at that evil semicolon. Argh.


int i = 0;
if (i > 25); <---- Evil
{
	System.out.println("0 is greater than 25........WHAT?!");
}

This prints true on my computer ???. I have yet to figure out how to fix it, but I was wondering if you had the same issue. I’m thinking it might have to do with a corrupted JRE or something…


int i = 0;
if (i > 25);
{
	System.out.println("0 is greater than 25........WHAT?!");
}

Stick out your hand. * slaps it*
NO!
The semicolon on the end of your if-statement terminates it, turning the code below into a scope block

@Stumpy: don’t use sqrt. Squaring maxdistance is faster since we don’t care what the actual distance is, just if it’s lower than 400 units.

@anon951759: if you’re in eclipse try stepping through and watching the value of render me. I believe there was a way to set conditional breakpoints, so you could use one to detect when render me is true, then stepping through will reveal where your problem is.

Ouch. That’l teach me to try to write code fast. How did I miss that all weekend long?? :emo: Thanks for your help though :slight_smile:

I used it for the sake of clarity and left that part out as something he could figure out on his own. Please try and read everything someone says as the lack of communication can cause errors. I know that sometimes I type too fast. :smiley:

anon951759: Now I´m confused. First you stated that renderMe is always set to false in your if statement. Then you posted the result and you´re having a lot of true when it´s supposed to be true. So… Problem solved?

everybody makes that error once in a while.

it’s one of the harder errors to catch because it’s caused by one character, but is still syntactically correct.

one I tend to do is Stringx.replace(blah) instead of Stringx=Stingx.replace(blah).

@StumpyStrust
I somewhat understand what you’re doing there; you’re calculating how far away the enemy is from the player and then, if it’s further than the max distance, don’t render it… right? I’ll try that out in a second here.

Edit:
I’ve tried implementing what you’ve shown me into the code and it ended up working exactly the same as the code I was using before. The enemy only shows up outside of the 400 area around the player and he doesn’t show up if I go West or North.

// Decides whether to render the sprite or not.
        int xDiff = player.getXLocation() - xLocation;
        int yDiff = player.getYLocation() - yLocation;
        
        xDiff *= xDiff;
        yDiff *= yDiff;
        
        int distance = (int) Math.sqrt(xDiff+yDiff);
        if( distance > 400)
        {
            renderMe = false;
        }
        else
        {
            renderMe = true;
        }

@ctomi
I’ve switched my code with yours and now I can see that the code isn’t behaving at all as it should. If I move East or South, the enemy will eventually appear; but no matter how war West of North I go he won’t appear. The enemy also seems to only appear outside of the 400x400 area where he should only appear in.

Here is a log of me going East, then South, then West and then North.
http://pastebin.com/5rwubzxE

Here is the entire NetBeans project file. The two classes being used are NonPlayer.java (lines 31-56) & ScreenGame.java (line 205). If you want to test out the code for yourself just run the project.
http://www.mediafire.com/?s4zt4hv9898fqya

@ deepthought
I’m on NetBeans. =/ Thanks anyways!

@Everyone talking about a semicolon
I must have mis-typed an older version of this code before CnPing it here, the code I’m using doesn’t have the semicolon that you were all pointing out.

@Axeman
If you check out the mediafire link above and run the code then you’ll see what I mean about the code not working…

Thanks for all the help so far!

See my post above. I was didn’t see a semicolon in my code, and therefore I thought something with the JRE/Java had gone wrong. Ironically, I was tested it on another computer, and because I typed it out instead of copy-pasting it it, the semicolon wasn’t there, so (obviously) the code ran, making me even more suspicious of my java installation. In the end, it was just me mis-typing, and then not noticing, because I was so focused on the actual math. :stuck_out_tongue:

I didn’t mean print out the value of renderMe.

Put System.out.println(“a”), (“b”), (“c”), etc in every step of the game to see what letter it all stops.

Oh people stop that system.out nonsense - learn to use a debugger. You’l solve stuff like this in minutes using one…

That is how you debug when the console doesn’t show any problems.

Actually, you’re supposed to use the debugger full-time…with hotswapping the time difference for simple problems is negligible, and you’ll save a lot of time with more complex problems.

I disabled your culling, and noticed that even though they’re practically on top of each other their coordinates are drastically different, and the distance decreases as they get further away :o. looks like the player and nonplayer are using different frames of reference or something. Or your game is trying to make me look really stupid :persecutioncomplex:.

What eclipse theme is that?

O.o I noticed something like this in one of the tests last night but I thought the numbers were just wrong or something… I’m going to go check out all of the movement code and that for the player, they should definitely both be moving on the same coordinate system; I can’t think of many reasons for them being different but maybe if I add half of the screen’s height and width to the +400 & -400, that might just put the coordinates back together properly… The reason for this is that the map and everything on it move around the player who is always at the exact center of the screen.

my own weird-guy theme. Based partially off the theme from Liberty Basic, partially off the theme from FreeBasic, with a dark blue background for at night and because i happen to like that color.

It looks like my assumption of increasing the amount from 400 to around 1000 seems to have solved everything, now I just need to adjust the number for each direction and it should all work out. I’m not exactly sure if it’s solving the problem or just masking it somehow but it seems to be working at the moment.

Edit:
Well… For reasons unknown to me this code somehow works. The numbers don’t seem to fit in with anything else but they work. It took a bit of guesswork and a few tries but the numbers are just about perfect.

// Decides whether to render the sprite or not.
        renderMe = true;
        int x = player.getXLocation();
        int y = player.getYLocation();
        
        if ( xLocation < (x-30) ){
              renderMe = false;
        }
        if ( xLocation > (x+1250) ){
              renderMe = false;
        }
        if ( yLocation < (y-30) ){
              renderMe = false;
        }
        if ( yLocation > (y+1000) ){
              renderMe = false;
        }

what I do in openGL (not sure if there’s an equivalent in Java2d) is to render everything in it’s actual position after setting the transform to the negative player position.