Has anyone design some good algorithms 4theirgames

10-24-2001, 04:27 AM
I would like some algorithms that people have designed for games etc

10-24-2001, 04:43 AM
A please would be nice! What are you trying to do? Have you put any thought into it yourself? images/icons/mad.gif

10-24-2001, 06:41 AM
You cant just ask for 'alogirthms' --> Well, if you insist, I can put together a bubble sort algorithm, but I'll bet thats not what you're after...... you need to be more specific about what kinds of algorithms.....

And yes, a please would be nice images/icons/wink.gif

10-24-2001, 04:36 PM
I think he just wants to see some cool code. I dont know about the rest of you, but i go weak at the knees at the thought of a three page algorithm that makes my coffee in the mornings..... images/icons/wink.gif (and yes - i have got one.)


10-24-2001, 04:45 PM
Well, okay then........ This is the core code from my rather slow (~55fps) DirectX software 3D engine...... useless to you without all the other stuff, but its a cool looking algorithm:

<pre>Public Sub RenderTiles(iGridsize As Integer, Optional bMoved As Boolean = True)
Dim iMeshSize As Integer
Dim VisTiles() As udtGameTile
Dim Count As Integer
Dim p As Integer, q As Integer, r As Integer
Dim NodeBuffer(0 To 3) As D3DVECTOR
Dim ScreenBuffer(0 To 3) As D3DVECTOR
Dim CentreX As Integer, CentreY As Integer
Dim col As Long

Static TilePolys() As D3DTLVERTEX

If bMoved Then 'We need to re-calculate the vertexes
ReDim TilePolys(1 To 3750)
Count = 1
CentreX = Settings.Width / 2
CentreY = Settings.Height / 2
For p = 1 To iGridsize
For q = 1 To iGridsize
For r = 0 To 3
'// Fill up the Node Buffers
NodeBuffer(r).X = GridMesh(Tile(p, q).RefPt(r)).X
NodeBuffer(r).Y = GridMesh(Tile(p, q).RefPt(r)).ht
NodeBuffer(r).z = GridMesh(Tile(p, q).RefPt(r)).Y
'// Calculate messy business
ScreenBuffer(r).X = CentreX + 512 * (NodeBuffer(r).X / NodeBuffer(r).z)
ScreenBuffer(r).Y = CentreY + 512 * (NodeBuffer(r).Y / NodeBuffer(r).z)
ScreenBuffer(r).z = 1 - (10 / NodeBuffer(r).z)
Next r

col = -1

'// Make 6 vertexes from 4 buffers (magic!)
DX.CreateD3DTLVertex ScreenBuffer(0).X, ScreenBuffer(0).Y, ScreenBuffer(0).z, 1, col, 1, 0, 0, TilePolys(Count)
DX.CreateD3DTLVertex ScreenBuffer(1).X, ScreenBuffer(1).Y, ScreenBuffer(1).z, 1, col, 1, 1, 0, TilePolys(Count + 1)
DX.CreateD3DTLVertex ScreenBuffer(2).X, ScreenBuffer(2).Y, ScreenBuffer(2).z, 1, col, 1, 1, 1, TilePolys(Count + 2)
DX.CreateD3DTLVertex ScreenBuffer(3).X, ScreenBuffer(3).Y, ScreenBuffer(3).z, 1, col, 1, 0, 1, TilePolys(Count + 5)
TilePolys(Count + 3) = TilePolys(Count)
TilePolys(Count + 4) = TilePolys(Count + 2)

Count = Count + 6
Next q
Next p
'// PHEW what a long process!
End If

Device.SetTexture 0, TextureSet(1)
Device.DrawPrimitive D3DPT_TRIANGLELIST, D3DFVF_TLVERTEX, TilePolys(1), UBound(TilePolys), D3DDP_DEFAULT

End Sub</pre>


<font color=purple>* Squirm shrugs</font color=purple>

10-24-2001, 04:52 PM
Cool....what's with the C++ style comments?

10-24-2001, 04:55 PM
Javascript style more like.....images/icons/wink.gif


10-24-2001, 04:57 PM
or maybe it's java.....Who knows? With all these languages copying each other, ya never know anymore....images/icons/smile.gif

10-24-2001, 04:58 PM
C++ ?? I picked it up from JavaScript

