• What Do You Need Help With? V6
    7,544 replies, posted
[QUOTE=Contron;46581279]That is the completely wrong syntax. You cannot have a multi-dimensional ArrayList in Java. It'll either have to be an actual array, or a List that contains a wrapper class for the two values. Plus, you'll [B]want[/B] the generic identifier within the diamond braces. [code] public class StringContainer { public StringContainer(String first, String second) { this.first = first; this.second = second; } //getters and setters... private String first; private String second; } [/code] [code] ArrayList<StringContainer> stringContainers = new ArrayList<StringContainer>(); [/code][/QUOTE]That's what I thought. So I would need to make a separate class to handle coordinates and entity ID's, and put that into an ArrayList? [editline]26th November 2014[/editline] I think the simpler solution would just be to do what was said before, making a traditional 3D array that holds a max of 32 entities per cell. Now I just need to make ID's for entities. Anyone care to get me started on that?
[QUOTE=NixNax123;46581624]That's what I thought. So I would need to make a separate class to handle coordinates and entity ID's, and put that into an ArrayList? [editline]26th November 2014[/editline] I think the simpler solution would just be to do what was said before, making a traditional 3D array that holds a max of 32 entities per cell. Now I just need to make ID's for entities. Anyone care to get me started on that?[/QUOTE] Simple. Make an enum class in java called entityType and in there, list your types in caps (for good style) - PLAYER, ASTEROID, BULLET, etc. Include a GENERIC as a base case as well. Then you can distinguish what an entity is by giving it a simple data member of entityType named id, and use a getter to retrieve it. Set it in the constructor of entity. If you're looking to give every single entity on the screen a different id then that's a different story, but i'd question the necessity of that in the first place.
[QUOTE=Contron;46581279]That is the completely wrong syntax. You cannot have a multi-dimensional ArrayList in Java. It'll either have to be an actual array, or a List that contains a wrapper class for the two values. Plus, you'll [B]want[/B] the generic identifier within the diamond braces. [/QUOTE] It is a 2d array of array lists. There is absolutely nothing wrong with it if you want something that is 3d where only the innermost dimension can grow and shrink but not the outer dimensions. And it is 100% valid java A better approach is probably a list of list of list. Or a class like: [code] class Foo { List<Bar> list = new ArrayList<Bar>(); } Foo[][] ar; [/code] But my original approach seems to work fine [editline]26th November 2014[/editline] [QUOTE=killerteacup;46582339]Simple. Make an enum class in java called entityType and in there, list your types in caps (for good style) - PLAYER, ASTEROID, BULLET, etc. Include a GENERIC as a base case as well. Then you can distinguish what an entity is by giving it a simple data member of entityType named id, and use a getter to retrieve it. Set it in the constructor of entity. If you're looking to give every single entity on the screen a different id then that's a different story, but i'd question the necessity of that in the first place.[/QUOTE] Why not just have a list of IGameEntity or ICollideable or something. If all this quadtree gives a shit about is stuff that takes up area and can collide then they should inherit from something common and you make a list out of that [editline]26th November 2014[/editline] I'm not saying he's going down the right path for his overall assignment, I'm just answering his specific questions Nax your teacher / professor / TAs are a better resource than facepunch / the internet.
[QUOTE=thrawn2787;46582656]It is a 2d array of array lists. There is absolutely nothing wrong with it if you want something that is 3d where only the innermost dimension can grow and shrink but not the outer dimensions. And it is 100% valid java A better approach is probably a list of list of list. Or a class like: [code] class Foo { List<Bar> list = new ArrayList<Bar>(); } Foo[][] ar; [/code] But my original approach seems to work fine [editline]26th November 2014[/editline] Why not just have a list of IGameEntity or ICollideable or something. If all this quadtree gives a shit about is stuff that takes up area and can collide then they should inherit from something common and you make a list out of that [editline]26th November 2014[/editline] I'm not saying he's going down the right path for his overall assignment, I'm just answering his specific questions Nax your teacher / professor / TAs are a better resource than facepunch / the internet.[/QUOTE] That's actually what I do on my games so you're 100% right. I actually wasn't entirely sure about the context of his question. If I needed to make each entity have an ID for generic purposes I probably would use enums but if its just for the use of a quadtree just grab everything from the list.
Is there a way to fetch the last message sent over Steam in Python?
[QUOTE=thrawn2787;46582656]It is a 2d array of array lists. There is absolutely nothing wrong with it if you want something that is 3d where only the innermost dimension can grow and shrink but not the outer dimensions. And it is 100% valid java A better approach is probably a list of list of list. Or a class like: [code] class Foo { List<Bar> list = new ArrayList<Bar>(); } Foo[][] ar; [/code] [/QUOTE] I apologise, you're completely right. It does work, I just misread your post wrong. Sorry. I've just never actually seen it been done like that so it's why I recommended having a container class of sorts.
So I've got this: [code] // data private final List<Entity>[][] grid; /** * Constructs a grid with the specified boundary. * @param gridSize the size of the grid. */ public PartionedGrid(Vector2f gridSize) { this.gridSize = gridSize; gridCellSize = gridSize.x / 32; rows = (int)((gridSize.y + gridCellSize - 1) / gridCellSize); cols = (int)((gridSize.x + gridCellSize - 1) / gridCellSize); grid = new ArrayList<Entity>[cols][rows]; }[/code] Which won't compile. How would I make this work without having a raw type for grid?
Well, I need some help understanding some stuff. I'm really new to proggramming, and %90 of the time I have minor questions about minor stuff, that isn't even worth to ask here, but also I need to know these stuff. What I'm asking for mainly is either some people here to ask stuff by PM, or someone to add me on Steam. I guess I can't simply post this here, so have some of the questions that bugged my mind for the past 2-3 hours. What is compiling and decompiling to sum up(maybe I've got it wrong)? Why is it particularly hard to decompile a C++ file? What other languages can be decompiled better than C++, and why is that, what difference is there? How can I see the interior of dlls in any other way? Also one last thing: Rise of Nations has a script editor, which has pre defined "trigger functions". All I wanted to find is where these are defined, and how they are defined. To learn from examples. Now , I have opened nearly every file that I can open with my Notepad++, and couldn't find anything. Any ideas?
[QUOTE=NixNax123;46585797]So I've got this: [code] // data private final List<Entity>[][] grid; /** * Constructs a grid with the specified boundary. * @param gridSize the size of the grid. */ public PartionedGrid(Vector2f gridSize) { this.gridSize = gridSize; gridCellSize = gridSize.x / 32; rows = (int)((gridSize.y + gridCellSize - 1) / gridCellSize); cols = (int)((gridSize.x + gridCellSize - 1) / gridCellSize); grid = new ArrayList[cols][rows]; }[/code] Which won't compile. How would I make this work without having a raw type for grid?[/QUOTE] As I said in my post above you need to exclude the generic on the rhs. I've edited the code in my quote [editline]27th November 2014[/editline] [QUOTE=Trebgarta;46587410] I guess I can't simply post this here, so have some of the questions that bugged my mind for the past 2-3 hours. What is compiling and decompiling to sum up(maybe I've got it wrong)? Why is it particularly hard to decompile a C++ file? What other languages can be decompiled better than C++, and why is that, what difference is there? How can I see the interior of dlls in any other way? Also one last thing: Rise of Nations has a script editor, which has pre defined "trigger functions". All I wanted to find is where these are defined, and how they are defined. To learn from examples. Now , I have opened nearly every file that I can open with my Notepad++, and couldn't find anything. Any ideas?[/QUOTE] Compiling = taking the written code and transforming it into something the computer actually understands (binary). Computers don't read C/C++/Java, they read binary. Decompiling = taking compiled binary code and transforming it back into the language it came from. Compilers do a shit ton of optimizations when compiling so it is very hard to recreate the original code most of the time. If you are new to programming and want to learn I recommend learning by making your own programs rather than trying to reverse engineer existing things. Seems like you want to mod games. Go mod games that have mod support rather than trying to do weird hacky things with games that don't have mod support. And if google yields nothing on finding where the rise of nations methods are defined then the devleopers have probably hidden them intentionally (aka only offered them as compiled code, not the source). Many programs do this. They will give you documentation as to what is [I]publicly [/I]exposed and how to use it but they do not give you access to the actual source code.
[url]http://stackoverflow.com/questions/448981/what-characters-are-valid-in-css-class-selectors[/url] This solved it.
[QUOTE=thrawn2787;46587450] Compiling = taking the written code and transforming it into something the computer actually understands (binary). Computers don't read C/C++/Java, they read binary. Decompiling = taking compiled binary code and transforming it back into the language it came from. Compilers do a shit ton of optimizations when compiling so it is very hard to recreate the original code most of the time. If you are new to programming and want to learn I recommend learning by making your own programs rather than trying to reverse engineer existing things. Seems like you want to mod games. Go mod games that have mod support rather than trying to do weird hacky things with games that don't have mod support. And if google yields nothing on finding where the rise of nations methods are defined then the devleopers have probably hidden them intentionally (aka only offered them as compiled code, not the source). Many programs do this. They will give you documentation as to what is [I]publicly [/I]exposed and how to use it but they do not give you access to the actual source code.[/QUOTE] Thanks for the info, however, I am not trying to do weird hacky things. I can define my own functions in the script maker very well as of now, but I just wanted to see the default ones to get an idea;I personally learn best through examples. Did modding on Company of Heroes before, it really helped. I am making scenarios with custom scripts for RoN now. You know, my idea was: The function to lock mouse of the player exists, but it is not particularly nice in Multiplayer since it locks everyone's mouse (in thory, haven't actually tested it, let's say 99% since it has no nation/player input). There is also disable_type (disables a unit globally) and disable_unit_by_tribe (disables a unit [I]for a single nation[/I]) there, so looking at the definition ways of three, I thought I could figure out a way myself to write a function that would lock the mouse of particular players. I could try making my own program in the future, right now I have 2 WIP scenarios in RoN, I will finish them first. One is in a deadlock because of MP problems I've mentioned here, and I'm working on the other one right now. The second one is progressing nicely. Or I could mod CoH2, since attribeditor is released and &#305; got it off exploration sale (if ti will download-20 hours, nice.)
Hey guys, I'm working with Python and I have a question I have a list of informations like this in a list: ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'] ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K'] aka list[1] would be: ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'] and list[2] would be: ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K'] I want to only take the last bit of it, <=50K, how do I do that?
[QUOTE=thrawn2787;46587450]As I said in my post above you need to exclude the generic on the rhs. I've edited the code in my quote [editline]27th November 2014[/editline] Compiling = taking the written code and transforming it into something the computer actually understands (binary). Computers don't read C/C++/Java, they read binary. Decompiling = taking compiled binary code and transforming it back into the language it came from. Compilers do a shit ton of optimizations when compiling so it is very hard to recreate the original code most of the time. If you are new to programming and want to learn I recommend learning by making your own programs rather than trying to reverse engineer existing things. Seems like you want to mod games. Go mod games that have mod support rather than trying to do weird hacky things with games that don't have mod support. And if google yields nothing on finding where the rise of nations methods are defined then the devleopers have probably hidden them intentionally (aka only offered them as compiled code, not the source). Many programs do this. They will give you documentation as to what is [I]publicly [/I]exposed and how to use it but they do not give you access to the actual source code.[/QUOTE] Removing the generic would just yield a raw data type warning. Which ends up in the list not being able to be used.
It is 100% useable. Did you try using it? Warnings aren't errors! [code] List<String>[][] l = new ArrayList[20][20]; for(int i = 0; i < 20; i++) for(int j = 0; j < 20; j++) l[i][j] = new ArrayList<String>(); l[0][0].add("ASDF"); System.out.println(l[0][0].get(0));[/code] Output: [code] ASDF [/code]
Oh, I totally forgot to initialize the indices. Silly me!
[QUOTE=Kingbob387;46588457]Hey guys, I'm working with Python and I have a question I have a list of informations like this in a list: ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'] ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K'] aka list[1] would be: ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'] and list[2] would be: ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K'] I want to only take the last bit of it, <=50K, how do I do that?[/QUOTE] If your list is named, for instance, foo, then you can either use negative indices: [code] foo[-1] [/code] or get length of the list and use it to access the last element: [code] foo[len(foo) - 1] [/code]
That's a feature I wish was implemented in some way for other high level programming languages. I feel so dumb sometimes doing arr[arr.length - 1].
I have a C# project (A) which is dependent on a C# library (L). Both have their own separate Git repo, and both are in development. Is there anything inherently wrong with including the entire library solution L as a submodule in the root directory of A, then referencing the required projects in L from A? Looks like this: [code] \A \ProjectInA\ \ProjectInA.csproj \A.sln \L \ProjectInL \ProjectInL.csproj \L.sln [/code] A.sln contains a reference to ProjectInL.csproj. L would be a Git submodule. Bonus question: Is there anything wrong with treating L as a 3rd party library and just including a DLL in the repo? Bonus question 2: Is NuGet overkill for this simple dependency situation?
[QUOTE=NixNax123;46591262]That's a feature I wish was implemented in some way for other high level programming languages. I feel so dumb sometimes doing arr[arr.length - 1].[/QUOTE] perl does away with array size completely and uses $# to get the last index of an array when you get used to it its pretty nice actually
probably the wrong section, but anyone know if it's possible to format your pc and install windows 7 on a different disk than the one my system was on - all of this without using discs? or do I have to use a USB-pen?
[QUOTE=war_man333;46592039]probably the wrong section, but anyone know if it's possible to format your pc and install windows 7 on a different disk than the one my system was on - all of this without using discs? or do I have to use a USB-pen?[/QUOTE] You just run the installer from the .iso you downloaded, it works just fine unless you interrupt the installation process. [editline]28th November 2014[/editline] Don't forget to extract the ISO first.
[QUOTE=Kingbob387;46588457]Hey guys, I'm working with Python and I have a question I have a list of informations like this in a list: ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'] ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K'] aka list[1] would be: ['68', ' ?', ' 38317', ' 1st-4th', ' 2', ' Divorced', ' ?', ' Not-in-family', ' White', ' Female', ' 0', ' 0', ' 20', ' United-States', ' <=50K'] and list[2] would be: ['32', ' ?', ' 293936', ' 7th-8th', ' 4', ' Married-spouse-absent', ' ?', ' Not-in-family', ' White', ' Male', ' 0', ' 0', ' 40', ' ?', ' <=50K'] I want to only take the last bit of it, <=50K, how do I do that?[/QUOTE] Try list[1][15]
So I've got a functional Grid class for entity collision. [code]/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coratticca.Utils; import coratticca.Entities.Entity; import java.util.List; import java.util.ArrayList; import java.util.Collections; import org.jsfml.system.Vector2f; /** * A Grid class to partition the game screen for smoother collision * handling. * * @author Nick */ public class Grid { // size of each cell in the grid private final float gridCellSize; // rows and columns in the grid private final int rows; private final int cols; // data private final List<Entity>[][] grid; private final List<Entity> retrieveList = new ArrayList<>(); /** * Constructs a grid with the specified boundary. * @param gridSize the size of the grid. */ @SuppressWarnings("unchecked") public Grid(Vector2f gridSize) { gridCellSize = gridSize.x / 32; rows = (int)((gridSize.y + gridCellSize - 1) / gridCellSize); cols = (int)((gridSize.x + gridCellSize - 1) / gridCellSize); grid = new ArrayList[cols][rows]; // initialize grid indices for (int c = 0; c < cols; c++) { for (int r = 0; r < rows; r++) { grid[c][r] = new ArrayList<>(); } } } public void clear() { for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { grid[x][y].clear(); } } } public void addEntity(Entity e) { // get all the points the entity takes up // do not accept negative values, or values outside of the grid float topLeftX = Math.max(0, e.getPos().x / gridCellSize); float topLeftY = Math.max(0, e.getPos().y / gridCellSize); float edgeLength = e.getBounds().getRadius().x * 2; float bottomRightX = Math.min(cols - 1, (e.getPos().x + edgeLength - 1) / gridCellSize); float bottomRightY = Math.min(rows - 1, (e.getPos().y + edgeLength - 1) / gridCellSize); for (int x = (int) topLeftX; x <= bottomRightX; x++) { for (int y = (int) topLeftY; y <= bottomRightY; y++) { grid[x][y].add(e); } } } public Entity getNearest(Entity e) { // For comparisons Entity nearest = null; float maxDist = Float.MAX_VALUE; // Retrieve the entities List<Entity> collidables = retrieve(e); // Iterate and find the nearest for (Entity toCheck : collidables) { float xDiff = toCheck.getPos().x - e.getPos().x; float yDiff = toCheck.getPos().y - e.getPos().y; float dist = (float) Math.sqrt(xDiff*xDiff + yDiff*yDiff); if (dist < maxDist) { nearest = toCheck; maxDist = dist; } } return nearest; } private List<Entity> retrieve(Entity e) { // Retrieve the list of collide-able entities retrieveList.clear(); // Calculate the positions again float topLeftX = Math.max(0, e.getPos().x / gridCellSize); float topLeftY = Math.max(0, e.getPos().y / gridCellSize); float edgeLength = e.getBounds().getRadius().x * 2; float bottomRightX = Math.min(cols - 1, (e.getPos().x + edgeLength - 1) / gridCellSize); float bottomRightY = Math.min(rows - 1, (e.getPos().y + edgeLength - 1) / gridCellSize); for (int x = (int)topLeftX; x <= bottomRightX; x++) { for (int y = (int)topLeftY; y <= bottomRightY; y++) { List<Entity> cell = grid[x][y]; // Add every entity in the cell to the list for (Entity ent : cell) { if (!retrieveList.contains(ent)) { retrieveList.add(ent); } } } } return Collections.unmodifiableList(retrieveList); } } [/code] My question is, how would I clear / update the grid every frame? Would I have to add the entity, refresh it, then clear the grid in every entity's update function?
Not really a need-help-with, more of a question on semantics. So I'm pretty fresh with C#, though I have a few years experience in Java. In Java, if I wanted to create a field that has a public getter but a private setter, I would do something like this: [code]int myProperty = 0; public int getMyProperty() { return myProperty; } private void setMyProperty(int myProperty) { this.myProperty = myProperty; } [/code] However, with C#'s properties providing getter and setter functionality, I don't have to explicitly declare the methods myself. Something I haven't really seen people do, though, is something like this, which is intuitively what comes to my mind: [code] private int _myProperty; public int MyProperty { get { return _myProperty; } private set { _myProperty = value; } } [/code] Are there any particular reasons why people don't seem to do this way? Is there some C#-ist way I should be doing this instead?
Well first of all your setter is recursive and will cause a stack overflow (edit: you fixed it) Secondly I do it a lot in situations like this [code] interface IFoo { int MyProperty { get; } } class Foo : IFoo { int _myProperty; #region IFoo public int MyProperty { get { return _myProperty; } private set { if(_myProperty != value) { // other stuff goes here _myProperty = value; // other stuff goes here } } } #endregion } [/code] If your getters and setters don't do anything then just use auto properties. Aka [code] public int MyProperty { get; private set; } [/code] No backing field needed
[QUOTE=thrawn2787;46595125]Well first of all your setter is recursive and will cause a stack overflow (edit: you fixed it) [/QUOTE] Yeah, I finally got it to compile and found that out right quickly. Can you tell I'm [b]really[/b] fresh with C#? :v: [quote=thrawn2787;46595125] Secondly I do it a lot in situations like this [code] ... [/code] If your getters and setters don't do anything then just use auto properties. Aka [code] public int MyProperty { get; private set; } [/code] No backing field needed[/quote] I tried that, and it yelled at me about using "private" in the auto-properties. Maybe it was due to an unrelated error, though. Like I said, I had a bit of a fight getting it to compile. :v: Thanks, mate! I'll try the private auto-properties again. Would be convenient if they worked, because I am looking at this and going "And I thought people said C# was supposed to be less verbose than Java when it came to getters and setters..." :v: [editline]Fish[/editline] Okay, I got it to work, and it's much cleaner. Now, on a related note, I am using an Interface - let's call it IFoo, following your example - that I will be operating on exclusively. Which is to say, I will have a collections of heterogeneous classes which share a common interface (IFoo), and so will be doing work of the form of [b]foreach (IFoo obj in MyCollection)[/b]. However, I still want the public-getter-private-setter, declared in the IFoo interface so that I can operate within loops like the above and use them. Interfaces apparently cannot have access modifiers defined in their properties; I have to omit the "set" entirely, like you do, in the interface. What would be the behavior of that? Would I not then be able to call [b]foreach (IFoo obj in MyCollection) obj.MyProperty = 5[/b], assuming MyProperty is defined in the interface IFoo as [b]int MyProperty {get; }[/b]? [editline]Taco[/editline] Actually, looking through C#'s access levels, I actually want to use a public-getter-internal-setter, so that everything within the namespace/project can set these properties, but external items cannot. I am building a library, if the use of internal isn't already evident of that. So change every instance of "private" to "internal" in my examples. :v:
[QUOTE=NixNax123;46594078]So I've got a functional Grid class for entity collision. [code]/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package coratticca.Utils; import coratticca.Entities.Entity; import java.util.List; import java.util.ArrayList; import java.util.Collections; import org.jsfml.system.Vector2f; /** * A Grid class to partition the game screen for smoother collision * handling. * * @author Nick */ public class Grid { // size of each cell in the grid private final float gridCellSize; // rows and columns in the grid private final int rows; private final int cols; // data private final List<Entity>[][] grid; private final List<Entity> retrieveList = new ArrayList<>(); /** * Constructs a grid with the specified boundary. * @param gridSize the size of the grid. */ @SuppressWarnings("unchecked") public Grid(Vector2f gridSize) { gridCellSize = gridSize.x / 32; rows = (int)((gridSize.y + gridCellSize - 1) / gridCellSize); cols = (int)((gridSize.x + gridCellSize - 1) / gridCellSize); grid = new ArrayList[cols][rows]; // initialize grid indices for (int c = 0; c < cols; c++) { for (int r = 0; r < rows; r++) { grid[c][r] = new ArrayList<>(); } } } public void clear() { for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { grid[x][y].clear(); } } } public void addEntity(Entity e) { // get all the points the entity takes up // do not accept negative values, or values outside of the grid float topLeftX = Math.max(0, e.getPos().x / gridCellSize); float topLeftY = Math.max(0, e.getPos().y / gridCellSize); float edgeLength = e.getBounds().getRadius().x * 2; float bottomRightX = Math.min(cols - 1, (e.getPos().x + edgeLength - 1) / gridCellSize); float bottomRightY = Math.min(rows - 1, (e.getPos().y + edgeLength - 1) / gridCellSize); for (int x = (int) topLeftX; x <= bottomRightX; x++) { for (int y = (int) topLeftY; y <= bottomRightY; y++) { grid[x][y].add(e); } } } public Entity getNearest(Entity e) { // For comparisons Entity nearest = null; float maxDist = Float.MAX_VALUE; // Retrieve the entities List<Entity> collidables = retrieve(e); // Iterate and find the nearest for (Entity toCheck : collidables) { float xDiff = toCheck.getPos().x - e.getPos().x; float yDiff = toCheck.getPos().y - e.getPos().y; float dist = (float) Math.sqrt(xDiff*xDiff + yDiff*yDiff); if (dist < maxDist) { nearest = toCheck; maxDist = dist; } } return nearest; } private List<Entity> retrieve(Entity e) { // Retrieve the list of collide-able entities retrieveList.clear(); // Calculate the positions again float topLeftX = Math.max(0, e.getPos().x / gridCellSize); float topLeftY = Math.max(0, e.getPos().y / gridCellSize); float edgeLength = e.getBounds().getRadius().x * 2; float bottomRightX = Math.min(cols - 1, (e.getPos().x + edgeLength - 1) / gridCellSize); float bottomRightY = Math.min(rows - 1, (e.getPos().y + edgeLength - 1) / gridCellSize); for (int x = (int)topLeftX; x <= bottomRightX; x++) { for (int y = (int)topLeftY; y <= bottomRightY; y++) { List<Entity> cell = grid[x][y]; // Add every entity in the cell to the list for (Entity ent : cell) { if (!retrieveList.contains(ent)) { retrieveList.add(ent); } } } } return Collections.unmodifiableList(retrieveList); } } [/code] My question is, how would I clear / update the grid every frame? Would I have to add the entity, refresh it, then clear the grid in every entity's update function?[/QUOTE] You don't clear the grid in an entity's update function, you do it in your main game loop. You clear and re-fill it before performing collision detection.
[QUOTE=Gmod4ever;46595202]Yeah, I finally got it to compile and found that out right quickly. Can you tell I'm [B]really[/B] fresh with C#? :v: I tried that, and it yelled at me about using "private" in the auto-properties. Maybe it was due to an unrelated error, though. Like I said, I had a bit of a fight getting it to compile. :v: Thanks, mate! I'll try the private auto-properties again. Would be convenient if they worked, because I am looking at this and going "And I thought people said C# was supposed to be less verbose than Java when it came to getters and setters..." :v: [editline]Fish[/editline] Okay, I got it to work, and it's much cleaner. Now, on a related note, I am using an Interface - let's call it IFoo, following your example - that I will be operating on exclusively. Which is to say, I will have a collections of heterogeneous classes which share a common interface (IFoo), and so will be doing work of the form of [B]foreach (IFoo obj in MyCollection)[/B]. However, I still want the public-getter-private-setter, declared in the IFoo interface so that I can operate within loops like the above and use them. Interfaces apparently cannot have access modifiers defined in their properties; I have to omit the "set" entirely, like you do, in the interface. What would be the behavior of that? Would I not then be able to call [B]foreach (IFoo obj in MyCollection) obj.MyProperty = 5[/B], assuming MyProperty is defined in the interface IFoo as [B]int MyProperty {get; }[/B]? [editline]Taco[/editline] Actually, looking through C#'s access levels, I actually want to use a public-getter-internal-setter, so that everything within the namespace/project can set these properties, but external items cannot. I am building a library, if the use of internal isn't already evident of that. So change every instance of "private" to "internal" in my examples. :v:[/QUOTE] In all languages I know of everything in an interface has to be public That's the point of interfaces :) So if you want that property to be settable on IFoo then have set; in the interface. If you really want to do some trickery you could do [code] namespace MyLib { /// <summary> /// Public interface /// </summary> public interface IFoo { int MyProperty { get; } } /// <summary> /// Internal interface with setter /// </summary> internal interface IFoo2 : IFoo { int MyProperty { get; set; } } /// <summary> /// IFoo + IFoo2 implementation. Internal. /// </summary> internal class MyFoo : IFoo2 { #region IFoo2 public int MyProperty { get; set; } #endregion } /// <summary> /// Some publicly exposed class to get access to your interface implementations /// </summary> public class LibraryFactory { public static IFoo MakeFoo() { return new MyFoo { MyProperty = 5 } } } } [/code] However I recommend against this. Just have your library know about all of its classes. Saves you having the two interfaces.
[QUOTE=Tommyx50;46596139]You don't clear the grid in an entity's update function, you do it in your main game loop. You clear and re-fill it before performing collision detection.[/QUOTE]So I would have to add the entity to the grid in the update function, then?
[QUOTE=NixNax123;46598508]So I would have to add the entity to the grid in the update function, then?[/QUOTE] Yes. After performing the euler integration (but before collision detection), you'll want to clear and re-fill the entire grid.
Sorry, you need to Log In to post a reply to this thread.