|
OOP Physics Implimentation... |
Michael Jensen
Member #2,870
October 2002
|
Hey guys, I'm new to OOP and my next attempted project will be my first c++ one (usually I program in just c with the perks of ++ except for OOP -- since I've been learning VB.NET/C# I've learned most of what's capable with OOP except for syntax with c++ which doesn't seem to be that hard to pickup on...) Anyway at first I was thinking I'd probably have a general object class that everything in my game would be a part of (maybe inherit from, not sure) and I'd pass the a pointer to a collection of objects off to a physics object to let it deal with them (apply gravity, friction, etc...) But then I was thinking maybe that's not the best way... It would be interesting if I had a global physics object that I could pass these other objects into, but let the objects themselves do it -- maybe pass a pointer to the global phsyics object into these entity objects in the constructor, and just call a tick or logic method or something and let themselves handle their own phsyics on the whole... anyway that's my starting point... any ideas, thoughts, better ways, critisisms?
|
SonShadowCat
Member #1,548
September 2001
|
Get a piece of paper, make a box with the base class and draw lines to boxes of derived classes. This helps you visualize if you're overdoing it and inheriting classes which should inherit from other classes. You wouldn't inherit an NPC from a base class that works for boxes would you? |
Michael Jensen
Member #2,870
October 2002
|
I dunno -- All of my oop experience so far is with the .net framework -- everything is an object or inherits from a base class named object -- everything... I kind of liked the idea... is this sort of thing slow?
|
lennaert van der linden
Member #6,053
July 2005
|
I don't think there is a noticeable speed decrease if you have every object derive from a common base class. But others may disagree. I think the question is wether you need a common base class for all classes or not. Stroustrup has some information on the topic: [url http://www.research.att.com/~bs/bs_faq2.html#object] |
HoHo
Member #4,534
April 2004
|
It will get slow if you have a lot of virtual functions that run for a short time. I once tried making a benchmarking program where each bencmarked class had a virtual function that did the test. This virtual function was called in a loop and after the loop the results were calculated. After I removed the virtual functions things speed up a lot. If the functions run for a lot longer time that it takes to call them then it shouldn't be a big problem. If in doubt, test and profile. __________ |
kentl
Member #2,905
November 2002
|
Both C# and Java use the idea of a common base class called Object. Not C++ though. If you want to use it make sure that you actually have an application of usage before just assuming that you "like it". A design should contain the relevant elements for the project at hand, and be as easy as possible. That's my philosophy anyway but others have their own ways. At a certain point making things even more general and extensible just loses it's purpose. |
Michael Jensen
Member #2,870
October 2002
|
Quote: Both C# and Java use the idea of a common base class called Object. Not C++ though. I know. That's not really what I meant... I was more thinking about putting all that my GAME OBJECTS have in common into one base class so that passing them into different functions etc would but simple... What's a virtual function? like a function pointer? I was planning more on overriding functions in the base-class, is that a virtual function? -- guess I don't know enough of the oop syntax/vocabulary for C++ yet, maybe I'll hold off a little bit longer...
|
HoHo
Member #4,534
April 2004
|
Quote: What's a virtual function? like a function pointer? In Java and probaly in C#, every class function is a virtual one. In C++ functions that you declare as virtual(functions that can be overriden by derieving classes) are virtual. Quote: guess I don't know enough of the oop syntax/vocabulary for C++ yet, maybe I'll hold off a little bit longer... Search for Bruce Eckel's Thinking in C++. It is a free download from its site. It's a good book for learinign C++, I used it as my only book when I learnt it. __________ |
da_flo
Member #1,907
February 2002
|
Quote: is that a virtual function? -- guess I don't know enough of the oop syntax/vocabulary for C++ yet, Yeah, learn what virtual functions are in C++. Otherwise, if you never used polymorphism in C++, you might have problems when overriding class functions ( static polymorphism VS dynamic polymorphism ) |
kentl
Member #2,905
November 2002
|
Quote: maybe I'll hold off a little bit longer... No learn by doing instead. It's not that difficult. Gillius seems to have made a good tutorial about OO with C++. |
nonnus29
Member #2,606
August 2002
|
Java and C# hide the 'early and late binding' idea from you, the programmer. In Java and c# all functions are 'bound' after the number and types in the arguments are known. In C++ this isn't so; early binding is the default. In java and c# you can override any method. In c++ to override a method it must be 'virtual'. Quote: I know. That's not really what I meant... I was more thinking about putting all that my GAME OBJECTS have in common into one base class so that passing them into different functions etc would but simple... I don't think it'll be as simple as you might think. You'll have game objects with wildly different behavior; for example particles like smoke that don't interact with the other objects and triggers that have no on-screen representation. So to code a robust set of methods in a base class to account for all of these possibilties becomes very cumbersome. IMHO the most intuitive solution is to build a limited number of objects from some basic parts; ie a CActor may have objects CSprite, CPhysics, CBehavior. a CTrigger may have CPhysics and CBehavior. For ease of use the base class could be a CList node or a CObject so you can stick em all in a <vector>. That's just my $.02. Edit; and this is why I think C is more fun than c++. All this oop mumbo jumbo is distracting and it can grow very complex very quickly... imho of course |
da_flo
Member #1,907
February 2002
|
Quote: In c++ to override a method it must be 'virtual' In C++ you can actually override any method, virtual as well as non virtual. If the method is not virtual, the overriden method will only get called is your instance has the type of the derived class. Calling the method on a generic base class instance will call the base class method (Static polymorphism) If the method is virtual, then when it's called it'll always execute the method corresponding to the actual type of the instance, ie the method from the derived class. (Dynamic polymorphism) Well, I don't know if I was clear here, but I wanted to point this out. |
nonnus29
Member #2,606
August 2002
|
Oops, you're right and I can't think of a good reason why anyone would ever care to have static polymorphism. |
da_flo
Member #1,907
February 2002
|
Quote: I can't think of a good reason why anyone would ever care to have static polymorphism.
Me neither. Static polymorphism is here mainly to confuse people learning OOP in C++, IMO. |
Rick
Member #3,572
June 2003
|
I see allot of: Quote: Java and C# Just to be correct, you can't really say this is how C# handles things. C# is just a language to the .NET framework. Really you should say "Java and .NET". Since there are about 20 different languages that work on the .NET platform, and they all work the same way, just different syntax. You could create a class in COBOL.NET and use it in C#. All languages for the .NET conform to a specific functionality. Anyway... it sounds like you need to read up a little more on C++. If you didn't know what virtual functions are, then you cannot use the full power of C++. [EDIT] ======================================================== |
HoHo
Member #4,534
April 2004
|
Quote: Just to be correct, you can't really say this is how C# handles things. C# is just a language to the .NET framework. Really you should say "Java and .NET". I know that. I just simplified because OP said he had used C# before __________ |
Michael Jensen
Member #2,870
October 2002
|
How many non .NET bound compilers are there for C#? Anyway, in .NET OOP seems to be about the same in all of the languages, except for syntax... I figured to go to c++ it would all be just syntax, guess not... I'll pick it up sooner or later... For now I'm gonna stick to straight-c for my speed hack entry at least...
|
Rick
Member #3,572
June 2003
|
Quote: How many non .NET bound compilers are there for C#? None. C# was created specially for .NET. It's basically Microsofts alternative from using MFC. C# can't be used for anything but Microsofts .NET platform as of now. Basically Microsoft saw the possible problems with using C++ to program .NET applications (although you can use Managed C++ with the .NET framework), and decided to create their own C like language for .NET. This is to ease C & C++ people over to the .NET framework. Almost every book you read on .NET has the line, "If you are a C++ programmer, migrating to C# is easy". ======================================================== |
Michael Jensen
Member #2,870
October 2002
|
Quote: as of now I'm told that C# has been submitted to ANSI as a normal programming language (or something of that nature) supposedly anybody that wants to can write a c# compiler and it doesn't have to be linked in any way to .NET -- the only non MS compiler I know of for C# exists in linux -- but it's linked with MONO which is basically a recreation of .NET for linux... pretty cheesy stuff... (and no, I don't know the compiler's name, but it might be a part of MONO, not sure...)
|
Ron Ofir
Member #2,357
May 2002
|
Back to topic, I once (4-5 month ago) had an idea on how to do it (physics engine) which I based on Newton's laws. It's probably a bad idea, but here goes: Well, whaddaya say? Is it that bad? I didn't write it because a)I know nothing about collision detection b)I still don't know enough physics (I know simple Mechanics and Newton's laws) c)I got 60 in my last physics test which was about Newton's 2nd law :x:-/ |
Michael Jensen
Member #2,870
October 2002
|
Eh, you don't have be good with physics to fake it in a game, game physics is inspired by real world physics but you shouldn't make the user have a 2ghz machine because your physics is more real.
|
|