The MaxPhysics Project
The vision is to create a solid expandable and flexible 2D physics module for Blitzmax, which will easily be expandable into a 3D version in the future. It is going to be public and open source. Anyone can add their improvements and suggest ideas and tips.
Here is a first topic to focus on the project plan and overall ideas about the project. Here I want everyone who is interested to discuss what they would want from the Physics Module. - Or perhaps we should split it up into more than one module?
--- Points to Decide upon ---
[ ] Find a Name for the Project. Key words: Physics, Vectors. Right now it's "MaxPhysics"
[ ] How is code management going to work? How can we avoid the problem that more than two may be working on the same part? I suggest one file for each type (not always though).
[ ] What about coding styles? Guidelines to make the code similar (as much as possible) for everyone in the project, includes how and where to add comments and formation of the code. - Right now you should try to follow my style in the vector library (see some post below), comments are welcome.
[ ] Referense List
Some about Collisions /f Visual Basic
Flipcode - Collison detection in 3D
N collision, Vector collision /w source in Flash
Hugo - Physics, Effects and Collisons
[a ] no link [/a]
TO DO:
[100%] Finnish the project plan and start the actual project in another topic
Here is my first overview of the project plan, I will update this according to suggestions.
Project Plan - MaxPhysics
[ ] Based upon a solid vector library
[ ] Types: Vectors,Lines,Polygons?,Rects,Cricles
[ ] Collision Checks (above types)
[ ] Collision Response (above types)
Requires we know the point of impact and the speed of impact. With that we could choose anything from perfect slide to perfect bounce, depending on settings.
[ ] Collision Response :Physics Object
When two "lose" objects collide, take momentum into account. These physics objects have a Mass,Position,Velocity,Acceleration. Any object in your game that you want to use with collisions can be derivied from this "physicsObject" and using one (or more?) of the collision types. For example:
Ship extends MaxPhysicsObject
New()
CollisionShape = new Circle( ShipSize )
Perhaps we can also include torque here so that rotation is affected by collisions to.
[ ] Examples and Documentation!
This is very important so that everyone can use this module with ease!
End Notes:
The Physics Library is supposed to be very simple to implement and use, while being as general purpose as possible.
--------- WORK DONE SO FAR ---------
The Second version of the new improved vector library is done. I haven't added anything with physics or collision or lines yet. Here is the start:
Thanks to Haramanai for conversion to real cartesian coordinates.
Anyone may add and improve to this. Make sure that you, in the comments at the top of the file, write what have changed and what adition you have done or bugs you have discovered, perhaps even fixed.
The vision is to create a solid expandable and flexible 2D physics module for Blitzmax, which will easily be expandable into a 3D version in the future. It is going to be public and open source. Anyone can add their improvements and suggest ideas and tips.
Here is a first topic to focus on the project plan and overall ideas about the project. Here I want everyone who is interested to discuss what they would want from the Physics Module. - Or perhaps we should split it up into more than one module?
--- Points to Decide upon ---
[ ] Find a Name for the Project. Key words: Physics, Vectors. Right now it's "MaxPhysics"
[ ] How is code management going to work? How can we avoid the problem that more than two may be working on the same part? I suggest one file for each type (not always though).
[ ] What about coding styles? Guidelines to make the code similar (as much as possible) for everyone in the project, includes how and where to add comments and formation of the code. - Right now you should try to follow my style in the vector library (see some post below), comments are welcome.
[ ] Referense List
Some about Collisions /f Visual Basic
Flipcode - Collison detection in 3D
N collision, Vector collision /w source in Flash
Hugo - Physics, Effects and Collisons
[a ] no link [/a]
TO DO:
[100%] Finnish the project plan and start the actual project in another topic
Here is my first overview of the project plan, I will update this according to suggestions.
Project Plan - MaxPhysics
[ ] Based upon a solid vector library
[ ] Types: Vectors,Lines,Polygons?,Rects,Cricles
[ ] Collision Checks (above types)
[ ] Collision Response (above types)
Requires we know the point of impact and the speed of impact. With that we could choose anything from perfect slide to perfect bounce, depending on settings.
[ ] Collision Response :Physics Object
When two "lose" objects collide, take momentum into account. These physics objects have a Mass,Position,Velocity,Acceleration. Any object in your game that you want to use with collisions can be derivied from this "physicsObject" and using one (or more?) of the collision types. For example:
Ship extends MaxPhysicsObject
New()
CollisionShape = new Circle( ShipSize )
Perhaps we can also include torque here so that rotation is affected by collisions to.
[ ] Examples and Documentation!
This is very important so that everyone can use this module with ease!
End Notes:
The Physics Library is supposed to be very simple to implement and use, while being as general purpose as possible.
--------- WORK DONE SO FAR ---------
The Second version of the new improved vector library is done. I haven't added anything with physics or collision or lines yet. Here is the start:
Thanks to Haramanai for conversion to real cartesian coordinates.
Anyone may add and improve to this. Make sure that you, in the comments at the top of the file, write what have changed and what adition you have done or bugs you have discovered, perhaps even fixed.
Strict Rem File: "Vector2D.bmx" 2D Vector Module The Vector Module which is a part of the Public MaxPhysics PROJECT VERSION HISTORY: ADDITIONS: Added a method: Plus( value!) - increases a vectors leangth with a scalar FIXES: Fixed: Now using the real cartesian coordinate system TO DO: Each method should have a equivalent function only 3-4 are done. EndRem Type TShape Abstract EndType ' / / / / / / / / / / / / / / / / ' T V E C T O R '------------------------------------------------------------------------ Type Vector2D Extends TShape' Field X!,Y! '=============== ' BASIC COMMANDS '=============== ' C R E A T E V E C T O R '----------------------------------------------- Function Create:Vector2D( X!, Y! ) Local Vector:Vector2D Vector = New Vector2D Vector.X! = X! Vector.Y! = Y! Return Vector End Function ' C R E A T E V E C T O R F R O M '---------------------------------------------- Function CreateFrom:Vector2D( Position1:Vector2D , Position2:Vector2D ) Local Vector:Vector2D = Create(0,0) Vector.VectorFrom( Position1, Position2 ) Return Vector EndFunction ' C R E A T E /w D I E C T I O N '---------------------------------------------- ' I don't know any good name for this method Function CreateField:Vector2D( Length , Direction ) Local Vector:Vector2D = Create(1,0) Vector.SetLength( Length ) Vector.SetDirection( Direction ) Return Vector EndFunction ' C O P Y V E C T O R '---------------------------------------------- Method Copy:Vector2D()' Return Create( X , Y ) End Method ' S E T V E C T O R '---------------------------------------------- Method Set( newX! , newY! )' X! = newX! Y! = newY! End Method ' S E T D I R E C T I O N '---------------------------------------------- Method SetDirection( Angle! )' Local Length! = Length() X = Cos( Angle! )*Length Y = -Sin( Angle! )*Length End Method Method SetDir( Angle! ) SetDirection( Angle ) EndMethod ' S E T L E N G T H '---------------------------------------------- Method SetLength( Length! ) 'If we want to set vector to zero If Length = 0 Set(0,0);Return 'If the new length is negative assume we want to If Length < 0 Turn180() 'Reverse Local Angle! = Angle()'Of this Vector X = Cos(Angle) * Length Y = -Sin(Angle) * Length EndMethod Method SetMagnitude( Length! ) SetLength( Length ) EndMethod ' G E T L E N G T H '---------------------------------------------- Method Length!() Return Sqr( X*X + Y*Y )' EndMethod 'Alternative Names - Use whatever you like Method Magnitude!() Return Length() EndMethod Method GetLenght!() Return Length() EndMethod Method GetMagnitude!() Return Length() EndMethod ' G E T L E N G T H S Q U A R E D '---------------------------------------------- Method LengthSquared() Return ( X*X + Y*Y )' EndMethod ' G E T D I R E C T I O N '---------------------------------------------- Method Direction!() Return ATan2(-y , x) End Method 'Also Alternative Names - Use whatever you like Method GetDirection!() Return Direction() EndMethod Method Dir!() Return Direction() EndMethod Method GetAngle!() Return Direction() EndMethod Method Angle!() Return Direction() EndMethod 'And alternative to get 0<= Angle <360 Method Dir360!() Local Angle! = Direction!() If Angle < 0 Then Angle:+360 Return Angle End Method ' R E V E R S E V E C T O R '---------------------------------------------- Method Reverse() 'or Turn 180 Degrees X = -X Y = -Y End Method Method Turn180() Reverse() EndMethod ' V E C T O R F R O M '---------------------------------------------- Method VectorFrom( Position1:Vector2D , Position2:Vector2D) X = ( Position2.X - Position1.X ) Y = ( Position2.Y - Position1.Y ) 'Change the vector into a vector from Position1 to Position2 EndMethod '=============== ' MATH COMMANDS '=============== ' A D D '---------------------------------------------- Method Add( Vector:Vector2D ) X:+ Vector.X Y:+ Vector.Y EndMethod ' A D D /w D E L T A T I M E '---------------------------------------------- Method AddDelta( Vector:Vector2D ) Add( Vector ) MultiplyDeltaTime() EndMethod ' A D D C O P Y '---------------------------------------------- ' Add two Vectors and return the result as a ' third vector. Method AddCopy:Vector2D( Vector:Vector2D ) Local NewVector:Vector2D NewVector = Self.Copy() NewVector.Add( Vector ) Return NewVector EndMethod ' A D D N E W /w D E L T A T I M E '---------------------------------------------- Method AddDeltaCopy:Vector2D( Vector:Vector2D ) Local NewVector:Vector2D NewVector = AddCopy( Vector ) NewVector.MultiplyDeltaTime() Return NewVector EndMethod Method Plus( Value:Float )'Add a value to the vectors length Local Angle:Float = GetAngle() X:+ Cos( Angle ) * Value Y:- Sin( Angle ) * Value End Method ' R O T A T E '---------------------------------------------- Method Rotate( Angle! ) Local CurrentAngle! = Direction() Local Length! = Length() X = Cos( CurrentAngle + Angle ) * Length Y = -Sin( CurrentAngle + Angle ) * Length EndMethod Method AddAngle( Angle! ) Rotate( Angle ) EndMethod ' S U B T R A C T '---------------------------------------------- Method Subtract( Vector:Vector2D ) X:- Vector.X Y:- Vector.Y 'This would also work 'Self.Add( Vector.Copy().Reverse() ) EndMethod ' D O T P R O D U C T '---------------------------------------------- Method DOT!( Vector:Vector2D ) Return ( X * Vector.X + Y * Vector.Y) EndMethod 'Alternative Name Method DotProduct!( Vector:Vector2D ) Return Self.DOT( Vector ) EndMethod ' M U L T I P L Y V E C T O R '---------------------------------------------- Method Multiply( Value! ) X:*Value Y:*Value EndMethod ' M U L T I P L Y /w Delta Time '---------------------------------------------- ' Method MultiplyDeltaTime() Self.Multiply( Delta.Time() ) EndMethod ' N O R M A L I Z E '---------------------------------------------- Method Normalize() Local Length! = Length() If Length = 0 Return'Don't divide by zero Set( (X / Length), ( Y / Length) ) 'Make length = 1 End Method ' U N I T V E C T O R '---------------------------------------------- Method Unit:Vector2D() Local Vector:Vector2D Vector = Self.Copy() Vector.Normalize() Return Vector'Returns a New vector with length = 1 End Method '=============== ' DEBUG COMMANDS '=============== ' D R A W V E C T O R '-------------------------------------------------- Method DrawModify( From:Vector2D , Multiply! , Xtra ) DrawLine From.X + Xtra, From.Y, From.X + Xtra + X* Multiply , From.Y + Y* Multiply EndMethod Method DrawXY( FromX, FromY, Multiply! , Xtra ) DrawModify( Point(FromX, FromY) , Multiply! , Xtra ) End Method ' D R A W R E A L V E C T O R '-------------------------------------------------- Method Draw( Origin:Vector2D =Null ) If Origin = Null Origin = Create( 0, 0 ) DrawModify( Origin , 1 , 0 ) EndMethod ' D R A W V E C T O R D A T A '----------------------------------------------- Method DrawData( sLen, sDir, X, Y ) Local Row = 0 If sLen DrawText "Length : "+Length(),X,Y +Row*15; Row:+1 If sDir DrawText "Dir : " +Dir360(),X,Y +Row*15; Row:+1 End Method EndType ' / / / / / / / / / / / / / / / / '------------------------------------------------------------------------ ' C R E A T E V E C T O R '----------------------------------------------- 'Purpose: Creates a New Vector 'Parameters: X = Vectors X value ' Same For Y 'Returns: New Vector Type '----------------------------------------------- Function CreateVector:Vector2D( X!=0, Y!=0 ) Return Vector2D.Create( X, Y ) EndFunction ' C O P Y V E C T O R '---------------------------------------------- 'Purpose: Copies a Vector into a New vector 'Parameters: VECTOR 'Returns: a VECTOR, exact copy of first VECTOR '----------------------------------------------- Function CopyVector() EndFunction ' V E C T O R D I R E C T I O N '---------------------------------------------------- 'Purpose: Calculates the direction of a vector 'Parameters: VECTOR 'Returns: Degrees 'Note on Angel: 0 is Left, 90 is down (BlitzStyle) 'Note LastDir: If Length = 0 this Function returns LastDir '--------------------------------------------------------- Function VectorDirection!( Vector:Vector2D ) EndFunction ' D O T P R O D U C T '--------------------------------------------------- 'Purpose: Calculated the Dot-Product of Two Vectors 'Parameters: Two vectors you want to "Dot" 'Returns: The result '--------------------------------------------------- Function Dot!(Vector:Vector2D,Vector2:Vector2D) Return Vector.DOT( Vector2 ) End Function 'Alternative Naming (Both these works the same) Function DotProduct!(Vector:Vector2D,Vector2:Vector2D) Return Vector.DOT( Vector2 ) End Function ' N O R M A L I Z E '---------------------------------------------- 'Purpose: Sets Vector length To ONE but keeps ' it's direction 'Parameters: VECTOR to normalize '----------------------------------------------- Function Normalize() EndFunction ' U N I T V E C T O R '---------------------------------------------- 'Note: This is same as Normalizing a vector ' except that this function does not alter the ' original vector. Instead it return a new vector Function Unit() EndFunction ' D R A W V E C T O R '__________________________________________________ 'Purpose: Draws the vector from the specifed point 'Parameters: X,Y location '-------------------------------------------------- Function DrawVector( Vector:Vector2D, From:Vector2D , Multiply!=20 , Xtra=0 ) Vector.DrawModify( From , Multiply! , Xtra ) EndFunction ' D R A W V E C T O R D A T A '----------------------------------------------- 'Purpose: Prints the data of a vector 'Parameters: sLen=ShowLength, SDir=ShowDirection ' X and Y = Where to start draw the data (text) '----------------------------------------------- Function DrawVectorData( Vector:Vector2D, sLen=True, sDir=False, X=10, Y=20 ) Vector.DrawData( sLen, sDir, X, Y ) EndFunction Function DrawVectorInfo( Vector:Vector2D, sLen=True, sDir=False, X=10, Y=20 ) Vector.DrawData( sLen, sDir, X, Y ) EndFunction Rem If you ever need a function to check something of a point you can use the vector to check against (position vector for example) Ex drawline( Point(50,100), ShipPosition:Vector ) EndRem Function Point:Vector2D( X! , Y! ) Local Vector:Vector2D= New Vector2D Vector.X = X Vector.Y = Y Return Vector EndFunction Function DrawLineToPointFromOrigin( Start:Vector2D, Origin:Vector2D ) DrawLine Start.X, Start.Y, Start.X + Origin.X , Start.Y + Origin.Y EndFunction ' / / / / / / / / / / / / / / / / ' D E L T A T I M E '------------------------------------------------------------------------ Type Delta ' '----------------------------------------------------- ' This type is Fully global. Which means you'll always ' refere to it as Delta.Start() , Delta.Time(), ' Delta.Update() ' ' You shouldn't create instances of this type! ' DeltaTime is same for all your objects! ' Global DeltaTime! Global TimeDelay% 'Run this once before your main loop 'If you don't there will be a jump in deltatime Function Start() TimeDelay = MilliSecs() End Function 'Everytime where you want to get the deltatime 'call this: Delta.Time 'You should multiply this to every place which 'adds to you position/speed/acceleration ' ' The basic rule is to add deltatime to all ' things which gets added to every frame ' Like Speed:+ 10*Delta.Time ' ' If you use the Vector Library simply use ' AddDelta() instead of Add() ' And ' AddDeltaNew() Instead of AddNew() Function Time!() Return DeltaTime! End Function 'Put this once in your main loop 'it calculates the current deltatime 'depeding on your framerate or more exact 'the time between each/the last frame. Function Update() '_____________________________________________________ 'Purpose: Calculates DeltaTime , put it in mainloop '----------------------------------------------------- DeltaTime = ( MilliSecs()- TimeDelay )*0.001 TimeDelay = MilliSecs() EndFunction End Type ' / / / / / / / / / / / / / / / / '----------------------------------------------------------------------------------------