Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (514)
games submitted by our members
Games in WIP (466)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Simple Math Problem  (Read 2453 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-04-08 23:43:34 »

I'm attempting to make a little if statement that decides whether to render a sprite to the screen or not. On paper it makes perfect sense but for some reason it's not working at all when I put it into code.
I've managed to get one of the equations to work, xLocation > (x+400), everything else just doesn't work no matter how I've tried it.

Code Snip:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
// Decides whether to render the sprite or not.
       int x = player.getXLocation();
        int y = player.getYLocation();
        if(xLocation > (x+400) || xLocation < (x-400) || yLocation < (y-400) || yLocation > (y+400))
        {
            renderMe = false;
        }
        else
        {
            renderMe = true;
        }


What I'm trying to do is check if the X and Y locations of the enemy sprite are within 400 in any cardinal direction of the player and if it is then render it, if not then don't render it. It's kind of like creating a box around the player and checking if the enemy is inside of it. The four equations I've made up are:

  • enemyYPosition < playerYPosition-400
  • enemyYPosition > playerYPosition+400
  • enemyXPosition < playerXPosition-400
  • enemyXPosition > playerXPosition+400

They stand for:

  • If the enemy is further than 400 South of the player.
  • If the enemy is further than 400 North of the player.
  • If the enemy is further than 400 West of the player.
  • If the enemy is further than 400 East of the player

I probably just messed up my math somehow, if anyone can see what I did wrong please say so.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-04-09 00:19:28 »

Wait, libgdx or some other library or java2d? Because the y axis is inverted in java2d but not in libgdx
Offline tyeeeee1
« Reply #2 - Posted 2013-04-09 00:20:22 »

Wait, libgdx or some other library or java2d? Because the y axis is inverted in java2d but not in libgdx

Java2D, I don't use any non-standard libraries atm.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #3 - Posted 2013-04-09 00:23:13 »

What exactly is the problem? It's rendering when it's not supposed to or it's not rendering when it's supposed to?
Offline tyeeeee1
« Reply #4 - Posted 2013-04-09 00:25:22 »

What exactly is the problem? It's rendering when it's not supposed to or it's not rendering when it's supposed to?

The code is always setting renderMe to false, even when it shouldn't; the only time when renderMe is set to true is when the enemy is at least 400 to the West of the player.

Edit: Here is a picture of what I'm trying to do. Render the enemy if it's within the green area and don't render it if it's in the yellow area. http://i.imgur.com/Jb46jTD.png
Offline Axeman

Senior Member


Medals: 7



« Reply #5 - Posted 2013-04-09 00:50:33 »

Have you debugged your code so you know that all location values are correct?
Offline tyeeeee1
« Reply #6 - Posted 2013-04-09 00:55:24 »

Have you debugged your code so you know that all location values are correct?

As far as I can see from this test I saved, they are.

Quote
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837
Player xLocation: 582  Player yLocation: 437
182   982   37   837

The odd rows show the player's location, both X and Y. The even rows show x-400, x+400, y-400, y+400.


Edit: Just tested with the enemy's location instead of the player's and it seems that the enemy only appears when it's 300 away from the player. I'm not sure why it's 300 instead of the 400. Bleh, this is confusing.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #7 - Posted 2013-04-09 00:57:28 »

This doesn't make any sense. You say that your xLocation variable is the player x and your yLocation variable is the player y. However, your "x" variable is already your player x and your "y" variable is also already your player y...
Offline tyeeeee1
« Reply #8 - Posted 2013-04-09 01:00:58 »

This doesn't make any sense. You say that your xLocation variable is the player x and your yLocation variable is the player y. However, your "x" variable is already your player x and your "y" variable is also already your player y...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
int x = player.getXLocation();
        int y = player.getYLocation();
        System.out.println(xLocation+"   "+yLocation);
        if(xLocation > (x+400))
        {
            renderMe = false;
        }
        else
        {
            renderMe = true;
        }


The x and y variables are the player's x and y locations. The xLocation and yLocation variables are the enemy's locations.
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 121
Projects: 4
Exp: 3 years



« Reply #9 - Posted 2013-04-09 01:02:17 »

Well then, it might help to print the enemy locations... Roll Eyes
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #10 - Posted 2013-04-09 01:07:04 »

Well then, it might help to print the enemy locations... Roll Eyes

Here's a readout of all of the data including the enemy's positions and whether the enemy is visible or not; this is only testing the X axis. http://pastebin.com/ngc8tXu0
Offline deepthought
« Reply #11 - Posted 2013-04-09 01:47:02 »

Your if-statements look correct. could anything be messing with renderMe between your if-statement and the rendering?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline tyeeeee1
« Reply #12 - Posted 2013-04-09 01:53:26 »

Your if-statements look correct. could anything be messing with renderMe between your if-statement and the rendering?

As far as I can see there is nothing that could be, here is a snip of the class where renderMe is used to decide whether to render the sprite or not.

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  
if(nonPlayer.getRenderMe())
        {
            g.drawImage(resource.getSprite(0, playerDirection, spriteCounter), nonPlayer.getXLocation(), nonPlayer.getYLocation(), null);
           
            if (!playerIsWalking)
            {
                spriteCounter = 0;
                spriteCounterTwo = 0;
            }
            else
            {
                if(spriteCounter == 0)
                {
                    spriteCounter = 1;
                }
                else if (spriteCounter == 1 && spriteCounterTwo >= 15)
                {
                     spriteCounter++;
                     spriteCounterTwo = 0;
                }
                else if (spriteCounter == 2 && spriteCounterTwo >= 15)
                {
                    spriteCounter--;
                    spriteCounterTwo = 0;
                }
            }
        }


In the one circumstance, which I have previously mentioned, where the code actually does work, this snip works perfectly.
Offline vbrain
« Reply #13 - Posted 2013-04-09 02:15:59 »

Add system.out.println statements to display the value of renderMe after the if statement, and add some in every step of your drawing function to see where things stop.
Offline tyeeeee1
« Reply #14 - Posted 2013-04-09 02:22:53 »

Add system.out.println statements to display the value of renderMe after the if statement, and add some in every step of your drawing function to see where things stop.

I added one in the render code and after it, they're always the same. Putting them in the various steps of the render code wouldn't show anything other than true because the render code only runs when renderMe is true.

I know the way that I have everything set up and no matter how I think about this, there should be no reason for this to not be working. It's getting the position of the player, doing some simple math, checking if a few values are higher or lower than others and then changing a boolean. The fact that this isn't working is just weird to me...
Offline StumpyStrust
« Reply #15 - Posted 2013-04-09 02:35:55 »

...this is 2d so we are basically going to be doing culling or w/e it is called in 3d but in 2d.

Since you are using java2d which has some great utilities you should use.

If you want a circle around your player of visible enemies that is very easy.

http://www.purplemath.com/modules/distform.htm

If you want a square then for Gods sake use shapes/squares/rectangles that java2d gives you. They have a nice intersects method. I could post the code for both but that would not really help you learn so take this and have at it.

Offline tyeeeee1
« Reply #16 - Posted 2013-04-09 02:40:32 »

Oh god... not the distance formula again. *shudders in fear* We just finished using that awhile ago. Thanks for the link, I'll check it over.

I would use the shapes but it seemed easier just to figure everything out without them, it's worked quite well so-far.
Offline actual

JGO Coder


Medals: 24



« Reply #17 - Posted 2013-04-09 02:41:07 »

I wonder if one thing you can do is put the player in the middle of the screen and then place an enemy at every single pixel on the screen. If renderMe is true then draw a red pixel for that enemy, if renderMe is false then draw a gray one. This will give you the full picture of where your algorithm is working and where it isn't.

You don't necessarily have to put an actual enemy, just iterate through all of the points on the screen.
Offline StumpyStrust
« Reply #18 - Posted 2013-04-09 02:52:25 »

Don't do any of that. I will help with how the distance formula works.

The idea is to create a triangle and then find the hypotenuse.

So some psudo code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
public boolean distanceCheck( loc1x, loc1y, loc2x, loc2y, maxDistance)
{
      int xDiff = (loc1x-loc2x);
      xDiff *= xDiff //square it.
     int yDiff = (loc1y-loc2y);
      yDiff *= yDiff;//square it
     int distance = Math.sqrt(xDiff+yDiff);
      if(distance > maxDistance)
          return false; //not in the circle.
     else
          return true;
}


Note that, that all can be done in one line but I use many for the sake of clarity. There is also an optimization that is not all necessary but might be something to look into. You will probably figure it out. Basically, stay away from anything that is not addition, subtraction, or multiplication.

Offline ctomni231

JGO Ninja


Medals: 71
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #19 - Posted 2013-04-09 03:22:36 »

Even though I agree with StumpyStrust, the code snippet you posted should work. If I were you, I'd separate the statements and figure out exactly which one is messing up.

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  
         // Decides whether to render the sprite or not.
       int x = player.getXLocation();
        int y = player.getYLocation();
       
        //Always try to render the enemy. Print out the player and enemy locations.
       renderMe = true;
        System.out.println("Player Location: ("+x+","+y+")");
        System.out.println("Enemy Location: ("+xLocation+","+yLocation+")");
       
        //Put each test case into the "fake" debugger to see which ones trigger. Same code but stretched out.
       if ( xLocation < (x-400) ){
              System.out.println("Render Fail: Enemy Too Far West!");
              renderMe = false;
        }
        if ( xLocation > (x+400) ){
              System.out.println("Render Fail: Enemy Too Far East!");
              renderMe = false;
        }
        if ( yLocation < (y-400) ){
              System.out.println("Render Fail: Enemy Too Far North!");
              renderMe = false;
        }
        if ( yLocation > (y+400) ){
              System.out.println("Render Fail: Enemy Too Far South!");
              renderMe = false;
        }

        //Split up the readout... so that you can read it easier.
       System.out.println("-------------------------------------");

         
Replace this code snippet with the one you currently have. This should be able to tell you exactly where your code is failing. (Either that, or the code will work...) Either way, it should be much more informative in figuring out what is going wrong, because that code is 100% accurate in my book.

Offline Sammidysam
« Reply #20 - Posted 2013-04-09 03:55:34 »

Oh god... not the distance formula again. *shudders in fear* We just finished using that awhile ago. Thanks for the link, I'll check it over.

I would use the shapes but it seemed easier just to figure everything out without them, it's worked quite well so-far.

What's wrong with the distance formula?  It's extremely helpful and very simple.  I think it's great.
Offline Longarmx
« Reply #21 - Posted 2013-04-09 03:57:00 »

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.

Offline OpenGLShaders

Senior Newbie


Exp: 2 years



« Reply #22 - Posted 2013-04-09 04:06:47 »

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.
1  
2  
3  
4  
5  
int i = 0;
if (i > 25); <---- Evil
{
   System.out.println("0 is greater than 25........WHAT?!");
}

This prints true on my computer Huh. 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......
Offline deepthought
« Reply #23 - Posted 2013-04-09 04:35:05 »

1  
2  
3  
4  
5  
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.

@tyee111: 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.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline OpenGLShaders

Senior Newbie


Exp: 2 years



« Reply #24 - Posted 2013-04-09 04:46:10 »

1  
2  
3  
4  
5  
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

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 Smiley
Offline StumpyStrust
« Reply #25 - Posted 2013-04-09 05:25:55 »


Note that, that all can be done in one line but I use many for the sake of clarity. There is also an optimization that is not all necessary but might be something to look into. You will probably figure it out. Basically, stay away from anything that is not addition, subtraction, or multiplication.

@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

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.  Cheesy

Offline Axeman

Senior Member


Medals: 7



« Reply #26 - Posted 2013-04-09 12:57:53 »

tyeeeee1: 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?
Offline deepthought
« Reply #27 - Posted 2013-04-09 14:04:12 »

1  
2  
3  
4  
5  
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

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 Smiley

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).

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline tyeeeee1
« Reply #28 - Posted 2013-04-09 17:35:23 »

@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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
// 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!
Offline OpenGLShaders

Senior Newbie


Exp: 2 years



« Reply #29 - Posted 2013-04-09 17:53:10 »


@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.


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. Tongue
Pages: [1] 2
  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.

xsi3rr4x (72 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (79 views)
2014-04-14 17:39:50

UprightPath (65 views)
2014-04-14 17:35:47

Porlus (80 views)
2014-04-14 15:48:38

tom_mai78101 (104 views)
2014-04-10 04:04:31

BurntPizza (164 views)
2014-04-08 23:06:04

tom_mai78101 (260 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!