Jump to content

  • Log In with Google      Sign In   
  • Create Account


boogyman19946

Member Since 15 May 2010
Offline Last Active Jul 24 2014 07:06 PM
-----

#5132861 Better way to write this code

Posted by boogyman19946 on 19 February 2014 - 10:22 PM

The second return statement is necessary because the compiler can't just assume the first return statement will execute. There is no guarantee that a node in the list matches the argument key.

 

Therefore, the return statement in question should return a value that one would expect if there was no entry matching the given key. If you can't return null, you could write it like this:

public Coordinates GetNodeCoordinates(int number)
        { 
            foreach(Node node in nodes)
            {
                if (node.Number == number)
                {
                    return node.Coordinates;
                }
            }
            return new Coordinates(); 
        }

But you won't be able to tell if you actually found a valid set of Coordinates unless you save that state somewhere. The above code is pretty much the same as yourself, with the exception that it only creates a new Coordinates at the last minute and only when needed. Not a great improvement, but some start.

 

I don't know C# very well. I thought it would have a reference system more similar to Java, but in Java you can return a null value for just about anything that's not a primitive.




#5131458 Trouble with strings and chars with arrays and loops

Posted by boogyman19946 on 14 February 2014 - 08:41 PM

I myself would prefer to use a simple table method although it really depends I guess. A HashMap will do the job and will probably be more straightforward (I guess it depends on what you're more used to), but if you are bound to using arrays, the following solution is probably what I'd use:

final char alphabet[] = "abcdefghijklmnopqrstuvwxyz".toCharArray(); // A little ugly over here, but, that's meh.

String str = "Some string text goes here";
char charArr[] = str.toLowerCase().toCharArray();

int charCounters[] = new int[alphabet.length];
for(char character: charArr) {
   for(int i = 0; i < alphabet.length; i++) {
      if(character == alphabet[i]) {
         charCounters[i]++;
         break;
      }
   }
}



#5131455 Trouble with strings and chars with arrays and loops

Posted by boogyman19946 on 14 February 2014 - 08:24 PM

charAt( int ) returns the primitive char, so you can't call functions on it. The mistake is obvious: you need to use == instead of =. Single equal sign assigns to variables (which is why your compiler is complaining about needing a variable), and double equal signs compare equality (which is what the if-statement is expecting).




#5128270 GDI+ How to redraw screen?

Posted by boogyman19946 on 02 February 2014 - 05:13 PM

Thanks, but I don't know where in I have hand on "backbufferDC" to Graphics* m_pGraphics;

You create it yourself. You don't want the user to see you draw every piece of the UI manually, that's what creates the flicker. To solve this, you create a buffer off to the side, do all your drawing there, and when it's done, you show the results on screen.




#5126756 Figuring out normal force.

Posted by boogyman19946 on 27 January 2014 - 11:52 AM

On inclined surfaces the normal force will be equal with the sine or cosine of the inclined surface angle * gravity . Am I right?

270px-Freebodydiagram3_pn.svg.png

 

I always remember it as the negative projection of the gravity force onto the normal vector of the surface where the object is rested.




#5126623 Figuring out normal force.

Posted by boogyman19946 on 26 January 2014 - 07:57 PM

It looks like you are making a step into the Continuous Collision Detection concept.

The idea is simple. You should first detect the collision between the two bodies, platform and player. When this type of collision is made, you mark it them as colliding, thus activating your continuous contact routines, which should apply the normal force. While they are marked as colliding, you should apply the normal, and keep in watch for a break of this contact, that could be caused by several things:

The player jumps

The player goes beyond the platform border

A new force is applied (a bomb, a bullet, a backflip kick from a rhino...)

Any force change (weakening gravity, variable magnetic force)

 

This is a simplified version of the theory, also notice that it is my own interpretation, but it is quite a complex algorithm and I am searching the Box2D code to understand its implementation myself; so I can't help you further, but if you like to read code, here is a link that can.

You could use a simpler but specialized version. It would probably perform even better.

 

This seems to be pretty much what I was looking for. I wasn't sure what to look up but I'll have a look at this when I have time and get back to you on that.

 

From your description, it sounds like the platform is not a collision object. If you make the platform a collision object, a collision will result (when appropriate) between the player and the platform, and provide the point of contact and the contact normal. (Many collision engines provide for a planar collision object with a normal, an extent, friction factors, etc.)

 

Am I understanding your question?

 

The platform is a collision object. The entire world in my game is actually made out of discrete objects. My question should have been "how do I know if I'm standing on a platform if I haven't detected collisions yet on this cycle." My physics simulator does things in the following order: sum forces, get acceleration, integrate acceleration and velocity, and with the resulting disposition, check collisions and respond. The problem with this is that I won't know if I'm standing on a platform until I check collisions, but I need to know that when I'm summing forces because of the normal force.

 

 

The normal force minus gravity equals 0 if you are standing on a platform.

N - G = 0 
N = G

 

The question isn't that simple I'm afraid. And I don't want to be a party pooper, but on an inclined surface, N != G.




#5126377 Figuring out normal force.

Posted by boogyman19946 on 25 January 2014 - 03:57 PM

Hello everyone!

 

I have a question that pertains to figuring out normal force. I seem to have a predicament that involves collision detection.

 

At the start I first sum all the forces acting on the object at hand. All external forces come from input or collisions that were maybe applied last cycle. Then all the internal forces: friction, gravity, and normal force. Gravity I can figure out, but figure friction requires me to know the normal but for that I have to know if I'm standing on a platform or not. My question is, is there some elegant way to solve this? For example, is it plausible to just omit the normal force on the first cycle, check collisions and apply the normal force on the next? Or should I make a deliberate check if player is standing on a platform?




#5126250 "Oh God, Who Wrote This?", "You Did", "Wait? I wrote Thi...

Posted by boogyman19946 on 24 January 2014 - 09:56 PM

 


Of course, everyone says I'm over reacting, but I've seen the effects of not caring, and I know how big of a benefit clarity and consistency carry.

There is definitely a balance to be struck there. Ideally you are able to write both sorts of code: the throwaway prototype you needed yesterday, and the carefully structured (and tested) code that is headed for production.

 

 

I agree that perhaps it's unnecessary to structure code with utmost care when prototyping, but even if prototyping I feel like there's still things one could do to write cleaner, albeit throwaway, code.

 

The poster in this thread is a prime example: http://www.gamedev.net/topic/652538-pathfinding-behaviour/

 

I wouldn't put code like this in production, but I would certainly not write it as throwaway either. I managed to fix an error in his adding function merely by giving the variables proper names, which, if he had done in the first place, he would have noticed the glaring logical error. 




#5125976 pathfinding behaviour

Posted by boogyman19946 on 23 January 2014 - 03:28 PM

The function that adds to heap does not seem correct to me. I tried to figure out what the proper name of the variables should have been, and it seems rather odd.

void addToOpenList(int x, int y)
{
	openListNumber++;
	Openlist[openListNumber] = new Point(x, y);
	int child = openListNumber;
	int parent = child;
	int F = squareMap[x, y].F;
	Point temp;
	while (child > 1)
	{
		parent = child / 2;
		
		if (squareMap[Openlist[child].X, Openlist[child].Y].F > F)
		{
			temp = Openlist[parent];
			Openlist[parent] = Openlist[child];
			Openlist[child] = temp;
		}
		child = parent;
	}
}

Now this algorithm is much easier to understand. The if statement seems to be a likely culprit. You are comparing the cost of a child node to the cost of the inserted node. Notice however, that in the body of the if statement, you swap the parent (which hasn't been compared to anything yet) with the child node. I think you meant to say the following:

void addToOpenList(int x, int y)
{
	openListNumber++;
	Openlist[openListNumber] = new Point(x, y);
	int child = openListNumber;
	int parent = child;
	Point temp;
	while (child > 1)
	{
		parent = child / 2;
		
		int childCost = squareMap[Openlist[child].X, Openlist[child].Y].F;
		int parentCost = squareMap[OpenList[parent].X, OpenList[parent].Y].F;
		
		if (childCost < parentCost)
		{
			temp = Openlist[parent];
			Openlist[parent] = Openlist[child];
			Openlist[child] = temp;
		}
		child = parent;
	}
}

Now at least the code clearly shows that it's moving the smallest node to the head of the tree. I think there's also a way to terminate the while loop early once the if statement evaluates false (since you're adding only one node and are moving up one branch of a binary tree).




#5125766 How do I fix my game in pygame

Posted by boogyman19946 on 22 January 2014 - 07:25 PM

Forum FAQ

 

 

Asking Questions:

  • Do not ask homework related questions, they will be closed on sight.



#5125665 pathfinding behaviour

Posted by boogyman19946 on 22 January 2014 - 10:28 AM

I think that might depend on which algorithm you're using. Some of the more naive ones can paint the whole darn thing yellow so that there wouldn't be any white left.

 

From the second picture it looks to me like you're using A* because the algorithm will start with checking paths that produce the shortest traveled distance + heuristic distance. It doesn't take into consideration the fact that there might be an obstacle on it's path. You can picture this in your own house for example. If you have two people standing on opposite sides of a wall, the straight line distance (commonly used as heuristic in A*) would direct A* at the wall first, but the actual shortest path might mean that you have to backtrack to get to the door. If there were no obstacles in your path, and all tiles had equal cost, you could probably just expect a single straight path to your goal.

 

The first one looks like it might have goofed up, unless some of the tiles incur significantly different costs.

 

But, all said and done, I've only ever really had to deal with pathfinding once, so my knowledge of it is rather limited.




#5125252 Pass entire vector of objects to constructor by reference?

Posted by boogyman19946 on 21 January 2014 - 12:05 AM

The vectors have incompatible types. Spell constructor is expecting std::vector<Hero>& and what you're giving it is... actually you're trying to perform an invalid operation on heroes so I'm not really sure what to say in this case.

 

There's a two main ways to go here. A) you change the std::vector<Hero>& inside of Spell to std::vector<Hero*>& and then change the push_back call to what Pachanoi said, or B) you change the type of std::vector<Hero*> to std::vector<Hero> and still change the push_back call to what Pachanoi said biggrin.png That will probably solve the problem. 

 

You might want to review the syntax for using pointers and references though to fully understand how to pass them to functions.




#5125246 Pass entire vector of objects to constructor by reference?

Posted by boogyman19946 on 20 January 2014 - 11:50 PM

What is the type of the heroes object? Your compiler is complaining about you using operator* on heroes.




#5124715 Python or Java?

Posted by boogyman19946 on 18 January 2014 - 03:40 PM

I'm with Sean on the point of efficiency. You should not worry about it unless you're making some truly performance critical software, and if you're not sure if your software is performance critical, then it's a safe bet that you're not writing it.

 

Now, on the note of learning programming, you're off to a good start. You first have to learn the basics of some language in order to start using it. This part is relatively straight forward. Which language you pick, however, will determine how easy the rest of your learning is going to be.

 

Second, you have to clock in a lot of practice, and if you intend to take programming seriously, then you must take your practice seriously. You cannot get rid of this step. In order to become an effective programmer, you must gain a certain amount of experience that you cannot read in books. Books are a great starting point, but they don't work very well with our memory. In order to remember something, you must repeat it. The more you work with it, the more available the knowledge becomes to you. 

 

Third, while you practice your programming chops, feel free to expand your knowledge in other areas. Programming can only get you so far. Programming is a tool, not a magical solution to everything. If you don't know classical mechanics very well, then your game physics code will reflect that with the results it's producing. You must know how to do something before you can tell the computer to do it, therefore, study! Knowledge can't hurt, and you can gain many benefits from it.

 

You'll notice I didn't mention anything about your Java vs Python question. That's because your question should be self-explanatory after reading the rest of your post. You're making progress in Python, so it would be silly to scrap that and start somewhere else.




#5124406 Is using Java a good way to create a game?

Posted by boogyman19946 on 17 January 2014 - 08:56 AM

Java is fine as any other language to make your first game, but a RPG game isn't in my opinion.

That's a rather particular genre that this general purpose language can't seem to handle correctly. Why do you think so?

 

EDIT: Oops, Sorry, I totally misread that post. I thought you were saying that Java isn't good for RPG games, but I understand hope (otherwise the question stands :D) that this is not the case. Thanks AlanSmithee for point that out!






PARTNERS