Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) 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 3941 times) 0 Members and 1 Guest are viewing this topic.
tyeeeee1
 « Posted 2013-04-08 21: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.
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #1 - Posted 2013-04-08 22:19:28 »

Wait, libgdx or some other library or java2d? Because the y axis is inverted in java2d but not in libgdx
tyeeeee1
 « Reply #2 - Posted 2013-04-08 22: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!
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #3 - Posted 2013-04-08 22: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?
tyeeeee1
 « Reply #4 - Posted 2013-04-08 22: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
Axeman

Senior Devvie

Medals: 7

 « Reply #5 - Posted 2013-04-08 22:50:33 »

Have you debugged your code so you know that all location values are correct?
tyeeeee1
 « Reply #6 - Posted 2013-04-08 22: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.
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #7 - Posted 2013-04-08 22: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...
tyeeeee1
 « Reply #8 - Posted 2013-04-08 23: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.
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #9 - Posted 2013-04-08 23:02:17 »

Well then, it might help to print the enemy locations...
 Games published by our own members! Check 'em out!
tyeeeee1
 « Reply #10 - Posted 2013-04-08 23:07:04 »

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

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
deepthought
 « Reply #11 - Posted 2013-04-08 23: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
tyeeeee1
 « Reply #12 - Posted 2013-04-08 23: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.
vbrain
 « Reply #13 - Posted 2013-04-09 00: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.
tyeeeee1
 « Reply #14 - Posted 2013-04-09 00: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...
StumpyStrust
 « Reply #15 - Posted 2013-04-09 00: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.

tyeeeee1
 « Reply #16 - Posted 2013-04-09 00: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.
actual

JGO Coder

Medals: 24

 « Reply #17 - Posted 2013-04-09 00: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.
StumpyStrust
 « Reply #18 - Posted 2013-04-09 00: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.

ctomni231

JGO Wizard

Medals: 99
Projects: 1
Exp: 7 years

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

 « Reply #19 - Posted 2013-04-09 01: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.

Sammidysam
 « Reply #20 - Posted 2013-04-09 01: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.
Longarmx
 « Reply #21 - Posted 2013-04-09 01: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.

OpenGLShaders

Junior Devvie

Medals: 4
Exp: 2 years

 « Reply #22 - Posted 2013-04-09 02: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 . 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......
deepthought
 « Reply #23 - Posted 2013-04-09 02: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
OpenGLShaders

Junior Devvie

Medals: 4
Exp: 2 years

 « Reply #24 - Posted 2013-04-09 02: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??  Thanks for your help though
StumpyStrust
 « Reply #25 - Posted 2013-04-09 03: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.

Axeman

Senior Devvie

Medals: 7

 « Reply #26 - Posted 2013-04-09 10: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?
deepthought
 « Reply #27 - Posted 2013-04-09 12: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??  Thanks for your help though

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
tyeeeee1
 « Reply #28 - Posted 2013-04-09 15: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!
OpenGLShaders

Junior Devvie

Medals: 4
Exp: 2 years

 « Reply #29 - Posted 2013-04-09 15: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.
Pages: [1] 2
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ClaasJG (18 views) 2015-04-27 13:36:51 BurntPizza (33 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (50 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (42 views) 2015-04-14 20:13:12 Riven (45 views) 2015-04-12 21:36:37 bus hotdog (61 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (71 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x wessles 15x alwex 11x 65K 11x kingroka123 11x kevglass 8x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Riven 7x chrislo27 7x Hanksha 7x KevinWorkman 6x KudoDEV 4x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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