2D Lighting

Blitz3D Forums/Blitz3D Beginners Area/2D Lighting

Kenshin Yurihara(Posted 2011) [#1]
Hello guys, its been quite awhile since I took the time to come on here and post, or messed with B3D for that matter. I've mostly been learning C++ and now my B3D code even has the habit of adding ; at the end of it.
Anyway, enough about what I've been up to. I'm trying to create 2D lighting in Blitz3D, I know that the Read and Write Pixel commands are slowish, but I'm sure we could find a work around, my main problem is coming up with a algorithm to get the pixels.




Any help would be appreciated.
Thanks,
Zac


Matty(Posted 2011) [#2]
What do you mean by 2D lighting?

Are we talking lighting of a 2D top down scene? Isometric?


Kenshin Yurihara(Posted 2011) [#3]
2D top down or side ways, no isometric what so ever.
Sorry for the slowish reply.


I'm currently TRYING something along these lines.


;Lighting
Function Maximum(Arg1,Arg2)

If(Arg1 > Arg2)
Return Arg1 
Else 
Return Arg2
EndIf 

End Function 

Function Minimum(Arg1,Arg2)

If(Arg1 < Arg2)
Return Arg1
Else 
Return Arg2
EndIf 

End Function 

Function Calculate(Object1X,Object1Y,Object1W,Object1H,Object2X,Object2Y,Object2W,Object2H)


X1 = Maximum(Object1X,Object2X)
Y1 = Maximum(Object1Y,Object2Y)
X2 = Minimum(Object1X + Object1W, Object2X + Object2W)
Y2 = Minimum(Object1Y + Object1H, Object2Y + Object2H)


Width = X2 - X1
Height = Y2 - Y1

If Width > 0 And Height > 0

ReturnBank = CreateBank(15)
PokeInt ReturnBank,0,X1
PokeInt ReturnBank,4,Y1
PokeInt ReturnBank,8,Width
PokeInt ReturnBank,12,Height

Return ReturnBank 

Else 

ReturnBank = CreateBank(15)

Return ReturnBank 

EndIf 

End Function 



Look at the math, I'm not sure if this is anywhere, near correct,
but its close to what I did in SDL, to get Per-Pixel Collision, using
SDL Tutorials.

I would get the X and Y of the object and the width and height.
Then I would go through a For loop, for X = 0 to Width
and For Y = 0 to Height, to get each individual pixel at X,Y

Last edited 2011

Last edited 2011


Kenshin Yurihara(Posted 2011) [#4]
Yea, that'll work for getting if the pixels are in range. I now just need to read and write per a pixel at that point. Wish me luck.

Right, so either I completely, don't understand pixel editing with Blitz3D or something about this code isn't 100% correct.

I borrowed code from Ross C, at this thread:
http://www.blitzbasic.com/Community/posts.php?topic=64678
Complete credit goes to him for that, I'm just using it for
testing. I used his Pixel editing.


;Lighting
Function Maximum(Arg1,Arg2)

If(Arg1 > Arg2)
Return Arg1 
Else 
Return Arg2
EndIf 

End Function 

Function Minimum(Arg1,Arg2)

If(Arg1 < Arg2)
Return Arg1
Else 
Return Arg2
EndIf 

End Function 

Function Calculate(Object1X,Object1Y,Object1W,Object1H,Object2X,Object2Y,Object2W,Object2H)


X1 = Maximum(Object1X,Object2X)
Y1 = Maximum(Object1Y,Object2Y)
X2 = Minimum(Object1X + Object1W, Object2X + Object2W)
Y2 = Minimum(Object1Y + Object1H, Object2Y + Object2H)


Width = X2 - X1
Height = Y2 - Y1

If Width > 0 And Height > 0

ReturnBank = CreateBank(16)
PokeInt ReturnBank,0,X1
PokeInt ReturnBank,4,Y1
PokeInt ReturnBank,8,Width
PokeInt ReturnBank,12,Height

Return ReturnBank 

Else 

ReturnBank = CreateBank(16)

Return ReturnBank 

EndIf 

End Function 

Function EditAlpha(X,Y,Alpha)

RGB1=ReadPixel(X,Y)
r=(RGB1 And $FF0000)Shr 16
g=(RGB1 And $FF00) Shr 8
b=RGB1 And $FF
a=(RGB1 And $FF000000)Shr 24

a=Alpha

Return newrgb = (a Shl 24) Or (r Shl 16) Or (g Shl 8) Or b

End Function 

;End Functions;

Graphics 800,600
CakeImg = LoadImage("Cake1.bmp")
CakeImg2 = LoadImage("Cake2.bmp")
MaskImage CakeImg,255,0,0

Lolz = Maximum(200,180) ;Object 1 = 100 X, Object 2 = 68 X
Lolz2 = Maximum(200,180) ; Object 1 = 100 Y, Object 2 = 68 Y
Lolz3 = Minimum(232,212); Object 1 Y + H = 132, Object 2 Y + H = 100
Lolz4 = Minimum(132,212)
Col = Calculate(200,200,32,32,180,180,32,32)

While Not KeyHit(1)
Cls 
DrawImage CakeImg,200,200
DrawImage CakeImg2,180,180

Text 0,12,"Maximum X:" + Lolz
Text 0,24,"Maximum Y:" + Lolz2
Text 0,36, "Minimum Y + H:" + Lolz3
Text 0,48, "Minimum X + W:" + Lolz4
Text 0,60, "Collision Pos X:" + PeekInt(Col,0)
Text 0,72, "Collision Pos Y:" + PeekInt(Col,4)
Text 0,84, "Collision Pixel W:" + PeekInt(Col,8)
Text 0,96, "Collision Pixel H:" + PeekInt(Col,12)

For x = PeekInt(Col,0)  To PeekInt(Col,0) + PeekInt(Col,8)  
For y =  PeekInt(Col,4) To PeekInt(Col,4) + PeekInt(Col,12)  
EditAlpha(x,y,0)
Next
Next 

Flip 

Wend 



Right, so my entire basis, is futile. In Blitz3D it seems alpha is ALWAYS
set to 255. I don't wanna switch to 3D, so any suggestions to where, I won't have too are really appreciated.

Last edited 2011


Matt Vinyl(Posted 2011) [#5]
Naïvely or not, one of the key reasons I moved over to BMax was the ease of it's blending modes (Alpha, Colour, etc.) A shame there was no similar functionality in B3D.


Kenshin Yurihara(Posted 2011) [#6]
For, I have also considered moving over to BMax, but I don't have much experience with it, yet, nor do I have the money to purchase it right now.

I like doing things in C++, but without using all these libraries, that
either cost or make it where you can only create non-commercial products,
I have to create collision functions and what not myself, which
takes me a good bit of math and greatly decreases productivity.

Then again, I had to use the same collision code, I used for per-pixel collision(I learned it at SDL tutorials(giving credit, so I aint "stealing"))
to check if the "light" intersects with the image, on this code.

Decisions, decisions.

Last edited 2011


Kenshin Yurihara(Posted 2011) [#7]
Wow, writing this tile map editor, having "moving tiles" translate
to on screen, if the "camera" is over them, made Blitz3D freeze to death
and my CPU (Quad Core 3.2..) fan's speed up, because it got hotter,
really fast too.

Edit: Ah, the "Print" command I had trying to keep up with it,
is what froze everything. Fail print..

Last edited 2011


Ross C(Posted 2011) [#8]
Remember, if the screen is of high enough resolution, you may get away with a checkered pattern shadow. Just a checkered pattern of black pixels and masked pixels. This will appear as a shadow effect. At low resolutions, it looks bad though... :)


Warner(Posted 2011) [#9]
I think it is easiest to use 2D-in-3D graphics if you need special effects, such as alpha blending, rotation, scaling and additive blend.


Kenshin Yurihara(Posted 2011) [#10]
It more then likely is, I'm probably going to be stick to plain 2D till I get ALL this tile mapper done. I'm adding in a basic button system, cause hitting 20 different keys to assign values, is tiring. After I do that, I think I'm going to look into Blitz3D's Camera's ortho projection.

Using the ortho projection and sprites, I could get a kick ass look, because even though I really didn't wanna use 2D-in-3D, it'll be better for effects and speed. So, no reason not too. Lolz

Last edited 2011


b32(Posted 2011) [#11]
In that case, the following archive entry provides functions that should make such a transistion (from 2d to 3d) easier: http://www.blitzmax.com/codearcs/codearcs.php?code=2680


Kenshin Yurihara(Posted 2011) [#12]
Thats pretty good looking, thanks :)

Yo, for the record, MouseHit can cause some funk errors. Having it check if images over lap AND mousehit(1) will only make my first "button" react, but if I change it to if ImagesOverlap then if mousehit(1) they all react.
Just something interesting I found to share..

Last edited 2011


Warner(Posted 2011) [#13]
Store MouseHit in a variable, then use that variable. Basically, it should only be used once a loop.


Kenshin Yurihara(Posted 2011) [#14]
Good point I didn't think about that, but just throwing it after the for loop, fixes that. :) I'm now working on converting this while tile mapper to 2D-in-3D, wish me luck. I probably won't get far to day, but I'm a try.

Right, so sprites aren't the way for things like this I hear. So, I'm hearing using quads is the way to go.

If I'm not mistaken(which I might be) a quad is just 2 triangles in a flat surface, yes?

Last edited 2011


Kenshin Yurihara(Posted 2011) [#15]
How does one go about a quad? because trying my method:

Type Quad
Field X
Field Y
Field Z
Field Width
Field Height
Field Mesh
End Type

Function CreateQuad(X,Y,W,H,Z)
Q.Quad = New Quad
Q\X = X
Q\Y = Y
Q\Z = Z
Q\Width = W
Q\Height = H
Q\Mesh = CreateMesh()
End Function


Function DrawQuads()
For Q.Quad = Each Quad
TempSurface = CreateSurface(Q\Mesh)

Vertex1 = AddVertex(TempSurface,Q\X,Q\Y,Q\Z)
Vertex2 = AddVertex(TempSurface,Q\X + Q\Width,Q\Y,Q\Z)
Vertex3 = AddVertex(TempSurface,Q\X,Q\Y + Q\Height,Q\Z)
Vertex4 = AddVertex(TempSurface,Q\X + Q\Width,Q\Y + Q\Height,Q\Z)

AddTriangle(TempSurface,Vertex1,Vertex2,Vertex3)
AddTriangle(TempSurface,Vertex4,Vertex2,Vertex3)

PositionEntity Q\Mesh,Q\X,Q\Y,Q\Z
Next
End Function


this doesn't do anything at all. Do I use a single mesh or a single surface per a quad? I really have no clue how to go about this.
Lolz

Last edited 2011

Last edited 2011


Warner(Posted 2011) [#16]
Your method seems to work, it only shows one triangle instead of two, because the second triangle you add isn't right. It should be Vertex3,Vertex2,Vertex4

You can, however, use a single mesh for all quads. That is faster than using one mesh pro quad. First, add vertices+triangles for each quad, and remember their vertex index (store Vertex1,2,3,4)
Later on, when updating the quads, use VertexCoords to change the position of the existing vertices.


Kenshin Yurihara(Posted 2011) [#17]
Ah, thanks Warner, you're a life saver.

I was misunderstanding how the triangles are drawn, I suppose.

Last edited 2011


Ross C(Posted 2011) [#18]
I posted a tilemap type thing in another thread, creating a tilemap mesh, giving each quad the ability to display different parts of a texture:



There's even a function to change the vertex co-ords of any of the tiles.

Last edited 2011


Kenshin Yurihara(Posted 2011) [#19]
Thank you, Ross :D, I'll look into something close to that, the only thing is I'm going to be using, Types for my Tiles. :)

I have to stop working for now, the girlfriend is coming over, I'll get back to yall at what I experience in this, deep thrilling task, once I get a chance to start back on it.

:)

Last edited 2011


Kenshin Yurihara(Posted 2011) [#20]
Okay, so going about texturing, any enlightenment on how to do it would be nice. I'm figuring I should, get the textures X,Y,W,H and do it with vertextexcoords, the same way I did it with, the Quad, X,Y,W,H

Ross' code was good reference, but some more enlightenment on exactly how it works and how to go about it, would be appreciated.


Sorry to ask so many questions, but I'm like learning and feel like a student, so I'm asking the teachers here. Haha..man.

@Ross: I'm looking at your vertextexcoords and I think when you divide 1.0/12(num tiles wide) and multiply it by TX, it gets the coresponding texture "frame" and starts it there?

Last edited 2011

Last edited 2011

Last edited 2011


Warner(Posted 2011) [#21]
First you would need to create a texture that contains the tiles. Since textures should have both a width+height that is a power of two,
it might be best to base your tilesize on it.
The following texture contains a 4x4 grid of tiles. Each tile is 32x32:


Then you would need to create a mesh that contains quads. Each quad should
use it's own vertices, because texture coordinates are set pro vertex.
So don't reuse vertices for multiple quads (no welding).

The uv coordinates for each quad are set to the range 0.0 to 1.0
This means on each quad, the entire texture is rendered.
If you want only one of the tiles to be placed on the quad, change the
coordiates to the range 0 to 0.25 instead.
Both the width and the height of the texture should be divided into 4
parts: 1 / 4 = 0.25

If you need more tiles, double both the width and height of the texture,
and change 0.25 to 0.125
It might be a good idea to make this value a Global variable (or Const) so
you can change it later on.

Last edited 2011


Kenshin Yurihara(Posted 2011) [#22]
I think I get it, I'm going to experiment a bit, then see how this works out. Thanks for clarifying that.


Kenshin Yurihara(Posted 2011) [#23]
Right so sorry to double post, but again no one will know that I answered, unless I do it. Anyway, I have been trying my best at this and well.
Without setting the vertex coords at the creation of a vertex, I could apply a texture with my texture function, but it wouldn't draw at the correct
position. So, I added vertex coords, then it wouldn't draw a new texture.

Then I took a break and came back and now, I get a MAV error for the TextureQuad function. I know
what a Memory Access Violation is, but I don't see why, I'm getting one.
If it was the Q\Surface, then I'd get a MAV for the DrawQuads function,
but I don't. I know the Q\VertStart Exist, so I donno why, I'm being
thrown a MAV error, because it was working at first, it just didn't draw
the new texture, lolz. I followed your suggestion, Warner and did 4x1 because that is easily dividable, I only have 1 Y because, its a old habbit of making straight strips out of my tiles.

Type Quad
Field Surface
Field Vertex1X,Vertex1Y,Vertex1Z
Field Vertex2X,Vertex2Y,Vertex2Z
Field Vertex3X,Vertex3Y,Vertex3Z
Field Vertex4X,Vertex4Y,Vertex4Z
Field VertStart
End Type 

Function CreateQuad(Mesh,VStart,X,Y,Z,W,H) 
Q.Quad = New Quad
Q\Surface = CreateSurface(Mesh)
Q\Vertex1X = X 
Q\Vertex1Y = Y
Q\Vertex1Z = Z
Q\Vertex2X = X + W
Q\Vertex2Y = Y
Q\Vertex2Z = Z
Q\Vertex3X = X
Q\Vertex3Y = Y - H
Q\Vertex3Z = Z
Q\Vertex4X = X + W
Q\Vertex4Y = Y - H
Q\Vertex4Z = Z
Q\VertStart = VStart
End Function 


Function DrawQuads() 

For Q.Quad = Each Quad
Vert1 = AddVertex(Q\Surface,Q\Vertex1X,Q\Vertex1Y,Q\Vertex1Z,.25,0)
Vert2 = AddVertex(Q\Surface,Q\Vertex2X,Q\Vertex2Y,Q\Vertex2Z,.25,0)
Vert3 = AddVertex(Q\Surface,Q\Vertex3X,Q\Vertex3Y,Q\Vertex3Z,.25,1)
Vert4 = AddVertex(Q\Surface,Q\Vertex4X,Q\Vertex4Y,Q\Vertex4Z,.25,1)


AddTriangle(Q\Surface,Vert1,Vert2,Vert3)
AddTriangle(Q\Surface,Vert3,Vert2,Vert4)
Next 

End Function 

Function TextureQuad(Mesh,Texture,VStart,Frame)

For Q.Quad = Each Quad 

If Q\VertStart = VStart
VertexTexCoords Q\Surface,Q\VertStart   ,.25 * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +1,.25 * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +2,.25 * Frame,1
VertexTexCoords Q\Surface,Q\VertStart +3,.25 * Frame,1
EndIf 
Next 

EntityTexture Mesh,Texture
End Function 


Graphics3D 800,600
SetBuffer BackBuffer()

Cam = CreateCamera()
MoveEntity Cam,0,0,-15

TileMesh = CreateMesh() 
Sur = CreateSurface(TileMesh)
Texture = LoadTexture("Art/TM/TileSheet.bmp",4)
CreateQuad(TileMesh,1,1,1,0,1,1)
TextureQuad(TileMesh,Texture,1,2)

While Not KeyHit(1)

DrawQuads()


RenderWorld
Flip
Wend 


Last edited 2011


Ross C(Posted 2011) [#24]
Here is a animated guf I did in another thread explaining the theory behind this:

www.rcsolutions.webspace.virginmedia.com/3duvcoords.gif

I'll take a look at your code.

EDIT what i notice first is your "drawing" your quads every frame. Once a mesh is created, it is there. Just point the camera at it RenderWorld(), and it will appear. It's not like 2d graphics in that respect.

Secondly, remember textures for 3d graphics need to be in power of 2's. so 32x512 etc. Try to keep at maximum, a 8:1 or 1:8 ratio between the width and height of the texture.

Last edited 2011


Ross C(Posted 2011) [#25]
Ok, in the texturequad function:

VertexTexCoords Q\Surface,Q\VertStart   ,.25 * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +1,.25 * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +2,.25 * Frame,1
VertexTexCoords Q\Surface,Q\VertStart +3,.25 * Frame,1


Your vertex co-ords are, assuming frame = 1: v1(.25,0) v2(.25,0) v3(.25,1) v4(.25,1)

Which would make your texture a flat line, and would be starting from 1/4 the way from the textures left. You need something like:

VertexTexCoords Q\Surface,Q\VertStart   ,0   * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +1,.25 * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +2,.0  * Frame,1
VertexTexCoords Q\Surface,Q\VertStart +3,.25 * Frame,1



Kenshin Yurihara(Posted 2011) [#26]
VertexTexCoords Q\Surface,Q\VertStart   ,0   * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +1,.25 * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +2,.0  * Frame,1
VertexTexCoords Q\Surface,Q\VertStart +3,.25 * Frame,1


Will make the texture sideways, sorry. Thats my bad for posting that untested code, because of the darn MAV. Thanks for helping me solve that too.
--This Part--
VertexTexCoords Q\Surface,Q\VertStart   ,0   * Frame,0
VertexTexCoords Q\Surface,Q\VertStart +1,.25 * Frame,1
VertexTexCoords Q\Surface,Q\VertStart +2,.0  * Frame,1
VertexTexCoords Q\Surface,Q\VertStart +3,.25 * Frame,0


Makes the texture upright, but, when I try to increase the "Frame" it still doesn't display the correct texture. It rips the first and the texture and the "frame" texture together. At first, the first UVs made when the vertices are made, made it where you couldn't display anywhere after the first "frame". So I removed them, now it tears the first texture
and the "frame" texture together. I tried changing all the coords over and over and that got me no where, then I tried moving the "entitytexture" portion around, also no changes. Hm...a interesting day.

-All the Codes the same, just the "--This Part--" thing is different from yours-

Last edited 2011


Ross C(Posted 2011) [#27]
Can you provide the texture your using? Also, did you take the drawquads() function out, because that shouldn't be there.


Kenshin Yurihara(Posted 2011) [#28]
I took drawquads out of the game loop, if thats what you mean.

http://imageshack.us/photo/my-images/804/tilesheetz.png/


Thats the re-done image, I cleaned out the old one yesterday, it is the exact same width and height and does the same thing. Image shack turned it into a .png its original a .bmp.

Last edited 2011


Ross C(Posted 2011) [#29]
Ok, I've noticed a couple of things. Firstly, your creating a new surface for every quad you create. And you create a surface before that:

Sur = CreateSurface(TileMesh)


You only need to create this surface above. Creating a surface in your createquad function isn't needed. Following on from this, you only need to texture your mesh once also, so that doesn't need a seperate function. I have converted your texturequad function, into set frame. You simply pass across a vstart (a multiple of 4, you will need to keep track of them), and a frame, from 0 to 3 in this case.

Secondly, based on the above, you need to keep a global index counter. Everytime you create a quad, assign this index to your type field "vstart", then increment this counter forward by 4.

Also, pass across your surface to the createquad and texturequad function. The mesh is of no relevance, as each surface is unique and can only be associated with one mesh.

Additionally, make your mesh and surface you create global variables, same with your texture, so they don't get lost.

Also, put the contents of your drawquad into the createquad function, so you are creating the vertices and creating the triangles all in the one place.

Lastly, I buggered up when suggesting the UV co-ords. It should have been:

	Vert1 = AddVertex(Q\Surface,Q\Vertex1X,Q\Vertex1Y,Q\Vertex1Z,.25 * Frame        ,0)
	Vert2 = AddVertex(Q\Surface,Q\Vertex2X,Q\Vertex2Y,Q\Vertex2Z,(.25 * Frame) + .25,0)
	Vert3 = AddVertex(Q\Surface,Q\Vertex3X,Q\Vertex3Y,Q\Vertex3Z,.25 * Frame        ,1)
	Vert4 = AddVertex(Q\Surface,Q\Vertex4X,Q\Vertex4Y,Q\Vertex4Z,(.25 * Frame) + .25,1)


Because 0 * anything, is always going to be zero. So you need to say, 0.25 (the frame x size) * frame. the exact same goes for the far right vertex, except you add on the width of a frame.

So, the full code is:

Type Quad
Field Surface
Field Vertex1X,Vertex1Y,Vertex1Z
Field Vertex2X,Vertex2Y,Vertex2Z
Field Vertex3X,Vertex3Y,Vertex3Z
Field Vertex4X,Vertex4Y,Vertex4Z
Field VertStart
End Type 

Function CreateQuad(Surface,X,Y,Z,W,H) 
	Q.Quad = New Quad
	Q\Surface = surface
	Q\Vertex1X = X 
	Q\Vertex1Y = Y
	Q\Vertex1Z = Z
	Q\Vertex2X = X + W
	Q\Vertex2Y = Y
	Q\Vertex2Z = Z
	Q\Vertex3X = X
	Q\Vertex3Y = Y - H
	Q\Vertex3Z = Z
	Q\Vertex4X = X + W
	Q\Vertex4Y = Y - H
	Q\Vertex4Z = Z
	Q\VertStart = vertex_index
	vertex_index = vertex_index + 4
	
	Vert1 = AddVertex(Q\Surface,Q\Vertex1X,Q\Vertex1Y,Q\Vertex1Z,.25 * Frame        ,0)
	Vert2 = AddVertex(Q\Surface,Q\Vertex2X,Q\Vertex2Y,Q\Vertex2Z,(.25 * Frame) + .25,0)
	Vert3 = AddVertex(Q\Surface,Q\Vertex3X,Q\Vertex3Y,Q\Vertex3Z,.25 * Frame        ,1)
	Vert4 = AddVertex(Q\Surface,Q\Vertex4X,Q\Vertex4Y,Q\Vertex4Z,(.25 * Frame) + .25,1)
		
	
	AddTriangle(Q\Surface,Vert1,Vert2,Vert3)
	AddTriangle(Q\Surface,Vert3,Vert2,Vert4)
	
End Function 



Function SetFrame(VStart,Frame)

	For Q.Quad = Each Quad 
	
		If Q\VertStart = VStart
			VertexTexCoords Q\Surface,Q\VertStart   ,.25 * Frame        ,0
			VertexTexCoords Q\Surface,Q\VertStart +1,(.25 * Frame) + .25,0
			VertexTexCoords Q\Surface,Q\VertStart +2,.25 * Frame        ,1
			VertexTexCoords Q\Surface,Q\VertStart +3,(.25 * Frame) + .25,1
		EndIf 
	Next 
	
End Function 


Graphics3D 800,600
SetBuffer BackBuffer()

Cam = CreateCamera()
MoveEntity Cam,0,0,-5

Global vertex_index = 0
Global TileMesh = CreateMesh() 
Global Sur = CreateSurface(TileMesh)
Global Texture = LoadTexture("Art/TM/TileSheet.bmp",4)
EntityTexture TileMesh,Texture

CreateQuad(Sur,1,1,0,1,1)
SetFrame(0,0)

While Not KeyHit(1)



	RenderWorld
	Flip
Wend
End


Sorry if you didn't won't single surface, but I think this will really drag without it.

Last edited 2011


Kenshin Yurihara(Posted 2011) [#30]
Well, see when this topic started out, it was about lighting, then finding out 2D cannot do much at all(lolz). Then it turned into talking about quads, so its not what I "want" I'm just trying to learn new techniques in B3D.

I owe ya one Ross, I'm a check this out.

Last edited 2011


Ross C(Posted 2011) [#31]
No bother man. Once you get the basics for the 2d tile map, you can get shadows working :)


Kenshin Yurihara(Posted 2011) [#32]
^^ I think, I'm going to make the "global vertex counter" thing, be timed by the number of quads. So that if I have to delete a quad on the fly, then all the vertices would be translated over.

Example:
Global Quads = 0
Global Vertices = Quads * 4

CreateQuad(somesurface,somex,somey,somez,somewidth,someheight) <-1
CreateQuad(somesurface,somex,somey,somez,somewidth,someheight) <-2

RemoveQuad(2)


Would translating vertices when a quad gets deleted, be a smart idea?
That way, theres no gaps.

Like this would cause:
CreateQuad(somesurface,somex,somey,somez,somewidth,someheight) <-1
CreateQuad(somesurface,somex,somey,somez,somewidth,someheight) <-2
CreateQuad(somesurface,somex,somey,somez,somewidth,someheight) <-3

RemoveQuad(2)


Because doing this, without translating the vertices over, Quad 3's
vertices would be 8-11 and Quad 1's would be 0-3. But if that doesn't really matter..I donno lolz. I just think it'd be a good idea.

Last edited 2011

Last edited 2011


Ross C(Posted 2011) [#33]
I dunno. Why would you want to delete a quad, or a tile, in a tilemap engine? You could always just hide it, by setting the VertexAlpha on each of it's 4 vertices. And remember, deleting vertices requires the whole mesh to be rebuilt.

You could house all your vertex index data in an array:

tile(1,0) = vertex_index

etc,

that way when you want to access a tile, you simple call the tiles(x,y) location:

SetFrame(x,y,frame)

Function SetFrame(x,y,Frame)

	Vstart = tile(x,y)  

	For Q.Quad = Each Quad 
	
		If Q\VertStart = VStart
			VertexTexCoords Q\Surface,Q\VertStart   ,.25 * Frame        ,0
			VertexTexCoords Q\Surface,Q\VertStart +1,(.25 * Frame) + .25,0
			VertexTexCoords Q\Surface,Q\VertStart +2,.25 * Frame        ,1
			VertexTexCoords Q\Surface,Q\VertStart +3,(.25 * Frame) + .25,1
		EndIf 
	Next 
	
End Function


That way you don't need to concern yourself with vertex indices, just the x,y location of the tile within the tilemap.

Function CreateQuad(Surface,X,Y,Z,W,H,tilex,tiley) 
	Q.Quad = New Quad
	Q\Surface = surface
	Q\Vertex1X = X 
	Q\Vertex1Y = Y
	Q\Vertex1Z = Z
	Q\Vertex2X = X + W
	Q\Vertex2Y = Y
	Q\Vertex2Z = Z
	Q\Vertex3X = X
	Q\Vertex3Y = Y - H
	Q\Vertex3Z = Z
	Q\Vertex4X = X + W
	Q\Vertex4Y = Y - H
	Q\Vertex4Z = Z
	Q\VertStart = vertex_index
	tile(tilex,tiley) = vertex_index
	vertex_index = vertex_index + 4
	
	Vert1 = AddVertex(Q\Surface,Q\Vertex1X,Q\Vertex1Y,Q\Vertex1Z,.25 * Frame        ,0)
	Vert2 = AddVertex(Q\Surface,Q\Vertex2X,Q\Vertex2Y,Q\Vertex2Z,(.25 * Frame) + .25,0)
	Vert3 = AddVertex(Q\Surface,Q\Vertex3X,Q\Vertex3Y,Q\Vertex3Z,.25 * Frame        ,1)
	Vert4 = AddVertex(Q\Surface,Q\Vertex4X,Q\Vertex4Y,Q\Vertex4Z,(.25 * Frame) + .25,1)
		
	
	AddTriangle(Q\Surface,Vert1,Vert2,Vert3)
	AddTriangle(Q\Surface,Vert3,Vert2,Vert4)
	
End Function


As above, you'd need to pass across the x,y co-ords of the tile within the tilemap, so the index could be stored within the array.

Again, ask yourself, do you need to delete tiles from the tilemap? I've never really seen that happen...


Kenshin Yurihara(Posted 2011) [#34]
Well, in a tile map, when first creating it, I create quads when I click, then I remove quads when I right click, so I can remove something from the tile map, but then again, I could just probably hide that one, then reposition it on my next click.


Ross C(Posted 2011) [#35]
Hmmm, I thought the idea of tilemaps, was you had the grid structure in place, and you click to paint a tile, and right click to maybe remove the paint from that tile. The quads should always be there, just with a different texture assigned to them, or made invisible? Do you have an example of what kind of tilemap your creating? That means your going to have the black of nothingness showing through. I'm confused slightly :)

Last edited 2011


Kenshin Yurihara(Posted 2011) [#36]
Well, in like a side scroller, you would have areas with no "tiles", there,
so the background would be, for example, blue, so it looks like sky, but I guess if I just set alpha to 0 on those, it'd work, but wouldn't that be a problem. For example, say you have a large size game map, 1000x1000
in size. You have 20 tiles(quads), per every 100x100, and 10 of those tiles are just invisible or display the blue background. You would be using 200 Left to right and 200 up to down, 400 total. Which is 1600 Vertices, 200 triangles. If I didn't have 10 of those quads existing, I could save you, 800 Vertices, 100 triangles and just have the camera display the blue background.

-Lets say those 10 that are just masked out, were accidently placed and since I couldn't delete them, I just masked them out-

I hope that accurately describes it...

Last edited 2011


Ross C(Posted 2011) [#37]
But in tilemap games, everything is made from tiles, even the sky. Just paint those tiles blue :) And saving 100 triangles isn't much, if anything. Plus, most tilemaps engines work, by only actually creating the tiles on the screen. You would have your grid of quads, and simply scroll the tile grid, the width of a tile, snap it back, repaint all the tiles with the tiles left of each other, and it gives the illusion of scrolling.

I'm not sure what your 20 tiles per every 100,100 means though? Each tile is a quad. The quad is textured with the image of that particular tile.


Kenshin Yurihara(Posted 2011) [#38]
I meant 1000x1000 width and length in map size. So, you had 20 quads aka 20 tiles. Lolz

Thanks Ross.

Last edited 2011


Warner(Posted 2011) [#39]
I think you don't need to create a 100x100 tiles. Instead, create only say 20x15 tiles. Then, make it look as if they are scrolling by alternating the texture that is applied to the tiles.
Think of it like this: (2d example, needs tiles.png from post above)


Last edited 2011


Kenshin Yurihara(Posted 2011) [#40]
Thats actually a amazingly epic idea, never thought about that. Also the 100x100 tiles was only a example and truth if I only had the tiles scrolling and the character never actually moving from them, then I could never actually get to a real existing place in the. Thats still a amazing idea though.

Last edited 2011


Kenshin Yurihara(Posted 2011) [#41]
I implemented a simple single surface, collision using the same method I used to check for image collision at the start of this thread.

It seems to work, maybe not 100% perfect, but none the less, it does
it decently.

I'll be working on Collisions with other objects next, after I fix up the few errors in my collision code.

Again, thanks for the help thus far guys.

Last edited 2011


mv333(Posted 2011) [#42]
You can do 2d alpha blending in Blitz3d, using the Extended B2D dll.


mv333(Posted 2011) [#43]
But in tilemap games, everything is made from tiles, even the sky.


What if you're using parallax scrolling?