I dream of a dedicated Forum entry called Game Math..

Advanced game related topics
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

I dream of a dedicated Forum entry called Game Math..

Post by DK_PETER »

Greeting folks.

I've just begun to fiddle with game creation again. I haven't touched the subject
in 20 years, so my math are (at best) extremely rusty.
Well, my point is this: Event though I can figure most of the math out, there
are situation, when it simply doesn't make any sense to me...(Age related I think) :)

Doing a search forum wide for a specific explanation or example
can be pretty tedious and many times the search returns nada either due to
wrong words used or the specific topic is embedded into waaaay too much
other code, that it may not be recognized as what you need.

My hope is, that all you excellent and helpfull users out there would appreciate
this idea.

Well, I cannot just ask without providing myself, so I've dusted off my ooold codes
and tried to convert it to Purebasic:

It's a mix of everything and hopefully it will work as expected.
Please feel free to point out/correct typoes, mistakes, or wrong conversion. But please, do not
flame me..I did the best I could.
Code looks a bit messy and is a direct translation from C/C++..

I hope a dedicated area will be made just for small Game Math issues/examples/snippits.

Best regards
Peter.

Oh...Boy..Here we go :)

Code: Select all

; 
Surface Friction (mK)   Static Friction (mS)  Kinetic
;-------------------------------------------------------
; Steel on steel (dry)           0.6              0.4
; Steel on steel (greasy)        0.1              0.05
; Teflon on steel                0.041            0.04
; Brake lining on cast iron      0.4              0.3
; Rubber on concrete (dry)       1.0              0.9
; Rubber on concrete (wet)       0.30             0.25
; Metal on ice                   0.022            0.02
; Steel on steel                 0.74             0.57
; Aluminum on steel              0.61             0.47
; Copper on steel                0.53             0.36
; Nickel on nickel               1.1              0.53
; Glass on glass                 0.94             0.40
; Copper on glass                0.68             0.53
; Oak on oak (parallel To grain) 0.62             0.48
; Oak on oak (perpendicular To grain) 0.54        0.32
 

; List of Conversion Factors Length   Time
; 1m = 39.37in = 3.281ft              1s = 1000ms
; 1in = 2.54cm                        1min = 60s
; 1km = 0.621mi                       1hr = 3600s
; 1mi = 5,280ft = 1.609km             1 year = 365.242 days
 
Structure vector2D
  x.f
  y.f
EndStructure
Structure vector3D
  x.f
  y.f
  z.f
EndStructure

Procedure.d radtodeg(Rad.d)
  ProcedureReturn Rad * 180 / #PI
EndProcedure

Procedure.d PointDirection(x.d, y.d, x2.d, y2.d)
  ProcedureReturn radtodeg(ATan2(y-y2, x2-x))
EndProcedure

Procedure OrgMath()
Define x.d, y.d

Angle = 45
AngleRad = Radian(Angle)

x + Sin(AngleRad)
y - Cos(AngleRad)

EndProcedure


;returns the distance between two supplied points p and q
Procedure.d distance(*p.POINT, *q.POINT)
 dx.d = *p\x - *q\x;                  ;horizontal difference 
 dy.d = *p\y - *q\y;                  ;vertical difference 
 dist.d = Sqr(dx*dx + dy*dy )         ;distance using Pythagoras theorem
 ProcedureReturn dist;
EndProcedure

;distance from a point to a vertical line
Procedure.i FindDist2VertLine(px.f,lx.f)
  distance = Abs(Px) - Abs(Lx)
  ProcedureReturn distance
EndProcedure

;distance from a point to a horizontal line
Procedure.i FindDist2HorizLine(py.f,ly.f)
  distance = Abs(Py) - Abs(Ly)
  ProcedureReturn distance
EndProcedure

;Find the slope of a line
Procedure.i FindSlopeOfLine(*p1.POINT,*p2.POINT)
slope = (*p1\y - *p2\y) / (*p1\x + *p2\x)
EndProcedure

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

;Radians to degree
Procedure.f Rad2degree(Rad.f)
  myRad.f = (180 * Rad) / #PI
  ProcedureReturn myRad
