Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 39

Thread: Java light source simulation

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Java light source simulation

    I'm trying to make a light source simulation and it almost works. It's a 2D simulation, there are wall tiles in front of a lamp. When removing a wall tile the light shines through the hole. When removing a tile it works. But I don't know how to update it correctly when placing a wall. When removing or placing a wall, the updateLight method is called.

    public static void updateLight(int x, int y)
    	{
    		byte myLight = getLight(x, y);
     
    		if (getWall(x, y) == null) //Checks if there's a wall behind, if not it sets the light at this position to the max light and updates spaces next to this space.
    		{
    			byte lightPower = 6;
    			setLight(x, y, lightPower);
    			if (getLight(x - 1, y) < lightPower-1)
    			{
    				setLight(x - 1, y, (byte)(lightPower-1));
    				if (getWall(x - 1, y) != null)
    					updateLight(x - 1, y);
    			}
    			if (getLight(x + 1, y) < lightPower-1)
    			{
    				setLight(x + 1, y, (byte)(lightPower-1));
    				if (getWall(x + 1, y) != null)
    					updateLight(x + 1, y);
    			}
    			if (getLight(x, y - 1) < lightPower-1)
    			{
    				setLight(x, y - 1, (byte)(lightPower-1));
    				if (getWall(x, y - 1) != null)
    					updateLight(x, y - 1);
    			}
    			if (getLight(x, y + 1) < lightPower-1)
    			{
    				setLight(x, y + 1, (byte)(lightPower-1));
    				if (getWall(x, y + 1) != null)
    					updateLight(x, y + 1);
    			}
    		}
    		else
    		{
    			if (myLight > 12) // If there's a wall behind it shouldn't be able to have the max light.
    			{
    				setLight(x, y, (byte) 12);
    			}
    			if (myLight > 0) //If the own light is greater than 0 it checks if the spaces next to it are less lit then this space - 1. If so, it changes the light to the own light -1 and updates it.
    			{
    				byte light = getLight(x, y);
    				if (getLight(x - 1, y) < light - 1)
    				{
    					setLight(x - 1, y, (byte) (light - 1));
    					if (getWall(x - 1, y) != null)
    						updateLight(x - 1, y);
    				}
    				if (getLight(x + 1, y) < light - 1)
    				{
    					setLight(x + 1, y, (byte) (light - 1));
    					if (getWall(x + 1, y) != null)
    						updateLight(x + 1, y);
    				}
    				if (getLight(x, y - 1) < light - 1)
    				{
    					setLight(x, y - 1, (byte) (light - 1));
    					if (getWall(x, y - 1) != null)
    						updateLight(x, y - 1);
    				}
    				if (getLight(x, y + 1) < light - 1)
    				{
    					setLight(x, y + 1, (byte) (light - 1));
    					if (getWall(x, y + 1) != null)
    						updateLight(x, y + 1);
    				}
    			}
    		}
    	}

    getWall returns a wall object, but I'm only checking if there's a wall or not (null). getLight returns the light value (0 - 13) where 0 is no light and 13 is max light and setLights sets it.
    How can I update the placing correctly? And how can I update it correctly when the light power of the lamp changes?
    Last edited by Bingo90; April 2nd, 2014 at 06:59 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Java light source simulation

    Comment your code so that we can see what it is supposed to be doing.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    Removed unneccesary code and commented the rest.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    Can you explain the logic that you are trying to write code for?
    For example what does this mean:
    update it correctly when the light power of the lamp changes
    What is a "corrrect update" and how is it related to the light power?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    Updated the code to make the light power dynamic

    Well, here's an example if 2 walls are removed:

    light.jpg

    When replacing one it should go back to this:

    light2.jpg

    And when the light power changes, it should look like this:

    light3.jpg

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    What logic does the code need to do that?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    That's the problem. Like I said, when removing a wall tile, it calls the updateLight method, which calls it again for the spaces next to it. It should also call it when placing a wall, but it should unlight the spaces, without unliting the spaces that are still it by other sources.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    when placing a wall, but it should unlight the spaces, without unliting the spaces that are still it by other sources.
    Ok, what needs to be considered as you work on the logic for the program?
    What determines what spaces are lite by other sources? How will the code detect that condition and react correctly?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    If there's no wall on that space, the lamp's light will shine through that hole and the space is lit with the lamp's light power. A space also light's all spaces next to it with the light power -1.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    Ok. If there is a hole, the light shines full strength there and with a -1 power at the (up to) 8 adjacent squares.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    How far the light reaches depends on the power of the lamp, but yes you're right.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    How far the light reaches
    Does that mean the light goes past the adjacent squares? What determines how many squares it goes to?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    In the first and second pictures the lamp has a light power of 13 (max), in the third picture 6.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    Would the light intensity be decreased for each "ring" of squares around the hole?
    The first ring could have 8 squares.
    The next ring could have 18(?) squares
    The next ring could have 26(?) squares.

    There would NOT be rays pointing out from the hole. The decrease in intensity would be by ring.

    If there are overlapping rings from different holes, would the intensity would be additive?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    Right now it's not in a ring form. And the light is not additive. The light of the space is the light of the most lit space next to it (up, down, left, right not diagonally) -1. Min light is 0. And if there's no wall behind, the light is the light of the lamp.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    The light of the space is the light of the most lit space
    That would mean that there would have to be several passes made for a square to see which hole is closer and brighter to know which hole to use to set the light for that square.
    Seems like the light should be additive which would be simpler.

    not in a ring form.
    What does that mean? The light only shines in rays in the horizontal and vertical directions? The results will be a cross.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    That would mean that there would have to be several passes made for a square to see which hole is closer and brighter to know which hole to use to set the light for that square.
    Seems like the light should be additive which would be simpler.
    The darker space is not taking the light from the lighter space, it's the opposite.

    Here's an example:

    light4.png

    A's light is 6 and updates B. B gets light 5. B update's C which gets light 4. C checks D if the light is lower than C, but it's not, so it's not updating D. But E is getting updated.

    What does that mean?
    Don't you see it on the pictures? That's not a ring form, but also not a cross.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    That's not a ring form, but also not a cross.
    What is the algorithm for computing the intensity for adjacent squares?

    The light of the space is the light of the most lit space next to it
    Can you give an example of a square that has two holes near it, one closer than the other on different sides.
    Does what you said say that only one of the holes will be used to determine the intensity at the square. The intensity will not be additive.
    If the light is not additive, then the code needs to find which hole to use to set the intensity.

    --- Update ---

    The darker space is not taking the light from the lighter space, it's the opposite.
    Does that mean:
    The lighter space is taking the light from the darker space.
    That is very confusing.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    Here's how the light updates:

    light5.jpg

    A update's B, C, D and E - B updates G, F, H, and want's to update A - but A's more lit than B, so B doesn't update A. A space only updates a space next to it, if it is less lit than itself.

    And here's the example you wanted(I think that's what you meant)

    light6.jpg

    EDIT:

    The first picture seems to be a little bit blurry, here's a link to it:

    Link

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    I think it'd be easier to visualize if you used a 2D array of digits to show the light intensities for each square.
    For example:
    00100
    00300
    13531
    00300
    00100
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation


  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    Will the code be working with 2D arrays?
    Then it should be easier when talking about what the code is supposed to do to use 2D arrays of digits as I posted in post#20

    Sorry, I don't connect to other sites.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    Actually the light is a 2D byte array, only the wall is a 2D Wall array.

    Here's the image uploaded to this site:

    light7.jpg

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java light source simulation

    the light is a 2D byte array, only the wall is a 2D Wall array.
    So, the code is working with 2D arrays.

    What are the rules for setting the lightedness for squares when there is a light source (hole) at a square in the array?
    How do those rules change when there is more than one light source?
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Sep 2013
    Posts
    51
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Java light source simulation

    When a wall is removed, updateLight is called at this position. "if (getWall(x, y) == null)" is checking if there's a wall (of course after the removal there's no wall) and if not, the light level is set to the light level of the lamp (6 in the last images) it spreads in the way I showed you. It updates as I said in post #19. Slightly less blurry image: light8.jpg

Page 1 of 2 12 LastLast

Similar Threads

  1. The Light is ON!
    By yosukelight in forum Member Introductions
    Replies: 1
    Last Post: March 25th, 2014, 11:15 PM
  2. java simulation library
    By aceavong in forum Java Theory & Questions
    Replies: 1
    Last Post: March 12th, 2014, 05:45 AM
  3. Java Console American Football Simulation Tutorial/Example
    By TripleChickenJumpman in forum Java SE API Tutorials
    Replies: 0
    Last Post: January 4th, 2014, 06:40 PM
  4. Traffic Light problems
    By sircamolate in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 03:02 PM
  5. PeerSim P2P simulation based on JAVA
    By aditya149149 in forum Threads
    Replies: 0
    Last Post: August 24th, 2011, 04:26 PM

Tags for this Thread