Post by Centanoul on May 19, 2006 3:03:08 GMT -5
What are you talking about?
Why of course, Medium concepts. These are the concepts that are asked about alot, are not too hard to accomplish, and are just generally alot more useful to show someone than watching them waste their time trying to figure it out.
What concepts will we be dealing with?
One of the core ones will be collision detection. That will have to be heavily focused on since alot of people have trouble there. A few other concepts will be: Movement, and Buffered Output.
How will we do all this?!
Very slowly. Did you learn to do division the first time you did math? Probably not. More likely, you started with addition, then subtraction, multiplication, and finally after all that work, division.
I intend to start a program with Movement, add Collision Detection, and finally Buffered Output
Movement
Movement...Displacement, its all the same thing. One object is at one position one moment, and another the next. To do this requires some positioning system on the thing you are moving. Such as an X,Y coordinate. (Z if 3d, but I am not doing 3d today).
Here is a small example of a movement system:
Well...it moves! But what about the rest?
Collision Detection
What is collision detection?
Collision detection is the ability for us to be aware of when 2 objects collide, such as a wall and person *grins*. So we need to add some sort of object for the X to collide with...I know...a map!
Map:
There you have it...nice little map there. But it is soooooooo slow!
Well lets fix that then, Its time to delve into buffered output.
Buffered Output
What is it? Why will it help?
During our program, we made multiple calls to the graphics card. One for each character we printed. Buffered Output is the process of doing something to what you are printing on the screen. In our case, we are going to bunch it all up to only make 1 call to the graphics card, which will speed this program up.
Here it is:
It should run decently fast now. The Sleep() is there to prevent you from moving at blinding speeds, so that you have some limit to how fast you move.
Now...back to that whole collision detection:
Collision Detection...Continued
Well great...now we have everything we need to add the collision detection. But how?
Quite simply, we have to stop Px and Py from changing when it would place them on a @. So we just insert a few conditions into the movement code that basically say "You must be able to move into a space not containing @ in order to be allowed to move."
The code with this would look like this:
And there you have it folks. A quick, painless(heh, or not), lesson in a few medium concepts.
And as a teaser for what I am working on, here is the source for the program I am making. It includes collision detection using opengl.
The tiles can be found in: www.psystria.com/Files/Basic4GL/Custom.zip
I am going to be adding a Basic Concepts, section later. Sort of a between Introduction and Medium Concepts. I don't think I can really do an advanced concepts section. Not without major help from quite a few people. (GuppyB and Wybiral come to mind, but any help would be welcome). As well as help on any of these. I am by all means only 1 guy, and I left out alot...am just sorta going to be adding as I see a need for a particular concept, and fit it into one of these posts.
Until next time,
Cent
Why of course, Medium concepts. These are the concepts that are asked about alot, are not too hard to accomplish, and are just generally alot more useful to show someone than watching them waste their time trying to figure it out.
What concepts will we be dealing with?
One of the core ones will be collision detection. That will have to be heavily focused on since alot of people have trouble there. A few other concepts will be: Movement, and Buffered Output.
How will we do all this?!
Very slowly. Did you learn to do division the first time you did math? Probably not. More likely, you started with addition, then subtraction, multiplication, and finally after all that work, division.
I intend to start a program with Movement, add Collision Detection, and finally Buffered Output
Movement
Movement...Displacement, its all the same thing. One object is at one position one moment, and another the next. To do this requires some positioning system on the thing you are moving. Such as an X,Y coordinate. (Z if 3d, but I am not doing 3d today).
Here is a small example of a movement system:
Dim Px, Py
Px=3
Py=3
Gosub _Draw
_Loop:
While True
If scankeydown(VK_Right) then Px = Px+1:Gosub _Draw:Endif
If scankeydown(VK_Left) then Px = Px-1:Gosub _Draw:Endif
If scankeydown(VK_Up) then Py = Py-1:Gosub _Draw:Endif
If scankeydown(VK_Down) then Py = Py+1:Gosub _Draw:Endif
Wend
_Draw:
Cls
Locate Px,Py: Print "X"
Sleep(150)
Return
Well...it moves! But what about the rest?
Collision Detection
What is collision detection?
Collision detection is the ability for us to be aware of when 2 objects collide, such as a wall and person *grins*. So we need to add some sort of object for the X to collide with...I know...a map!
Map:
Dim Px, Py
Dim a,b,c$
Px=3
Py=3
Dim Map$(10,10)
Data "@","@","@","@","@","@","@","@","@","@"
Data "@"," "," "," ","@"," "," "," "," ","@"
Data "@"," "," "," "," "," ","@","@","@","@"
Data "@"," "," "," ","@"," "," "," "," ","@"
Data "@","@","@","@","@","@","@","@"," ","@"
Data "@"," "," "," "," "," "," "," "," ","@"
Data "@"," ","@","@"," ","@","@","@","@","@"
Data "@"," ","@","@"," ","@","@","@","@","@"
Data "@"," "," "," "," ","@","@","@","@","@"
Data "@","@","@","@","@","@","@","@","@","@"
For a = 1 to 10
For b = 1 to 10
read c$
Map$(a,b) = c$
Next
Next
Gosub _Draw
_Loop:
While True
If scankeydown(VK_Right) then Px = Px+1:Gosub _Draw:Endif
If scankeydown(VK_Left) then Px = Px-1:Gosub _Draw:Endif
If scankeydown(VK_Up) then Py = Py-1:Gosub _Draw:Endif
If scankeydown(VK_Down) then Py = Py+1:Gosub _Draw:Endif
Wend
_Draw:
For a = 1 to 10
For b = 1 to 10
Locate b,a
Print Map$(a,b)
If a=Py and b=Px then Locate b,a: Print "X":Endif
Next
Next
Return
There you have it...nice little map there. But it is soooooooo slow!
Well lets fix that then, Its time to delve into buffered output.
Buffered Output
What is it? Why will it help?
During our program, we made multiple calls to the graphics card. One for each character we printed. Buffered Output is the process of doing something to what you are printing on the screen. In our case, we are going to bunch it all up to only make 1 call to the graphics card, which will speed this program up.
Here it is:
TextMode(Text_Buffered)
Dim Px, Py
Dim a,b,c$
Px=3
Py=3
Dim Map$(10,10)
Data "@","@","@","@","@","@","@","@","@","@"
Data "@"," "," "," ","@"," "," "," "," ","@"
Data "@"," "," "," "," "," ","@","@","@","@"
Data "@"," "," "," ","@"," "," "," "," ","@"
Data "@","@","@","@","@","@","@","@"," ","@"
Data "@"," "," "," "," "," "," "," "," ","@"
Data "@"," ","@","@"," ","@","@","@","@","@"
Data "@"," ","@","@"," ","@","@","@","@","@"
Data "@"," "," "," "," ","@","@","@","@","@"
Data "@","@","@","@","@","@","@","@","@","@"
For a = 1 to 10
For b = 1 to 10
read c$
Map$(a,b) = c$
Next
Next
Gosub _Draw
_Loop:
While True
If scankeydown(VK_Right) then Px = Px+1:Gosub _Draw:Endif
If scankeydown(VK_Left) then Px = Px-1:Gosub _Draw:Endif
If scankeydown(VK_Up) then Py = Py-1:Gosub _Draw:Endif
If scankeydown(VK_Down) then Py = Py+1:Gosub _Draw:Endif
Wend
_Draw:
For a = 1 to 10
For b = 1 to 10
Locate b,a
Print Map$(a,b)
If a=Py and b=Px then Locate b,a: Print "X":Endif
Next
Next
DrawText()
Sleep(150)
Return
It should run decently fast now. The Sleep() is there to prevent you from moving at blinding speeds, so that you have some limit to how fast you move.
Now...back to that whole collision detection:
Collision Detection...Continued
Well great...now we have everything we need to add the collision detection. But how?
Quite simply, we have to stop Px and Py from changing when it would place them on a @. So we just insert a few conditions into the movement code that basically say "You must be able to move into a space not containing @ in order to be allowed to move."
The code with this would look like this:
TextMode(Text_Buffered)
Dim Px, Py
Dim a,b,c$
Px=3
Py=3
Dim Map$(10,10)
Data "@","@","@","@","@","@","@","@","@","@"
Data "@"," "," "," ","@"," "," "," "," ","@"
Data "@"," "," "," "," "," ","@","@","@","@"
Data "@"," "," "," ","@"," "," "," "," ","@"
Data "@","@","@","@","@","@","@","@"," ","@"
Data "@"," "," "," "," "," "," "," "," ","@"
Data "@"," ","@","@"," ","@","@","@","@","@"
Data "@"," ","@","@"," ","@","@","@","@","@"
Data "@"," "," "," "," ","@","@","@","@","@"
Data "@","@","@","@","@","@","@","@","@","@"
For a = 1 to 10
For b = 1 to 10
read c$
Map$(a,b) = c$
Next
Next
Gosub _Draw
_Loop:
While True
If scankeydown(VK_Right) and Map$(Py,Px+1)<> "@" then Px = Px+1:Gosub _Draw:Endif
If scankeydown(VK_Left) and Map$(Py,Px-1)<> "@" then Px = Px-1:Gosub _Draw:Endif
If scankeydown(VK_Up) and Map$(Py-1,Px)<> "@" then Py = Py-1:Gosub _Draw:Endif
If scankeydown(VK_Down) and Map$(Py+1,Px)<> "@" then Py = Py+1:Gosub _Draw:Endif
Wend
_Draw:
For a = 1 to 10
For b = 1 to 10
Locate b,a
Print Map$(a,b)
If a=Py and b=Px then Locate b,a: Print "X":Endif
Next
Next
DrawText()
Sleep(150)
Return
And there you have it folks. A quick, painless(heh, or not), lesson in a few medium concepts.
And as a teaser for what I am working on, here is the source for the program I am making. It includes collision detection using opengl.
const maxTextures=1
const maxX=30
const maxY=22
'const mobc=2
glEnable (GL_TEXTURE_2D)
glTranslatef(-1.5,1.1,-2)
dim Map(maxX,maxY)
dim x, y, c, j, k
dim loopVarA
'dim mobt(mobc)
'dim mobx(mobc)
'dim moby(mobc)
'For j = 0 to mobc
'mobt(j) = Int(Sin(Rnd()%90))
'mobx(j) = Rnd()%15+5
'moby(j) = Rnd()%15+5
'Next
dim Tiles(ImageStripFrames ("custom\Tiles_1.png",16) - 1)
Tiles = LoadMipmapImageStrip ("custom\Tiles_1.png",16)
dim Char(0)(ImageStripFrames ("custom\Char_1.png",16) - 1)
Char(0) = LoadMipmapImageStrip ("custom\Char_1.png",16)
'dim Mob(0)(ImageStripFrames ("custom\Char_2.png",16) - 1)
'Mob(0) = LoadMipmapImageStrip ("custom\Char_2.png",16)
Dim Blocked(1), i
Blocked(0)=32
Blocked(1)=250
Data 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
Data 32,250,62,62,62,62,62,250,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,32,32,32,47,32,32,32,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,32,32,32,47,32,32,32,32,32,32,32,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,62,62,62,62,47,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,62,62,62,62,62,62,62,62,62,62,62,32,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,32
Data 32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32,32
for y = 0 to maxY-1
for x = 0 to maxX-1
read c
Map(x,y)=c
next
next
dim pX:pX=4
dim pY:pY=4
dim ppX,ppY
dim frame
MainLoop:
While True
gosub DrawScreen
gosub DrawChar
Waittimer(150)
ppX=pX
ppY=pY
if ScanKeyDown(VK_Left) then pX=pX-1:endif
if ScanKeyDown(VK_Right) then pX=pX+1: endif
if ScanKeyDown(VK_Up) then pY=pY-1: endif
if ScanKeyDown(VK_Down) then pY=pY+1: endif
For i = 0 to 1
if Map(pX,pY)=Blocked(i) then pX=ppX:pY=ppY:endif
Next
'gosub MobAI
frame=frame+1
if frame > 3 then frame=0:endif
Wend
DrawChar:
glPushMatrix()
glTranslatef (pX*.1, -pY*.1,0)
glBindTexture (GL_TEXTURE_2D, Char(0)(frame))
glBegin (GL_QUADS)
glTexCoord2f (0, 0): glVertex2f (0,-.1)
glTexCoord2f (1, 0): glVertex2f (.1,-.1)
glTexCoord2f (1, 1): glVertex2f (.1, 0)
glTexCoord2f (0, 1): glVertex2f (0, 0)
glEnd()
glPopMatrix()
SwapBuffers()
return
DrawScreen:
for x = 0 to maxX-1
for y = 0 to maxY-1
glPushMatrix()
glTranslatef (0+x * .1, 0-y * .1, 0)
glBindTexture (GL_TEXTURE_2D, Tiles(Map(x,y)))
glBegin (GL_QUADS)
glTexCoord2f (0, 0): glVertex2f (0,-.1)
glTexCoord2f (1, 0): glVertex2f (.1,-.1)
glTexCoord2f (1, 1): glVertex2f (.1, 0)
glTexCoord2f (0, 1): glVertex2f (0, 0)
glEnd()
glPopMatrix()
next
next
'For k = 0 to mobc
'glPushMatrix()
'glTranslatef (0+mobx(k) * .1, 0-moby(k) * .1, 0)
'glBindTexture (GL_TEXTURE_2D, Mob(mobt(k),frame))
'glBegin (GL_QUADS)
'glTexCoord2f (0, 0): glVertex2f (0,-.1)
'glTexCoord2f (1, 0): glVertex2f (.1,-.1)
'glTexCoord2f (1, 1): glVertex2f (.1, 0)
'glTexCoord2f (0, 1): glVertex2f (0, 0)
'glEnd()
'glPopMatrix()
'Next
return
'MobAI:
'return
The tiles can be found in: www.psystria.com/Files/Basic4GL/Custom.zip
I am going to be adding a Basic Concepts, section later. Sort of a between Introduction and Medium Concepts. I don't think I can really do an advanced concepts section. Not without major help from quite a few people. (GuppyB and Wybiral come to mind, but any help would be welcome). As well as help on any of these. I am by all means only 1 guy, and I left out alot...am just sorta going to be adding as I see a need for a particular concept, and fit it into one of these posts.
Until next time,
Cent