It is currently Wed May 22, 2013 7:54 am

All times are UTC + 1 hour




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

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


Code:
;Degree to radians
Procedure.f Degree2Rad(degr.f)
  myRad.f = (#PI * degr) / 180
  ProcedureReturn myRad
EndProcedure


calculatework() is repeated here for educational purposes

Code:
Procedure.f calculateWork(force.f, friction.f, displacement.f)
 
    ;calculate the difference of the forces.
    netForce.f = force-friction

    ;multiply by displacement
    temp.f = displacement * netForce
    ; Returns the value of the work in Joules
    ProcedureReturn temp 
EndProcedure


Code:
Procedure.f calculateAngledWork(*obj.vector2D, friction.f, displacement.f)
    ;don't forget to convert to rads....
    temp.f = Cos(Degree2Rad(*obj\y))
   
    ;calculate the horizontal force;
    horizForce.f = *obj\x * temp

    work.f = calculateWork(horizForce,friction, displacement) ; Procedure is above an previous post

    ProcedureReturn work ;return the amount of work done considering an angled force:
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 11:58 am 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;calculation For potential energy
#GRAVITY = 9.81 ;
; Example: A book weighs 1.5 pounds, and it is raised 2 meters off the ground
; formula: 1.5lbs (1N/0.2248lbs)  -  1N = 1kg*m/s2
Procedure.f calculatePotentialEnergy(mass.f ,height.f)
  ;PE = Potential energy
  PE.f = mass * #GRAVITY * height
  ProcedureReturn PE
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:00 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;move an object based on its angles and distance
Procedure OrbitCalc3D(CenterX.f, CenterY.f, CenterZ.f, AngleX.f, AngleY.f, Radius.i)
  X = CenterX + Radius*Cos(AngleY)*Cos(AngleX)
  Y = CenterY + Radius*Sin(AngleX)
  Z = CenterZ + Radius*Sin(AngleY)*Cos(AngleX)
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:02 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
;Example for Projectile Trajectory
Code:
Procedure Projectile_Trajectory(Screen_Bottom.i,Initial_vel.f,Initial_Angle.f)
  x_pos.f = 0                         ;starting point of projectile
  y_pos.f = Screen_Bottom      ;bottom of screen
  y_velocity.f = 0                   ;initial y velocity
  x_velocity.f = 0                   ;constant x velocity
  gravity.f = 1                       ;do want To fall too fast
  velocity.f = Initial_vel           ;whatever
  angle.f = Initial_Angle           ;whatever, must be in radians
                                          ;compute velocities in x,y
  x_velocity = velocity*Cos(angle)
  y_velocity = velocity*Sin(angle)
                                          ;do projectile loop Until object hits
                                          ;bottom of screen at SCREEN_BOTTOM
While(y_pos < SCREEN_BOTTOM)
; update position
  x_pos + x_velocity
  y_pos + y_velocity
  ;update velocity
  y_velocity + gravity
Wend
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:06 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;Structure for holding vector
Structure _2Dvector
  x.f
  y.f
EndStructure

;vector in magnitude/direction
Structure _2Dvector_pol
  mag.f
  dir.f
EndStructure

;purpose:  To convert a vector from magnitude/direction To component form
;input:     vec- a vector in magnitude/direction form
;output:   our converted vector
Procedure v2Dvector_comp_PolarToCompConversion(*vec._2Dvector_pol)
  ;temporary variable which will hold the answer
  temp._2Dvector
  ;Fill in our values
  temp\x = *vec\mag * Cos(*vec\dir * #PI / 180)
  temp\y = *vec\mag * Sin(*vec\dir * #PI / 180)
  ProcedureReturn @temp
EndProcedure

;purpose:  To convert a vector from component To magnitude/direction form
;input:    vec- a vector in component form
;output:   our converted vector
Procedure v2Dvector_polar_CompToPolarConversion(*vec._2Dvector)
  ;temporary variable which will hold our answer
  temp._2Dvector_pol
  ;Calculate our magnitude using the Pythagorean theorom
  temp\mag = Sqr(*vec\x * *vec\x + *vec\y * *vec\y)
  ;Error check To prevent a divide-by-zero
  If temp\mag = 0
    ProcedureReturn @temp
  EndIf
  ;Calculate our angle. We are using ASin() which will Return an angle
  ;in either the 1st Or the 4th quadrant
  temp\dir = (180 / #PI) * ASin(*vec\y / temp\mag)
  ;Adjust our angle in the event that it lies in the 2nd Or 3rd quadrant
  If *vec\x < 0
    temp\dir + 180
    ;Adjust our angle in the event that it lies in the 4th quadrant
  ElseIf *vec\x > 0 And *vec\y < 0
    temp\dir + 360
  EndIf

  ProcedureReturn @temp
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:07 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;calculate the angular displacement given the arc length and radius
Procedure.f angleDisplacement(arc.f, radius.f)
  theta.f
  theta = arc/radius
  ProcedureReturn theta
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:08 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;Average Angular Velocity
Procedure.f avgAngularVelocity(arcStart.f, arcEnd.f, time.f, radius.f)
  Protected initialDisplacement.f, endDisplacement.f,omega.f
  ;calculate the angular displacement.
  initialDisplacement = arcStart/radius
  endDisplacement = arcEnd/radius
  omega = (endDisplacement - initialDisplacement) / time
  ProcedureReturn omega
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:10 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;Average angular acceleration
Procedure.f avgAngAcceleration(angVelBegin.f, angVelEnd.f, time.f)
  Protected alpha.f
  alpha = (angVelEnd - angVelBegin)/time
  ProcedureReturn alpha
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:11 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;Tangential velocity
Procedure.f tangVelocity(omega.f, radius.f)
  Protected  velT.f
  velT = omega*radius
  ProcedureReturn velT
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:13 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
;Procedure that will calculate the linear speed of a ball given the height, mass, And inertia
Code:
Procedure.f LinearSpeed(mass.f, initialHeight.f, inertia.f)
  Protected energy.f = 0.0, halfMass.f, halfInertiaMass.f, linearSpeed.f ,temp.f = 0.0
  ;first figure out what is known For sure.
   energy = mass*initialHeight*#GRAVITY ; Define GRAVITY yourself..
  ;this term is used To hold the math equivalent of 1/2(m)vf^2
   halfMass = mass/2
  ;this term hold on To the formula equivalent of
  ;1/2(inertia)*(mass) r^2 * wf^2
  halfInertiaMass = inertia*mass/2
  ;make a holding place.
  temp = energy/(halfMass+halfInertiaMass)
  ;take the square root To find the speed in m/s
  linearSpeed = Sqr(temp)
  ProcedureReturn linearSpeed
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:15 pm 
Offline
User
User

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

;here is a procedure that converts from Euler angles (roll, pitch, And yaw) To quaternion
Procedure EulerToQuat(roll.f, pitch.f, yaw.f, *quat.quaternion)
  Protected cr.f, cp.f, cy.f, sr.f, sp.f, sy.f, cpcy.f, spsy.f
  ;compute all trigonometric values used To compute the quaternion
  cr = Cos(roll/2)
  cp = Cos(pitch/2)
  cy = Cos(yaw/2)

  sr = Sin(roll/2)
  sp = Sin(pitch/2)
  sy = Sin(yaw/2)

  cpcy = cp * cy
  spsy = sp * sy

  ;combine values To generate the vector And scalar For the quaternion
  *quat\w = cr * cpcy + sr * spsy
  *quat\x = sr * cpcy - cr * spsy
  *quat\y = cr * sp * cy + sr * cp * sy
  *quat\z = cr * cp * sy - sr * sp * cy
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:16 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;input:  P1 – representing point 1
;          P2 – representing point 2
;output: the slope between our 2 points
Procedure.f slopeBetweenPoints(*P1.POINT, *P2.POINT)
  Protected temp.f
  temp = (*P2\y - *P1\y) / (*P2\x - *P1\x)
  ProcedureReturn temp
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:17 pm 
Offline
User
User

Joined: Sat Feb 19, 2011 10:06 am
Posts: 51
Location: Denmark
Code:
;purpose: calculate the midpoint of a line segment
;input: P1- representing point 1
;       P2- representing point 2
;output: midpoint between the two points
Procedure findThe2DMidPoint(*P1.POINT, *P2.POINT)
  Protected temp.POINT
  ;Calculate our midpoint
  temp\x = (*P1\x + *P2\x) / 2.0
  temp\y = (*P1\y + *P2\y) / 2.0
  ProcedureReturn @temp
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:19 pm 
Offline
User
User

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

;purpose: calculate the midpoint of a line segment in 3D
;input: P1- point 1 (x,y,z)
;       P2- point 2
;output: the midpoint between the two points
Procedure.f findThe3DMidPoint(*P1.vector3D, *P2.vector3D)
    Protected temp.vector3D
    ;Calculate our midpoint
    temp\x = (*P1\x + *P2\x) / 2.0
    temp\y = (*P1\y + *P2\y) / 2.0
    temp\z = (*P1\z + *P2\z) / 2.0
    ProcedureReturn @temp
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:20 pm 
Offline
User
User

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

;purpose: To detect a collision between 2 spheres
; input:   S1- our first sphere
;          S2- our second sphere
;output: true If there is a collision, Else false
Procedure  CollisionBetweenSpheres(*S1.sphere, *S2.sphere)
    Protected value
    value = (Pow(*S2\x - *S1\x,2) + Pow(*S2\y - *S1\y,2) + Pow(*S2\z - *S1\z,2) < Pow(*S1\radius + *S2\radius,2))
  ProcedureReturn value
EndProcedure

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


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

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


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