It is currently Fri Jan 24, 2020 8:57 pm

 All times are UTC + 1 hour  Page 2 of 4 [ 52 posts ] Go to page Previous  1, 2, 3, 4  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 11:53 am Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
Location: Denmark
Code:
Structure vector2D
x.f
y.f
EndStructure

Code:
myRad.f = (#PI * degr) / 180
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....

;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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 11:58 am Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:00 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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)
EndProcedure

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:02 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:06 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:07 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
Location: Denmark
Code:
;calculate the angular displacement given the arc length and radius
theta.f
ProcedureReturn theta
EndProcedure

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:08 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
Location: Denmark
Code:
;Average Angular Velocity
Protected initialDisplacement.f, endDisplacement.f,omega.f
;calculate the angular displacement.
omega = (endDisplacement - initialDisplacement) / time
ProcedureReturn omega
EndProcedure

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:10 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:11 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
Location: Denmark
Code:
;Tangential velocity
Protected  velT.f
ProcedureReturn velT
EndProcedure

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:13 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:15 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:16 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:17 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:19 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Post subject: Re: Game Math (Post all your snippits and guides here). Than Posted: Thu Mar 10, 2011 12:20 pm Addict  Joined: Sat Feb 19, 2011 10:06 am
Posts: 812
Location: Denmark
Code:
Structure sphere
x.f
y.f
z.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

_________________
“Tell me and I forget. Teach me and I remember. Involve me and I learn.”
— Benjamin Franklin
Current configurations: Windows 10, Intel 6800K, GeForce Gtx 1060, 32 gb ram.

Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending  Page 2 of 4 [ 52 posts ] Go to page Previous  1, 2, 3, 4  Next

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 4 guests

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

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite