Problems using Blitz for a 2D plataformer.

Blitz3D Forums/Blitz3D Programming/Problems using Blitz for a 2D plataformer.

Leandro(Posted 2016) [#1]
Greetings from Brazil.

I'm trying without success to use Blitz 3D to make a 2D plataform game (in this way I can use multiple resolution and camera zoom). I can of course create an ortoghaphic camera and use one sprite for each tile. However it seems that Blitz textures are wrapping for each sprite, and as a result, it creates many seams. Can anyone suggest a solution to fix it?

Thanks in advance!




Flanker(Posted 2016) [#2]
You'll have to use both ScaleSprite and ScaleTexture commands, try this :
ScaleSprite sprite,3,1
ScaleTexture texture,1,3



Leandro(Posted 2016) [#3]
I'm not scaling the textures. Actually both sprites and textures have a scale of 1. I'm just messing with camera zoom.


RemiD(Posted 2016) [#4]
Maybe try :
loadtexture(texturefilename,1+16+32) (or createtexture(width,height,1+16+32)
or
to disable the bilinear filtering on textures : http://www.blitzbasic.com/Community/posts.php?topic=103591 (#18)

also check that your textures are "clean" (with the exact colors that you want).

also check that the u,v coord of the vertices of your sprite mesh are correct.


Stevie G(Posted 2016) [#5]
Looks like the UV's.

Best to use quads rather than sprites to have more control of the UV's. You can then offset the uv's by 0.5/TextureWidth, 0.5/TextureHeight :

Global OU# = 0.5 / TextureWidth( MyTexture )
Global OV# = 0.5 / TextureHeight( MyTexture )

Function MESHquad()

	Local Mesh, S, v0,v1,v2,v3
	
	Mesh = CreateMesh()
	S = CreateSurface( Mesh )
	v0 = AddVertex( S, -1, 1, 0 , 0 , 0 )
	v1 = AddVertex( S, 1, 1, 0 , 1 - OU, 0  )
	v2 = AddVertex( S, 1, -1, 0, 1 - OU , 1 - OV )
	v3 = AddVertex( S, -1, -1, 0, 0 , 1 - OV )
	AddTriangle S, v2, v1, v0
	AddTriangle S, v0, v3, v2
	
	Return Mesh

End Function


You may also find that where the U or V coord is set to zero, offsetting by + OU or + OV might also help.


Leandro(Posted 2016) [#6]
RemiD, it worked great when I used 1+16+32 flags. Thanks!

Stevie G, could you please give me more information about UVs and quads? I'm quite used to Blitz 2D, but I'm still crawling into 3D programming. I read somewhere it'd be faster to use a grid of quads instead of, let me say, using 30x30 sprites.

Anyway, many thanks for your support! :)


steve_ancell(Posted 2016) [#7]
@Leandro:

UV is to polys and textures as XY is to bitmaps, or something like that anyways. ;)


RemiD(Posted 2016) [#8]
a vertex u,v coordinate represents the 2dposition (in percent) of a vertex on a texture.
u is the position (in percent) on the xaxis of the texture
v is the position (in percent) on the yaxis of the texture
0u,0v is at topleft of the texture
1u,0v is at topright of the texture
0u,1v is at bottomleft of the texture
1u,1v is at bottomright of the texture
u and v are floats between 0.0 and 1.0

if you know the texture width,height and if you know where you want to uvmap your vertices (so that the triangle between its 3 vertices will have the texels colors applied on it) you can calculate the u,v coord like so :
let's assume that
we have a 128width64height texture
we want vertex0 at the top middle of the texture, vertex1 at the bottom right of the texture, vertex2 at the bottom left of the texture
the u,v coords of each vertex will be :
vertex0 : u = 64.0/128 = 0.5 , v = 0.0/64 = 0.0
vertex1 : u = 128.0/128 = 1.0 , v = 64.0/64 = 1.0
vertex2 : u = 0.0/128 = 0.0 , v = 64.0/64 =1.0


related to this, is the concept of "unwrapping" some triangles of a surface which is duplicating some of its vertices so that triangles which share an edge can be projected on a 2d plan without distortion and can be separated on the texture to then be able to texelsfill the triangles and to uvmap the triangles

related to this, is the concept of "texelsfilling" which is the process of scaling up/down the triangles on the texture so that the texel size has a specific size in the 3d world and is not distorted

related to this, is the concept of "uvmapping", which is positionning the vertices on the texture in a way to fill them with the wanted texels colors and also to optimize the space they take on the texture


K(Posted 2017) [#9]
Here, you can borrow my sprite-substitute code:
Type Quad
Field entity,rx,ry,rz
Field tag$

Function CreateQuad%();QUAD_BLOC
m=CreateMesh(Parent)
s=CreateSurface(m)
v1=AddVertex(s,-1,-1,-1,1,0)
v2=AddVertex(s,1,-1,-1,0,0)
v3=AddVertex(s,-1,1,-1,1,1)
v4=AddVertex(s,1,1,-1,0,1)
t1=AddTriangle(s,v2,v1,v3)
t2=AddTriangle(s,v2,v3,v4)
RotateMesh m,0,0,180EntityFX m,13
Return m
End Function

Function NewQuad.Quad(parent=0)
q.Quad=New Quad
q\entity=CopyEntity(Base_Quad,parent)
Return q
End Function

Function UpdateQuads()
k=EntityPitch(cam,1):j=EntityYaw(cam,1)
For q.Quad=Each Quad
q\rx=EntityPitch(q\entity,1) : q\ry=EntityYaw(q\entity,1) : q\rz=EntityRoll(q\entity,1)
RotateEntity q\entity,k,j,q\rz,1
Next
End Function
Function ResetQuads() 
For q.Quad=Each Quad
RotateEntity q\entity,q\rx,q\ry,q\rz,1
Next
End Function
Function FreeQuad(entity)
If EntityName(entity)=""
 For q.Quad=Each Quad
  If q\entity=entity Delete q
   Next
Else
 q.quad=Object.quad(EntityName(entity))
  Delete q
	EndIf
FreeEntity(entity)
Return False
End Function


Notes: 1)Include the line: "Global Base_Quad=CreateQuad()"
2)'cam' ie. your camera, needs must be Global, or passed in.
3)For best cleanup results name q\entity with the Object Handle Q


Hopefully that's vaguely useful. The UVs are easily accessible in the CreateQuad() function (the last two digits of the AddVertex lines), just keep in mind that that alters the base quad and thus all quads derived from him.