EndProcedure

;/////////////////////////////////////////////////
;  VELOCITY, ACCELERATION - MOVEMENT
;/////////////////////////////////////////////////
;purpose: calculate final velocity, given initial velocity, acceleration and time
; input:   vi initial velocity
;          a  acceleration
;          t  time
; output:  our final velocity
Procedure.f  eqOne_vf(vi.f, a.f, t.f)
  ProcedureReturn vi + a * t
EndProcedure

;purpose: calculate change in distance, given final velocity, initial
;          velocity and time
; input:   vf final velocity
;          vi initial velocity
;          t  time
; output:  our change in distance
Procedure.f eqTwo_x(vf.f, vi.f, t.f)
  ProcedureReturn  0.5 * (vf - vi) /t
EndProcedure

;purpose: calculate change in distance, given initial velocity,
;           acceleration andnd time
; input:   vi initial velocity
;          t  time
;          a  acceleration
; output:  our change in distance
Procedure.f eqThree_x(vi.f, t.f, a.f)
  ProcedureReturn vi * t + 0.5 * a * t * t
EndProcedure


;purpose: To determine whether an object on an incline will slide
; input:   angle - the current angle of the incline
;          weight - the weight of the object
;          fric_coeff - the coefficient of Static friction between surfaces
; output:  true If the object should slide, Else false
Procedure checkForMotion(angle.f, weight.f, fric_coeff.f)
    ; Calculate the normal force being exerted by the ramp
    normal.f = weight * Cos(angle * #PI / 180)
    ; Calculate the force perpendicular To the normal
    perpForce.f = weight * Sin(angle * #PI / 180)
    ; Calculate the amount of Static friction keeping the object at rest
    stat_friction.f = fric_coeff * normal
    ; Return true If the object should slide, Else false
    ProcedureReturn perpForce > stat_friction
EndProcedure

;purpose: To determine whether an object on an incline will slide
; input:   angle - the current angle of the incline
;          weight - the weight of the object
;          fric_coeff - the coefficient of kinetic friction between surfaces
;          mass - the mass of the object
; output:  the acceleration of the object
Procedure.f calcAccel(angle.f, weight.f, fric_coeff.f, mass.f)
    ;Calculate the normal force being exerted by the object
    normal.f = weight * Cos(angle * PI / 180)
    ; Calculate the force perpendicular To the normal
    perpForce.f = weight * Sin(angle * PI / 180)
    ;Calculate the amount of Static friction keeping the object at rest
    kin_friction.f = fric_coeff * normal
    ;Calculate the sum of forces acting upon the object
    total_force.f = perpForce - kin_friction
    ;Return the acceleration of the object
    ProcedureReturn total_force / mass
EndProcedure
  
  
;//////////////////////////////////////////////////////////////////
;
; KINETIC ENERGY
;
;//////////////////////////////////////////////////////////////////
;Here is a procedure that returns the amount of work done given a force, 
;a friction force, And a displacement
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

    ProcedureReturn temp
EndProcedure

;Calculating Work with Angled Force
;Procedure that will return the amount of work done considering an angled force
;


Procedure.f calculateAngledWork(*obj.vector2D, friction.f, displacement.f)

    ;don't forget to convert to rads....
    temp.f = Cos(Degree2Rad(*obj\y))   ;Degree2Rad(degr.f) see procedure further up

    ;calculate the horizontal force;
    horizForce.f = *obj\x * temp

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

    ProcedureReturn work
EndProcedure
  

Procedure.f calculateKineticEnergy(mass.f, speed.f)
  
  KE.f = (mass/2)*(Pow(speed,2))
  ProcedureReturn KE;

EndProcedure

Procedure.f calculateWorkEnergy(force.f, mass.f, displacement.f, velocityInitial.f, friction.f)
        ;NOTE: I'M NOT SURE IF THIS IS CORRECT......
       work.f = calculateWork(force,friction,displacement); See procedure above

       vFinal.f = work/((mass/2) - calculateKineticEnergy(mass, velocityInitial)); See procedure above

       velocityFinal.f = Sqr(vFinal)

       ProcedureReturn velocityFinal
EndProcedure

;calculation For potential energy  
#GRAVITY = 9.81 ;<--- 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

;///////////////////////////////////////////////////////////////////////
; Everything else...
;///////////////////////////////////////////////////////////////////////
;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

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
;/////////////////////////////////////////////////////////////////
; Just added
;/////////////////////////////////////////////////////////////////
;Converting from Cartesian to polar coordinates.
;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
;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
;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

Procedure.f avgAngAcceleration(angVelBegin.f, angVelEnd.f, time.f)
  Protected alpha.f
  alpha = (angVelEnd - angVelBegin)/time
  ProcedureReturn alpha
EndProcedure
;Tangential velocity
Procedure.f tangVelocity(omega.f, radius.f)
  Protected  velT.f
  velT = omega*radius
  ProcedureReturn velT
EndProcedure
;Procedure that will calculate the linear speed of a ball given the height, mass, And inertia
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
  ;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
Phew...That's it for now.
Best regards
Peter
Last edited by DK_PETER on Sun Mar 06, 2011 7:18 pm, edited 2 times in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: I dream of a dedicated Forum entry called Game Math..

Post by eesau »

Hi Peter, welcome to the forum and thanks for posting. You're right, it would be nice to have dedicated forum sections for math/physics issues but unfortunately the forum section here is pretty quiet as it is, so I don't think there will be section specifically dedicated to what you described. So we'll have to get by with this Game Programming -forum.
DK_PETER wrote:

Code: Select all

Structure vector3D
  x.f
  y.f
  x.f
EndStructure
Shouldn't the above be x.f, y.f and z.f?
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: I dream of a dedicated Forum entry called Game Math..

Post by DK_PETER »

Hey eesau

Thanks for pointing the error out (Must have had a cross-eyed moment) . Fixed it :)
Well, I've been a member for awhile. Just couldn't access my account due to board problems :(
Normally my Nick was: Peter_DK .

Anyway..Still I'm glad, that you liked the idea :)

Best regards
Peter
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
eesau
Enthusiast
Enthusiast
Posts: 589
Joined: Fri Apr 27, 2007 12:38 pm
Location: Finland

Re: I dream of a dedicated Forum entry called Game Math..

Post by eesau »

I think we all have our cross-eyed moments every once in a while :wink:

What I would really like is more traffic in the Game Programming forum. I know PureBasic is probably more used for applications but there are still games programmed with it. I guess some kind of a game coding event would be in order to attract more attention to PureBasic game programming!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: I dream of a dedicated Forum entry called Game Math..

Post by netmaestro »

Hi Peter, yes I think that would be a good idea. Or maybe a sticky thread at the top of the game programming forum.

Btw:

Code: Select all

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

;Radians to degree
Procedure.f Rad2degree(Rad.f)
  myRad.f = (180 * Rad) / #PI
  ProcedureReturn myRad
EndProcedure
These functions are native now with Radian() and Degree() :wink:
BERESHEIT
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: I dream of a dedicated Forum entry called Game Math..

Post by DK_PETER »

@eesau

Code: Select all

What I would really like is more traffic in the Game Programming forum. I know PureBasic is probably more used for applications but there are still games programmed with it. I guess some kind of a game coding event would be in order to attract more attention to PureBasic game programming!
Yeah, that would be really nice. I've been using PB for normal apps only and just recently, I made my first attempt with games.
I got two kids, both 13 years of age and I wanted to show them the fun part of PB.
I was amazed...The speed and easiness of creation and execution was astounding..Game making was suddently fun and has drawn me in, completely.
It's a fact, that PB can do everything and it does it well. :)
Lol!! Listen to me..A 45 year old fart, feeling like 20 again :D

THANK YOU Purebasic team for making programming FUN!

Still..No game is complete without the understanding of the simetimes obscure language called math..
I still hope, that we can get this idea up and running.
Your idea about some kind of game coding event sounds good.

@netmaestro
Thanks for the info buddy.
Radian() and Degree() - That surely simplifies things.
I'm glad you liked the idea too. :)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2056
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: I dream of a dedicated Forum entry called Game Math..

Post by Andre »

netmaestro wrote: Or maybe a sticky thread at the top of the game programming forum.
This shouldn't be a big problem... 8)
(because like it was said before, there aren't so much postings, that a completely extra sub-forum would make much sense...)

Just tell me, if I should set this thread "sticky", or if you want to create a new one. :)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
IdeasVacuum
Always Here
Always Here
Posts: 6425
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: I dream of a dedicated Forum entry called Game Math..

Post by IdeasVacuum »

I understand the arguments against, but maths is used in more than just games, so a dedicated forum would not be so out of place in my view.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: I dream of a dedicated Forum entry called Game Math..

Post by DK_PETER »

Code: Select all

I understand the arguments against, but maths is used in more than just games, so a dedicated forum would not be so out of place in my view.
@IdeasVacuum

This is precisely the eather of things. I too can understand the arguments against it. But my hope is, that it will become an goldmine of
snippits , explanations and guides. I will certainly do my part in adding to it.
We need to show, that PB can do everything extremely well. It's a powerfull language and it should be used to do games too.

@Andre:
Thanks for the offer. Might we wait a couple of days before deciding? I would like other people to give their 2 cents and to let us know, if they too would add to the "PB Game Math" forum.
If this is not something that attracts peoples interest, then a sticky with a new title would be nice.
Will let you know soon.
Thanks :)

Anyway: Here's a little something I flicked together:

Code: Select all

;Simple Breakout game example by DK_PETER
;A messy code and primitive, but it was made real quick :)
;Took some time with the levels though..
;Have fun.
;Bricks has random number of hits ( could even be 3 hits on level 1)
;Values i.e Scores are also produced randomly and changes  each time
;a new level is loaded.
;The bat changes ball speed vector with some randomness.
;Thought this would make it more interesting.
;Start game by pressing "SPACE"
;End game as usual with the key "ESCAPE"
;--------------2011--------------
;Hope people will join the 'game math' idea and be active.
;Making games with PureBasic CAN be extremely FUN :)
;-----------------------------------------