10-24-2001, 04:59 PM
(It's the same in C++, java, javascript, and I think maybe some other languages)

I first saw that in C++, but yeah, I guess javascript has those too...

10-24-2001, 05:00 PM
Chief was right. images/icons/smile.gif Java and C++ use the same comment style or should I say Java copied C++'s style.

10-24-2001, 05:03 PM
Heres my fave. One of my new image manip ones...
<pre><font color=blue>Private Sub</font color=blue> cmdContrast_Click()
<font color=blue>Dim</font color=blue> i <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> j <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> col <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> nCol <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> tol <font color=blue>As Integer</font color=blue>
<font color=blue>Dim</font color=blue> per <font color=blue>As Integer</font color=blue>

Screen.MousePointer = vbHourglass

<font color=green>' get the contrast and tolerance levels</font color=green>
tol = <font color=blue>CInt</font color=blue>(txtTolerance.Text)
<font color=blue>If</font color=blue> tol &lt; 0 <font color=blue>Or</font color=blue> tol &gt; 256 <font color=blue>Then</font color=blue>
MsgBox "Tolerance level it outside of boundaries", vbOKOnly + vbCritical, "ImgBuffer"
<font color=blue>Exit Sub</font color=blue>
<font color=blue>End If</font color=blue>

per = lstPercent.Text

<font color=green>' alter the frmmain image</font color=green>
<font color=blue>With</font color=blue> frmMain
<font color=green>' make a copy</font color=green>
h = .picMain.Height
w = .picMain.width
.picHidden.Height = h
.picHidden.width = w
BitBlt .picHidden.hDC, 0, 0, w, h, .picMain.hDC, 0, 0, vbSrcCopy

<font color=green>' altering routine</font color=green>
<font color=blue>For</font color=blue> i = 0 <font color=blue>To</font color=blue> .picMain.width
<font color=blue>For</font color=blue> j = 0 <font color=blue>To</font color=blue> .picMain.Height
col = GetPixel(.picHidden.hDC, i, j)
nCol = doContrast(col, tol, per)
SetPixel .picMain.hDC, i, j, nCol
<font color=blue>Next</font color=blue> j
<font color=blue>Next</font color=blue> i
<font color=blue>End With</font color=blue>

Screen.MousePointer = vbNormal
Unload Me
<font color=blue>End Sub</font color=blue>

<font color=blue>Public Function</font color=blue> doContrast(col <font color=blue>As Long</font color=blue>, tol <font color=blue>As Integer</font color=blue>, con <font color=blue>As Integer</font color=blue>) <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> r <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> g <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> b <font color=blue>As Long</font color=blue>
<font color=blue>Dim</font color=blue> L <font color=blue>As Long</font color=blue>

<font color=green>' convert to rgb</font color=green>
r = col <font color=blue>And</font color=blue> 255
L = col \ 256
g = L <font color=blue>And</font color=blue> 255
b = L \ 256

<font color=green>' raise or lower</font color=green>
<font color=blue>If CInt</font color=blue>((r + g + b) / 3) &lt; tol <font color=blue>Then</font color=blue>
r = (r / 100) * (100 - con)
g = (g / 100) * (100 - con)
b = (b / 100) * (100 - con)
r = (r / 100) * (100 + con)
g = (g / 100) * (100 + con)
b = (b / 100) * (100 + con)
<font color=blue>End If</font color=blue>

doContrast = RGB(r, g, b)
<font color=blue>End Function</font color=blue>


10-24-2001, 05:03 PM
You got it all wrong. It originated in C++ (Bjarne Stroustrup), then java (Sun Microsystems) stole it. Then, javascript (Netscape) stole it from java.

10-24-2001, 05:07 PM
........and then Squirm stole it from JavaScript........

10-24-2001, 05:08 PM
Here's some REAL basic, super simple rectangular collision detection....(Beware, I wrote this a while ago, it probably could be better.....)

Basically, it was for a game with a falling car. If the falling car hit the left side of your car, it bounces of to the left, and vice versa.....

<pre><font color=blue>Public</font color=blue> <font color=blue>Sub</font color=blue> Detect()
<font color=green>'Check to see if the cars are overlapping for collision detection
</font color=green>If car.Left &lt; fall1.Left + fall1.Width <font color=blue>And</font color=blue> car.Left + car.Width &gt; fall1.Left <font color=blue>And _
</font color=blue> car.Top &lt; fall1.Top + fall1.Height <font color=blue>And</font color=blue> car.Top + car.Height &gt; fall1.Top Then
<font color=green>'fall left or right
</font color=green> <font color=blue>If</font color=blue> car.Left + (car.Width / 2) &gt; fall1.Left + (fall1.Width / 2) Then
fall1.Left = fall1.Left - 200
fall1.Left = fall1.Left + 200
<font color=blue>End</font color=blue> If
<font color=blue>End</font color=blue> If
<font color=blue>End</font color=blue> Sub</pre>

10-24-2001, 05:08 PM
Arrgghhh! The orange men invade (this thread is getting SPAMtastic)

10-24-2001, 05:12 PM
#include &lt;iostream.h&gt;
void main()
cout &lt;&lt; "Hey, it's got some useful information in it!"
// By the way, I like the use of SPAM there, it's quite unique.
// And don't forget, C++ RULES!


wild wolf
10-24-2001, 10:41 PM
C++ or Java does not matter coz they are both Object Oriented Programming languages, right or wrong? :) so if they are both OOP languages, its not a surprise that they are similar(hmm i dint think of that logic before hehehe)

wild wolf
10-24-2001, 10:45 PM
hey by the way orufet, the falling car program looks interesting, i could use it for my game, could u by any chance have a copy of the falling car and drop it in my mail box please, thanx :) email is asrar@joymail.com

10-24-2001, 11:07 PM
Really, the program isn't that great...But if you're interested, I dumped an older version at PSC a while ago....I'll try to upload a newer version that uses the better collision detection method. I never really finished it, and there's not too much to it; it's just the same thing repeating over and over again. But have a look :)

By the way, here's a snapshot:
Racing Game (http://www.planet-source-code.com/upload/ScreenShots/PIC2001610157108926.jpg)

At design time, if you make the bus Visisble, you'll notice that I never finished it, as the bus is really messed up!

wild wolf
10-25-2001, 01:29 AM
hey that was great, i have implemented it in my game hehehe hope u dun mind, ill acknowledge it in my documentation :)

10-25-2001, 08:37 AM
Hey, I'm glad you like it. Acknowledge me if you like, but you certainly don't have to. Thanks for the nice comments

11-01-2001, 02:34 AM
Heres my great random lander generator!
Just add the code and make a command1 button on the form
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Type XY
B As Integer
M As Integer
X As Integer
Y As Integer
X1 As Integer
X2 As Integer
Y1 As Integer
Y2 As Integer
End Type

Dim i, g
Dim Freeze As Boolean
Dim Lines(794) As XY
Dim LandRndX, LandRndY
Dim Points(40) As XY
Dim LandPad As XY
Dim WallLandSpot
Dim FlatsNumber
Dim LandGenerations
Dim LandPadSize
Dim theAngle

Private Sub Form_Load()
Me.ScaleMode = 3
Me.WindowState = 2
Me.BackColor = 0
End Sub

Private Sub command1_Click()
'Clear screen
'Set Values
LandPadSize = 0
WallLandSpot = 0
FlatsNumber = 0
LandRndX = 20
LandRndY = 150
'Generate Array of XY Points
For i = 1 To 28
Points(i).X = Int(Rnd * LandRndX) + Points(i - 1).X + 18
Points(i).Y = Int(Rnd * LandRndY) + 340
'Define Starting and Ending points of Previous Array
Points(1).X = 0
Points(1).Y = Int(Rnd * 150) + 500
Points(29).X = 794
Points(29).Y = Int(Rnd * 100) + 500
Next i

For i = 1 To 28
'Show amount of landing spots in Flats
If Points(i).Y = Points(i + 1).Y Then FlatsNumber = FlatsNumber + 1
Next i

For i = 1 To 28
'Find a if there is a flat spot
If Points(i).Y = Points(i + 1).Y Then
'Add Position of Landing Spot an Array
LandPad.X1 = Points(i).X
LandPad.X2 = Points(i + 1).X
LandPad.Y = Points(i).Y
End If
Next i

'Make land pad size in pixels larger then 20
If (LandPad.X2 - LandPad.X1) &gt; 20 Then LandPadSize = 1

'Counter telling how many generations have gone by
LandGenerations = LandGenerations + 1
'Make a landing spot not generate next to wall
If LandPad.X1 = Points(1).X Then WallLandSpot = WallLandSpot + 1
If LandPad.X2 = Points(29).X Then WallLandSpot = WallLandSpot + 1

'Loop until there is only one landing spot
Loop Until FlatsNumber = 1 And WallLandSpot = 0 And LandPadSize = 1

For i = 1 To 28
For g = 0 To 250
'Draw up points of land
Line (Points(i).X, Points(i).Y + g)-(Points(i + 1).X, Points(i + 1).Y + g), RGB(255 - g, 200 + i, 200 - i)
Line (Points(i).X, Points(i).Y)-(Points(i + 1).X, Points(i + 1).Y)
Next g
Next i

End Sub

11-01-2001, 07:44 AM
Looks cool, Andrewo! I like what you did with the colors.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum