PureBasic Forumhttp://forums.purebasic.com/english/ Game Math (Post all your snippits and guides here). Thanks.http://forums.purebasic.com/english/viewtopic.php?f=16&t=45701 Page 2 of 4

 Author: DK_PETER [ Thu Mar 10, 2011 11:53 am ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:Structure vector2D  x.f  y.fEndStructureCode:;Degree to radiansProcedure.f Degree2Rad(degr.f)  myRad.f = (#PI * degr) / 180  ProcedureReturn myRadEndProcedurecalculatework() is repeated here for educational purposesCode: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  EndProcedureCode: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

 Author: DK_PETER [ Thu Mar 10, 2011 11:58 am ] Post subject: Re: Game Math (Post all your snippits and guides here). Than 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/s2Procedure.f calculatePotentialEnergy(mass.f ,height.f)  ;PE = Potential energy  PE.f = mass * #GRAVITY * height  ProcedureReturn PEEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:00 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;move an object based on its angles and distanceProcedure 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

 Author: DK_PETER [ Thu Mar 10, 2011 12:02 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than ;Example for Projectile TrajectoryCode: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_BOTTOMWhile(y_pos < SCREEN_BOTTOM); update position  x_pos + x_velocity  y_pos + y_velocity  ;update velocity  y_velocity + gravityWendEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:06 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;Structure for holding vectorStructure _2Dvector  x.f  y.fEndStructure;vector in magnitude/directionStructure _2Dvector_pol  mag.f  dir.fEndStructure;purpose:  To convert a vector from magnitude/direction To component form;input:     vec- a vector in magnitude/direction form;output:   our converted vectorProcedure 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 @tempEndProcedure;purpose:  To convert a vector from component To magnitude/direction form;input:    vec- a vector in component form;output:   our converted vectorProcedure 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 @tempEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:07 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;calculate the angular displacement given the arc length and radiusProcedure.f angleDisplacement(arc.f, radius.f)  theta.f  theta = arc/radius  ProcedureReturn thetaEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:08 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;Average Angular VelocityProcedure.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 omegaEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:10 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;Average angular accelerationProcedure.f avgAngAcceleration(angVelBegin.f, angVelEnd.f, time.f)  Protected alpha.f  alpha = (angVelEnd - angVelBegin)/time  ProcedureReturn alphaEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:11 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;Tangential velocityProcedure.f tangVelocity(omega.f, radius.f)  Protected  velT.f  velT = omega*radius  ProcedureReturn velTEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:13 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than ;Procedure that will calculate the linear speed of a ball given the height, mass, And inertiaCode: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 linearSpeedEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:15 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:Structure quaternion  x.f  y.f  z.f  w.fEndStructure;here is a procedure that converts from Euler angles (roll, pitch, And yaw) To quaternionProcedure 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

 Author: DK_PETER [ Thu Mar 10, 2011 12:16 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;input:  P1 – representing point 1;          P2 – representing point 2;output: the slope between our 2 pointsProcedure.f slopeBetweenPoints(*P1.POINT, *P2.POINT)  Protected temp.f  temp = (*P2\y - *P1\y) / (*P2\x - *P1\x)  ProcedureReturn tempEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:17 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:;purpose: calculate the midpoint of a line segment;input: P1- representing point 1;       P2- representing point 2;output: midpoint between the two pointsProcedure 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 @tempEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:19 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:Structure vector3D  x.f  y.f  z.fEndStructure;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 pointsProcedure.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 @tempEndProcedure

 Author: DK_PETER [ Thu Mar 10, 2011 12:20 pm ] Post subject: Re: Game Math (Post all your snippits and guides here). Than Code:Structure sphere  x.f  y.f  z.f  radius.fEndStructure;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 falseProcedure  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 valueEndProcedure

 Page 2 of 4 All times are UTC + 1 hour Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/