It is currently Fri May 24, 2013 11:37 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 42 posts ]  Go to page Previous  1, 2, 3
Author Message
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Thu Mar 10, 2011 12:25 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
Procedure.f Rad2degree(Rad.f) 
  myRad.f = (180 * Rad) / #PI
  ProcedureReturn myRad
EndProcedure


Code:
;purpose: To calculate the angle between 2 objects in 2D space
;input:    P1 - the location of the first object
;          P2 - the location of the second object
; output: the angle between the objects in degrees
Procedure.f calculateAngle2D(*P1.POINT, *P2.POINT, rad.d)
    Protected  ang.f = 0.0, calc.f = 0.0
    calc = (*P2\y - *P1\y)/ (*P2\x - *P1\x)    ;<--- Error in calculation spottet by Demivec. (Fixed).
    ang = ATan(calc) * radtodeg(rad)
    ;In the event that the angle is in the first quadrant
    If *P2\y < *P1\y And *P2\x > *P1\x
      ProcedureReturn ang
    ElseIf (*P2\y < *P1\y And *P2\x < *P1\x) Or (*P2\y > *P1\y And *P2\x < *P1\x)
    ;In the event of second Or third quadrant
       ProcedureReturn ang + 180
    Else
    ;If none of the above, it must be in the fourth
       ProcedureReturn ang + 360
    EndIf
EndProcedure

_________________
Try not to become a man of success but rather to become a man of value.


Last edited by DK_PETER on Wed Mar 16, 2011 2:07 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Thu Mar 10, 2011 12:28 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
Structure _3Dvector
  x.f
  y.f
  z.f
EndStructure

;purpose: To calculate the weight of an object based on its mass
;input:   mass - the mass of the object
;         grav - the amount of constant acceleration due To gravity
;output:  an structure with 3 floats representing the vector of weight
Procedure calcWeight3D(*obj._3Dvector, mass.f, grav.f)
    ;The value in y will be the only number changed, since gravity
    ;is only applied along the Y axis
    ;Calculate the weight, it is assumed that grav(ity) is a negative number
    *obj\y = mass * grav
    ProcedureReturn @Obj
EndProcedure


_________________
Try not to become a man of success but rather to become a man of value.


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Thu Mar 10, 2011 12:29 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;Purpose: To find the momentum of object with a given mass,travelling at a certain velocity
Procedure.f momentum(velocity.f, mass.f)
  Protected momentum.f
  momentum = mass*velocity
  ProcedureReturn momentum
EndProcedure

_________________
Try not to become a man of success but rather to become a man of value.


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Thu Mar 10, 2011 4:02 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Thu Jan 10, 2008 1:30 pm
Posts: 711
Location: Germany, Glienicke
Note:

You don't need your procedures Degree2Rad() and Rad2degree(), PB has Radian() and Degree()

_________________
Image


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Fri Mar 11, 2011 12:51 am 
Offline
Addict
Addict

Joined: Sat Jul 11, 2009 4:57 am
Posts: 878
Location: United States
Scale speed based on FPS:
Code:
speed*(60/fps))

_________________
▓▓▓▓▓▒▒▒▒▒░░░░░


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Fri Mar 11, 2011 8:17 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;Pass velocity And time and return resulting displacement.
Procedure.f calcDisplacement(velocity.f, _time.f)
      ProcedureReturn velocity * _time
EndProcedure

;Pass the old position , velocity And time and return new position specified by time.
Procedure.f calcPosition(oldPosition.f, velocity.f, _time.f)
      ProcedureReturn oldPosition + (velocity * _time)
EndProcedure

Procedure.f calcAvgerageVelocity(_start.f , _End.f, time.f)
      ProcedureReturn (_End - _start)/ time
EndProcedure

_________________
Try not to become a man of success but rather to become a man of value.


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Wed Mar 16, 2011 2:26 am 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Might be usefull to someone :)
Code:
;Ohms law procedure
Procedure.f Ohm_Law(value1.f, value2.f, CalcType.i = 0)
  Protected result.f
  Select CalcType
    Case 0     ;Voltage    -     value1 = current : value2 = resistance
      result = value1 * value2
    Case 1     ;Current    -     value1 = voltage : value2 = resistance
      result = value1 / value2
    Case 2     ;Resistance -     value1 = voltage : value2 = current
      result = value1 / value2
  EndSelect
  ProcedureReturn result
EndProcedure

