planning out a physics engine
BlitzMax Forums/BlitzMax Programming/planning out a physics engine
| ||
ok so I have come up with a much better sleeker more OO and modularized design for my physics engine. Here is the "base" and how it will work.Rem Instructions for adding/removing custom properties to the physicsworld type: 1. add a field with all variables and lists your type will need. 2. After you create a physicsworld (assuming you call your world myworld:physicsworld) call the myworld.addperoperty( function pointer ) with a function pointer pointing to the update function of your new "property" This function must have a single parameter as follows myfunction(p:physicsworld) The physics world type will call the function passing itself as a parameter giving that function access to the lists and variables of that world 3. Now all you have to do is call myworld.update() and it will call all of your update functions. Call myworld.init and it will call all of your initiation functions you set using the myworld.addpropertyinit( function pointer ) This function must also have a single parameter of p:physicsworld EndRem Type PhysicsWorld Global PWorldList:TList = New TList 'list of all physics worlds Field Width#,Height# 'width and height of the physics world. Field VerletList:TList 'verlets in the physics world Field ConstraintList:TList 'constraints in the physics world Field ForceList:TList 'forces in the physics world Field PhysicsBodies:TList 'physics bodies in the physics world Field gravityx:Float 'gravity x velocity Field gravityy:Float 'and gravity y velocity Field PropertiesInit:TList 'list of functions that initiate the properties Field Properties:TList 'list of functions called every physics update. Function Create:PhysicsWorld(Width#,Height#,Property_Flag:Int = 1) Local p:physicsworld = New physicsworld p.width = width p.height = height p.propertiesInit:TList = New TList p.properties:TList = New TList pworldlist.addlast(p:physicsworld) Return p:physicsworld End Function End Type the comments are explanation enough. So I have a few concerns. 1. I have never delt with function pointers. how do i declare/use/add them to a list. And can I pass parameters to them when I call the pointer. 2. Will this type of structure allow for maximum flexibility? what do you think? |
| ||
how do i declare/use/add them to a list. Create a Function Pointer container (a type) and add its instances to the listExample Strict Type FPointer Field Func:Int(X:float, Y:Float) End Type 'Create an instance: Local Pointer1:FPointer = New FPointer Pointer1.Func = MyFunction 'Store it in a list: Local L:Tlist = New TList L.AddFirst(Pointer1) 'Use from the list: (shoud print 15) Local Caller:FPointer = FPointer(L.First()) If caller <> Null And Caller.Func <> Null Then Print Caller.Func(10, 20) End Function MyFunction:Int(Value1:Float, Value2:Float) Return (Value1 + Value2)/2.0 End Function This is just a little stupid example. |
| ||
thanks ziggy! that was very helpful. I think i get pointers exxcept how does this line know to return a pointer and not the value of the function. What if the function for instance returns a pointer?Pointer1.Func = MyFunction |
| ||
To get the return value of a function, you have to write the (). Whithout them, the expression refers to the addres of the function. Pointer1.Func = MyFunction() would not work (you may be assigning a return value of kind integer to a function pointer). The only exceptions allowed are directs call. Single line with the expression MyFunction will call the function, instead of returning a error: Function pointer can't be directly invoked. This has been done this way to ensure backward compatibility with other BASIC dialects. By the way, a function that returns a function pointer, could be, as instance: Strict Function MyFunction:String() (MyValue:Int) Return AnotherFunction End Function Function AnotherFunction:String() End FunctionIn this example, the Function "MyFunction" gets a single INT parameter called MyValue, and returns a pointer to the function "AnotherFunction". Function MyFunction:String()(MyValue:int) End Function |
| ||
ie if you make a function pointer = some function name without brackets, it assigns the address of the function to the function pointer instead of calling the function. |