Declare Create_Construct()
Declare CreateLevelFromData(Index.i = 1)
Declare DisplayBricks()
Declare MoveBall()
Declare MoveBat()
Declare CheckbatCollision()
Declare CheckBrickCollision()
Declare DisplayBall()
Declare Next_Level()
Declare DisplayScore()
Declare ResetGame()

#THEWIDTH = 1024
#THEHEIGHT = 768

Structure TheBricks
  ID.i      ;Sprite ID
  X.i     
  Y.i
  Hit.i     ;Brick has been hit (0 = has not been hit : 1 = has been hit
  NumHits.i ;Number of hits before Hit is 1
  Value.i   ;Score value
EndStructure

Structure TheObject ;Bat, ball
  ID.i
  X.i
  Y.i
  AngX.i
  AngY.i
  Up.i
  Left.i
EndStructure

Global NewList Brick.TheBricks()      ;Bricks displayed on screen
Global NewList Construct.TheBricks()  ;Serves as base for bricks
Global NewList BrickData.i()
Global ball.TheObject,bat.TheObject
Global win.i,scr.i, Life.i = 5, Playing.i = 0, Score.i = 0, BrickCount.i = 0,Lvl.i = 1

InitSprite()
InitMouse()
InitKeyboard()

win = OpenWindow(#PB_Any,0,0,#THEWIDTH,#THEHEIGHT,"Kracked up breakout")
If win
  scr = OpenWindowedScreen(WindowID(win),0,0,#THEWIDTH,#THEHEIGHT,0,0,0,0)
else
  End
EndIf

SetFrameRate(60)
Create_Construct()
CreateLevelFromData(Lvl)

Repeat
  ClearScreen(0)
  ExamineKeyboard()
  DisplayBricks()  
  ExamineMouse()
  DisplayScore()
  
  If Playing = 0
    bat\X = MouseX()
    ball\AngX = 0
    ball\AngY = 0
    ball\X = bat\X + 25
    ball\Y = bat\Y - 21 
    If KeyboardReleased(#PB_Key_Space)
      ball\AngX = Random(3)+1
      ball\AngY = 5 + Random(5)
      playing = 1
    EndIf
  EndIf
  MoveBat()  
  MoveBall()
  CheckbatCollision()
  CheckBrickCollision()
  
  DisplaySprite(bat\ID,bat\X,bat\Y)
  DisplaySprite(ball\ID,ball\X,ball\Y)
  If Life = 0
    ResetGame()
  EndIf
  FlipBuffers()
Until KeyboardReleased(#PB_Key_Escape) = 1
End

Procedure DisplayBricks()
  For x = 0 To ListSize(Brick())-1
    SelectElement(Brick(),x)
    If Brick()\Hit > -1
      DisplaySprite(Brick()\ID,Brick()\X,Brick()\Y)
    EndIf
  Next x
EndProcedure

Procedure DisplayScore()
  StartDrawing(ScreenOutput())
  DrawText(50,50,"SCORE " + Str(Score) + " Lifes " + Str(Life))
  StopDrawing()
EndProcedure

Procedure ResetGame()
  Playing = 0
  Score = 0
  Life = 5
EndProcedure

Procedure Next_Level()
    Playing = 0
    Lvl + 1
    If lvl > 11
      lvl = 1
    EndIf
    CreateLevelFromData(Lvl)
EndProcedure
  
Procedure MoveBat()
  Protected xmouse
  bat\X = MouseX()
  If bat\X + 80 > #THEWIDTH
    bat\X = #THEWIDTH - 80
  EndIf
  DisplaySprite(bat\ID,bat\X,bat\Y)  
EndProcedure

Procedure MoveBall()
  If ball\X <= 0
    ball\Left = 0
  EndIf
  If ball\Y + 18 >= #THEHEIGHT  ;Iiiit's out!
    life - 1
    Playing = 0
    ProcedureReturn 
  EndIf
  If ball\Y < 100  ;Not above first row of bricks
    ball\Up = 0
  EndIf
  If ball\X + SpriteWidth(ball\ID) >= #THEWIDTH
    ball\Left = 1
  EndIf
  If ball\Left = 1
    ball\X - ball\AngX
  Else
    ball\X + ball\AngX
  EndIf
  If ball\Up = 1
    ball\Y - ball\AngY
  Else
    ball\Y + ball\AngY
  EndIf
  DisplaySprite(ball\ID,ball\X,ball\Y)
EndProcedure

Procedure CheckBrickCollision()
  Protected col.i
  For x = 0 To ListSize(Brick())-1
    SelectElement(Brick(),x)
    If Brick()\Hit > -1
      col = SpriteCollision(ball\ID,ball\X,ball\Y,Brick()\ID,Brick()\X,Brick()\Y)
      If col > 0
        Brick()\NumHits - 1
        Score + Brick()\Value 
        If Brick()\NumHits < 0
          Brick()\Hit = -1
          BrickCount + 1
          If BrickCount>223  ;Total number of bricks. No more bricks
            count = 0
            Next_Level()
          EndIf
        EndIf
          If ball\Up = 1
            ball\Up = 0
          Else
            ball\Up = 1
          EndIf
          If ball\X+SpriteWidth(ball\ID)<= Brick()\X + 2
            ball\Left = 1  ;It must have hit the left side of the brick
          ElseIf ball\x >= (Brick()\X + SpriteWidth(Brick()\ID)) -1
            ball\Left = 0
          ElseIf (ball\Y+20) <= (Brick()\y - 3) ;Must have hit top of brick
            ball\Up = 1
          ElseIf ball\Y >= (Brick()\y+17) ;must have hit bottom of brick
            ball\Up = 0
          EndIf
        Break
      EndIf
    EndIf
  Next x
EndProcedure

;Really simple bat/ball collision check
Procedure CheckbatCollision()
  Protected  col.i
  col = SpriteCollision(bat\ID,bat\X,bat\Y,ball\ID,ball\X,ball\Y)
  If col > 0
    ball\Up = 1
    If ball\x < = bat\x + 10  ;just making some simple ball changes based on hit area
      ball\Left = 1
      ball\AngX = 5 + Random(5)
      ball\AngY = 5 + Random(5)
    ElseIf ball\X > bat\x + 10 And ball\X <= bat\X + 39
      ball\Left = 1
      ball\AngX = Random(4) + 1
      ball\AngY = 3 + Random(3)
    ElseIf ball\X >= bat\X + 41 And ball\X < bat\X + 60
      ball\Left = 0
      ball\AngX = Random(4) + 1
      ball\AngY = 3 + Random(3)
    ElseIf ball\X >= bat\X + 60
      ball\Left = 0
      ball\AngX = 5 + Random(5)
      ball\AngY = 5 + Random(5)
    EndIf
  EndIf
EndProcedure

Procedure Create_Construct()
  ClearList(Construct())   ;Holds the created base bricks
  AddElement(Construct())
  Construct()\ID = -1      ;Blank tile/Empty area
  For x = 1 To 20  ;19 base bricks with different properties
    AddElement(Construct())
    Construct()\ID = CreateSprite(#PB_Any,60,20)
    col = 100 + Random(155):col2 = 100 + Random(155):col3 = 100 + Random(155)
    StartDrawing(SpriteOutput(Construct()\ID))
    Box(0,0,60,20,RGB(col,col2,col3))
    Line(0,0,60,0,RGB(255,255,255))
    Line(0,0,0,20,RGB(255,255,255))
    Line(0,20,60,20,RGB(50,50,50))
    Line(60,0,60,20,RGB(50,50,50))
    StopDrawing()
    Construct()\Hit = 0
    Construct()\NumHits = Random(3)
    Construct()\Value = Random(100)+50
  Next x
  ;Make the ball and bat
  ball\ID = CreateSprite(#PB_Any,20,20)
  StartDrawing(SpriteOutput(ball\ID))
  Circle(10,10,10,RGB(100,100,100))
  StopDrawing()
  
  bat\ID = CreateSprite(#PB_Any,80,15)
  StartDrawing(SpriteOutput(bat\ID))
  Box(0,0,80,15,RGB(70,70,70))
  StopDrawing()
  ;Bat and ball properties
  bat\X = (#THEWIDTH / 2) -(SpriteWidth(bat\ID)/2)
  bat\Y = #THEHEIGHT - SpriteHeight(bat\ID)
  ball\AngY = 0
  ball\AngY = 0
  ball\Up = 1
  ball\X = Bat\X + 25
  ball\Y = bat\y + 21
EndProcedure

Procedure CreateLevelFromData(Index.i = 1)
  Protected count.i = 0
  ClearList(Brick())
  Select Index
    Case 1
      Restore level1
    Case 2
      Restore level2
    Case 3
      Restore level3
    Case 4
      Restore level4
    Case 5
      Restore level5
    Case 6
      Restore level6
    Case 7
      Restore level7
    Case 8
      Restore level8
    Case 9
      Restore level9
    Case 10
      Restore level10
    Case 11
      Restore level11
  EndSelect
  ClearList(BrickData())
  For x = 0 To 223  ;14 *15
    Read.b brk
    AddElement(BrickData())
    BrickData() = brk
  Next x
  For y = 0 To 13
    For x = 0 To 15
      SelectElement(BrickData(),count)
      AddElement(Brick())
      If BrickData() = -1
        SelectElement(Construct(),0)
        Brick()\Hit = -1
      Else
        SelectElement(Construct(),BrickData()+1)
        Brick()\ID = CopySprite(Construct()\ID,#PB_Any)
        Brick()\Hit = Construct()\Hit
        Brick()\NumHits = Construct()\NumHits
        Brick()\Value = Construct()\Value
        Brick()\X = x * 62
        Brick()\Y = 100 + (y *23)
      EndIf
      count + 1
    Next x    
  Next y
EndProcedure


DataSection
level1:  
Data.b -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,-1,-1,0,-1,0,-1,0,0,-1,-1,0,0,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,-1,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,0,-1,-1,-1,0,0,-1,-1,0,-1,0,-1,0,0,-1,-1,0,0,-1,-1,0,-1,-1,-1,0,-1,0,-1,0,-1,0,-1,0,-1,-1,-1,0,-1,-1,-1,0,-1,0,-1,0,-1,0,-1,0,-1,-1,-1,0,-1,-1,-1,-1,0,-1,-1,0,-1,0,-1,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2,2,-1,-1,-1,2,-1,-1,2,2,-1,2,-1,2,2,-1,2,-1,2,-1,2,-1,2,-1,2,-1,-1,2,-1,2,-1,-1,2,2,-1,-1,2,-1,2,-1,2,2,-1,2,-1,2,-1,-1,2,-1,2,-1,2,2,2,-1,-1,2,-1,2,-1,2,-1,-1,2,2,-1,-1,2,-1,2,-1,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4,0,4,0,0,0,0,4,0,0,4,0,0,0,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,21,21,21,0,0,0,0,0,0,0,0,0,0,0,0,0,21,21,21,0,0,0,0,0,0,0,0,0,1,1,1,21,1,6,1,21,1,1,1,1,1,1,1,1,1,1,1,1,1,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,6,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,1,1,1,1,1,1,1,1,1,1,1,3,3,3,3,3,3,1,1,1,1,1,1,1
level10:
Data.b 5,19,19,19,19,19,19,5,19,19,19,19,19,19,19,19,19,5,19,19,19,19,5,19,19,19,19,19,19,19,17,19,19,19,5,19,19,5,19,19,19,19,17,17,19,19,17,19,19,19,19,5,5,19,19,19,19,19,17,17,19,19,19,17,19,19,19,5,5,19,19,19,19,19,19,17,19,19,19,19,19,19,5,19,19,5,19,19,19,19,17,17,19,19,19,19,19,5,19,19,19,19,5,19,19,19,17,19,19,19,19,19,5,19,19,19,19,19,19,5,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,19,4,19,19,19,19,19,19,19,4,19,19,19,19,19,19,0,0,0,19,19,19,19,19,0,0,0,19,19,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
level11:
Data.b -1,-1,19,-1,19,-1,-1,-1,19,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,-1,19,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,19,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,19,-1,19,19,-1,-1,-1,19,19,19,-1,19,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,19,-1,19,-1,-1,-1,-1,5,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,19,-1,-1,19,5,5,5,5,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,18,6,6,6,6,18,18,18,18,18,18,18,18,18,18,18,18,18,6,6,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18,18
EndDataSection
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: I dream of a dedicated Forum entry called Game Math..

Post by idle »

Yes a math topic in general would be of great benefit in my opinion, though not just specifically for games.

PS and thanks for the breakout, you're a quick study!
(it's a bit slugish on linux but I haven't got time to look now, will see what up in the morning)
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: I dream of a dedicated Forum entry called Game Math..

Post by DK_PETER »

Hey idle.

Code: Select all

Yes a math topic in general would be of great benefit in my opinion, though not just specifically for games.
I could live with that, if a game math section is completely out of the question. At least that would make a search
concerning math much more localized.
PS and thanks for the breakout, you're a quick study!
(it's a bit slugish on linux but I haven't got time to look now, will see what up in the morning)
You're welcome and thanks :)
About the slugginess: let me know what you find :)
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
User avatar
DK_PETER
Addict
Addict
Posts: 898
Joined: Sat Feb 19, 2011 10:06 am
Location: Denmark
Contact:

Re: I dream of a dedicated Forum entry called Game Math..

Post by DK_PETER »

Hi again fellow comrades :)

Doing some cleanup...Removed this entry..

Best regards
Peter
Last edited by DK_PETER on Tue Aug 13, 2013 9:40 am, edited 2 times in total.
Current configurations:
Ubuntu 20.04/64 bit - Window 10 64 bit
Intel 6800K, GeForce Gtx 1060, 32 gb ram.
Amd Ryzen 9 5950X, GeForce 3070, 128 gb ram.
marc_1024
User
User
Posts: 10
Joined: Thu Feb 17, 2011 3:24 pm

Re: I dream of a dedicated Forum entry called Game Math..

Post by marc_1024 »

Hi DK_PETER,

Great idea, i love it...
Can we also subdivide it in
- Physics
- Statics
- Dynamics
- Kinematics
- ...

I don't know i made the good translations ?

Marc
Post Reply