_________________
Try not to become a man of success but rather to become a man of value.


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Wed Mar 16, 2011 2:56 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
To calculate the angle of an object from an arbitrary point (origin) in 2D space
Code:
;purpose: To calculate the angle of an object from an arbitrary point (origin) in 2D space
;input:    origin - the location of the origin
;          P1 - the location of the object
;output: the angle from the origin to the object in degrees
Procedure.f calculateRelativeAngle2D(*origin.POINT, *P1.POINT)
  Protected  ang.f, calc.f
  calc = (*P1\y - *origin\y) / (*P1\x - *origin\x)
  ang = Degree(ATan(calc))

  If *P1\y < *origin\y And *P1\x > *origin\x
    ProcedureReturn ang  ;angle is in the first quadrant
  ElseIf (*P1\y < *origin\y And *P1\x < *origin\x) Or (*P1\y > *origin\y And *P1\x < *origin\x)
    ProcedureReturn ang + 180 ;angle is in the second Or third quadrant
  Else
    ProcedureReturn ang + 360 ;angle is in the fourth quadrant
  EndIf
EndProcedure


I would've posted an option to return the results in radians but fumbled on my first few attempts to do so, maybe in the near future I'll update the code when I have a second to work out what needs to be done.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Sun Mar 20, 2011 2:05 am 
Offline
Addict
Addict
User avatar

Joined: Fri Sep 21, 2007 5:52 am
Posts: 2488
Location: New Zealand
first 3 moments of inertia for 2D particle physics, image analysis or a case where you need information on an object that that you haven't explicitly constructed.

Code:
;first 3 moments of inertia 2D
;   
;   1st moment, center of a mass

;   where
;       sumx + x
;       sum y + y
;   
;    cx = sumx / area
;    cy = sumy / area
;
;   2nd moment, inertia of mass around axis       

;   where
;       sumXX  +  (X*X)
;       sumYY  +  (Y*Y)   
;       sumXY  +  (X*Y)
;   
;   IX = SumXX - (Area * (cx * cx))
;   IY = SumYY - (Area * (cy * cy))
;   IXY = Sumxy - (Area * (cx * cy))
;
;   3rd moments, ra,rb, rc or shape parameter
;   *note the shape parameter is invariant to both scale and rotation for a given identical mass 
;    and can be used to identify shapes in some cases.
 
;   ta= Sqr((2.0 * ix * iY) - (4.0 *( ixy * ixy))/2.0)
;   Ra = (ix + iy) + ta
;   Rb = (ix + iy) - ta
;   Shape = (Ra / Rb)
;
;   orientation of mass   
;   
;       where
;         mmx  = Xmax - Xmin
;         mmy = Ymax - Ymin         
;
;     If ix = iy
;             orient = 45       
;     ElseIf mmx >= mmy     
;             orient = (0.5 * (ATan(2 * (Ixy / (Iy - ix))))) / (#PI / 180)
;     Else
;           orient = (0.5 * (ATan(2 * Ixy / (ix - Iy)))) / (#PI / 180)
;     EndIf

;example below

Structure moments
    ;moments
   ;1st 
    cx.f
    cy.f
    ;2nd
    ix.f
    iy.f
    ixy.f
    ;3rd
    ra.f
    rb.f
    shape.f
    ;orientation
    orient.f 
    ;elipse
    majoraxis.f
    minoraxis.f
    ;bounds
    Xmin.i
    Xmax.i
    Ymin.i
    Ymax.i
 EndStructure
 
Procedure GetMoments(img,x1,x2,y1,y2,color,*mom.moments)
  Protected x,y,px,area.f,sumX.f,sumY.f,sumXX.f,sumYY.f,sumXY.f
  Protected ta.f,mmx.f,mmy.f,cmx.f,cmy.f,orient.f
 
  *mom\Xmin = 99999
  *mom\Ymin = 99999
 
 If StartDrawing(ImageOutput(img))
 
   For x = x1 To x2
       For y = y1 To y2
     
        px = Point(x,y) 
     
         If px = color
           
              sumX + x 
              sumY + y 
              Area + 1   
              sumXX + (x*x)
              sumYY + (y*y) 
              sumXY + (x*y) 
                               
              If x < *mom\Xmin 
                 *mom\Xmin = x
              EndIf
               
              If x > *mom\XMax
                   *mom\Xmax = x
              EndIf
               
              If y < *mom\Ymin 
                 *mom\Ymin = y
              EndIf
               
              If y > *mom\Ymax
                 *mom\Ymax = y
              EndIf
            EndIf
          Next
     Next     
     
     StopDrawing()
     
     ;1st moments cx = centerX cy=centerY
     *mom\cx = sumx / Area
     *mom\cy = sumy / Area
 
     
     ;2nd moments Ix Iy Ixy 
     *mom\ix = SumXX - (Area * (cx * cx))
     *mom\iY = SumYY - (Area * (cy * cy))
     *mom\ixY = Sumxy - (Area * (cx * cy))
                   
     ;3rd moment shape parameter invarent to scale and rotation
     ta= Sqr((2.0 * *mom\ix * *mom\iY) - (4.0 *(*mom\ixy * *mom\ixy)) * 0.5)
           
     *mom\Ra = (*mom\ix + *mom\iy) + ta
     *mom\Rb = (*mom\ix + *mom\iy) - ta
     
     *mom\Shape = (*mom\Ra / *mom\Rb)
       
     mmx = (*mom\Xmax - *mom\Xmin)
     mmy = (*mom\Ymax - *mom\Ymin)
     cmx = (*mom\Xmax + *mom\Xmin) * 0.5
     cmy = (*mom\Ymax + *mom\Ymin) * 0.5
     dmx = *mom\cx - cmx
     dmy = *mom\cy - cmy
                   
     ;orientation       
      If *mom\ix = *mom\iy
             orient = 45       
      ElseIf mmx >= mmy
             orient = (0.5 * (ATan(2 * (*mom\Ixy / (*mom\iy - *mom\ix))))) / (#PI / 180)
             *mom\majoraxis = (mmx / Cos(Abs(orient) / (180 / #PI))) * 0.5
             *mom\minoraxis = (Area) / (#PI * *mom\majoraxis)
      Else
             orient = (0.5 * (ATan(2 * *mom\Ixy / (*mom\ix - *mom\Iy)))) / (#PI / 180)
             *mom\majoraxis = (mmy / Cos(Abs(orient) / (180 / #PI))) * 0.5
             *mom\minoraxis = (Area) / (#PI * *mom\majoraxis)
      EndIf
         
      If mmx = mmy  And Abs(*mom\ix- *mom\iy) < 1000
             orient = 0
      EndIf
     
      ;messy fudge to output  0 to 360 degrees
      If *mom\ixy > 0
           If *mom\iy > *mom\ix
                If dmx > dmy
                   orient = (360 + orient) / (180/#PI)
                Else
                   orient = (180 + orient) / (180/#PI)
                EndIf
             Else
                If dmx > dmy
                  orient = (90 - orient) / (180/#PI)
                Else
                  orient = (270 - orient) / (180/#PI)
                EndIf 
             EndIf
          Else
             If *mom\iy > *mom\ix
                If dmy > dmx
                  orient = (180 + orient) / (180/#PI)
                Else
                  orient = orient / (180/#PI)
                EndIf   
             Else
                If dmx > dmy
                  orient = (90 - orient) / (180/#PI)
                Else
                  orient = (270 - orient) / (180/#PI)
                EndIf   
             EndIf
        EndIf   
                   
      If orient < 0
            orient = 0
      EndIf     
     
      *mom\orient = orient
     
   EndIf   
         
 EndProcedure   



Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Mon Apr 25, 2011 11:49 pm 
Offline
Addict
Addict

Joined: Sat Jul 11, 2009 4:57 am
Posts: 878
Location: United States
Fractional Part function:
Code:
Procedure.f Frac(a.f)
   result.f = a.f - Round(a.f,#PB_Round_Down)
   ProcedureReturn result.f
EndProcedure

Debug frac(2003.5)

_________________
▓▓▓▓▓▒▒▒▒▒░░░░░


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Fri Aug 05, 2011 2:43 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Wed Oct 22, 2003 2:51 am
Posts: 734
Location: Canada
Hello everyone,

If you search for a General Maths Matrix calculation have a look here : http://www.purebasic.fr/english/viewtopic.php?f=12&t=47084&p=357864#p357864

Best regards.
Guimauve


Top
 Profile  
 
 Post subject: Re: Game Math (Post all your snippits and guides here). Than
PostPosted: Wed Jan 11, 2012 6:40 pm 
Offline
Addict
Addict

Joined: Fri Oct 23, 2009 2:33 am
Posts: 2863
Location: Wales, UK
Some interesting Physics for a car game: http://www.purebasic.fr/english/viewtopic.php?t=7116&view=previous

_________________
IdeasVacuum
If it sounds simple, you have not grasped the complexity.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 42 posts ]  Go to page Previous  1, 2, 3

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye