2D with Blitz3D

Blitz3D Forums/Blitz3D Programming/2D with Blitz3D

Can someone help me out on some resources on doing this? Using the blitz3d 3d commands for 2d stuff (using textures and what not). I don't exactly want to use someone elses code, but I would like a starting point to study from so I can write my own 2d-in-3d code. I looked in the Code Archives and didn't see anything, but maybe I just missed some.

Keith

using the CameraProjMode camera,2 will force it into a 2D-like view.

Working with 3d, 2d style is very simple. Not much diferent from working with 2d. Its all about exchanging the y and z
axis so y becomes z.(if you want it so that your just looking down on the player that is.) Set up a 3d camera,
create a player and parent the camera to the player.

Graphics 3d 640,480
plane=createplane()
cam=createcamera()
player=createsphere(32,cam);or loadmesh(whatever,cam)
Oh here had this made already.

Graphics3D 640,480
AutoMidHandle True
light=CreateLight() 
RotateEntity light,90,0,0 

Global p_alpha#=.5

type_ground=1 
type_character=2
type_charactershield=4 
 
type_scenery=3 

p_pivot=CreatePivot()
EntityRadius p_pivot,2.4
PositionEntity p_pivot,0,4,0
plane=CreatePlane()
EntityColor plane,200,100,0

cube = CreateCube(p_pivot)
EntityRadius cube,2.4 

sphere = CreateSphere(16,p_pivot)
EntityRadius sphere,2 
ScaleEntity sphere,2,2,2
EntityAlpha sphere,p_alpha#
PositionEntity sphere,0,0,0
PositionEntity cube,0,0,0
cam = CreateCamera(p_pivot)
PositionEntity cam,0,1,10
PointEntity cam,cube

EntityType sphere,type_charactershield
EntityType cube,type_character
EntityType plane,type_ground
EntityType p_pivot,type_character


;EntityType red,type_scenery

 
 shieldsgone=True   

Global red=CreateCube()

backround()

;Collisions type_character,type_scenery,method,response 
Collisions type_character,type_scenery,3,1 
Collisions type_character,type_ground,2,1 
;Collisions type_charactershield,type_ground,2,1 

While Not KeyHit(1)
TurnEntity sphere,5,5,0
If KeyDown(200) TranslateEntity p_pivot,0,.5,0
If KeyDown(208) TranslateEntity p_pivot,0,-.5,0
If KeyDown(203) TranslateEntity p_pivot,.5,0,0
If KeyDown(205) TranslateEntity p_pivot,-.5,0,0

If KeyDown(30) TurnEntity cam,0,.5,0
If KeyDown(32) TurnEntity cam,0,-.5,0
If KeyDown(17) TurnEntity cam,.5,0,0
If KeyDown(31) TurnEntity cam,-.5,0,0

If EntityCollided (p_pivot,type_ground) Then
	p_alpha#=p_alpha#-.001
	EntityAlpha sphere,p_alpha#
EndIf 

If KeyHit(57) Then
    
	p_alpha#=.5 
	EntityAlpha sphere,p_alpha#
EndIf 

RenderWorld()
UpdateWorld()
Flip


Wend
End

Function backround()
 
ScaleEntity red,10,10,1
RotateEntity red,0,90,0
EntityColor red,200,10,10
PositionEntity red,4,0,-10
End Function


Complete with basic collisions. And if the player collides with the plane, shields will start to fade. I guess it would be a side scroller here but could be used as a to down just reposition the camera above the player and point it at the player. Which could be anything.

Somehow I think he meant 3d accelerated 2d. Not 3d that looks like 2d.

http://www.blitzbasic.com/toolbox/toolbox.php?tool=71
Supposedly a nice one. I have not used it.

same example without errors:
Graphics3D 640,480
AutoMidHandle True
light=CreateLight() 
RotateEntity light,90,0,0 

Global p_alpha#=.5

type_ground=1 
type_character=2
type_charactershield=4 
 
type_scenery=3 

p_pivot=CreatePivot()
EntityRadius p_pivot,2.4
PositionEntity p_pivot,0,4,0
plane=CreatePlane()
EntityColor plane,200,100,0

cube = CreateCube(p_pivot)
EntityRadius cube,2.4 

sphere = CreateSphere(16,p_pivot)
EntityRadius sphere,2 
ScaleEntity sphere,2,2,2
EntityAlpha sphere,p_alpha#
PositionEntity sphere,0,0,0
PositionEntity cube,0,0,0
cam = CreateCamera(p_pivot)
PositionEntity cam,0,1,10
PointEntity cam,cube

EntityType sphere,type_charactershield
EntityType cube,type_character
EntityType plane,type_ground
EntityType p_pivot,type_character



 
 shieldsgone=True   

Global red=CreateCube()
EntityType red,type_scenery

background()

;Collisions type_character,type_scenery,method,response 
Collisions type_character,type_scenery,2,2 
Collisions type_character,type_ground,2,1 
Collisions type_charactershield,type_ground,2,1 

While Not KeyHit(1)
	TurnEntity sphere,5,5,0
	If KeyDown(200) TranslateEntity p_pivot,0,.5,0
	If KeyDown(208) TranslateEntity p_pivot,0,-.5,0
	If KeyDown(203) TranslateEntity p_pivot,.5,0,0
	If KeyDown(205) TranslateEntity p_pivot,-.5,0,0
	
	If KeyDown(30) TurnEntity cam,0,.5,0
	If KeyDown(32) TurnEntity cam,0,-.5,0
	If KeyDown(17) TurnEntity cam,.5,0,0
	If KeyDown(31) TurnEntity cam,-.5,0,0
	
	If EntityCollided (p_pivot,type_ground) Then
		p_alpha#=p_alpha#-.001
		EntityAlpha sphere,p_alpha#
	EndIf 
	
	If KeyHit(57) Then
	    
		p_alpha#=.5 
		EntityAlpha sphere,p_alpha#
	EndIf 
	
	RenderWorld()
	UpdateWorld()
	Flip


Wend
End

Function background()
	ScaleEntity red,10,10,1
	RotateEntity red,0,90,0
	EntityColor red,200,10,10
	PositionEntity red,4,0,-7
End Function


Get SpriteMaster Pro, nothing makes it easier :) (it should be on the blitzcoder showcase)

Tracer

Yeah sorry I wasn't clear, I meant hardware accelerated 2d (using 3d hardware for sprites and such)... And as I said a starting point to study from so I can write my own routines.

Thanks :-)

Keith

Oh sorry.
Wolron: What errors?

JFK has a really simple example in the code archives that I think is a great starting point.

The only thing that I saw in the code archives (and yes i clicked his name and code archive entries) is a thing that allows you to position blitz3d sprites as you would 2d sprites, but from what i've heard in the past that the blitz3d sprite system is somewhat slow, i'd rather learn to use textures and figure out how to place them myself. So any other resources on using blitz3d and texture mapping to create my own hardware accelerated sprite system?

Thanks

Well, for one, you misspelled background as backround. Fatal error...

Thanks for the endorsement Tracer. (SpriteMasterPro in my sig-link)

Something pretty simple to try out here.
It's a very fast single-surface approach. Not pixel perfect though:

; Single-Surace Quads


Const numquads=50
numQI=numquads*4-4

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

HidePointer

cam=CreateCamera()
MoveEntity cam,0,0,-6.5
MoveMouse 320,240

; container for quads
quad=CreateMesh()
surf=CreateSurface(quad)
EntityFX quad,1+2+32

; texture for quads
;; tex=LoadTexture("testimage.png")
;; EntityTexture quad,tex

For d=1 To numquads
  q=AddQuad(surf,Rnd(-10,10),Rnd(-10,10),Rnd(25))
  QuadColor surf,q,Rand(255),Rand(255),Rand(255) , Rnd(1.0)
Next

mainquad=AddQuad(surf,0,0)

; ==================================================

Repeat

	If KeyHit(17) ;[W]
		w=Not w
		WireFrame w
	EndIf

	If KeyDown(57) ; [SPACE]
		RotateQuad surf,mainquad,1.5
	EndIf
	
	For r=0 To numQI Step 4
		RotateQuad surf,r,0.5 + Float(r)/500
	Next
	
	mx#=mx#+Float(MouseXSpeed())/16.0
	my#=my#-Float(MouseYSpeed())/16.0
	MoveMouse 320,240

	PositionQuad surf,mainquad,mx,my,0

	RenderWorld

	Color 100,100,100
	Text 20,40,"[MOUSE] = move main quad"
	Text 20,60,"[SPACE] = rotate main quad"
	Text 20,80,"[W] = Wireframe"
	Text 80,130,"No. of quads = "+numquads

	; reference box	
	Color 200,0,0
	Rect 320,140,100,100,0
	Flip

Until KeyHit(1)

End

; ==================================================

Function AddQuad%(s%,x#,y#,z#=0)
	Local o%,v%
	v=AddVertex(s,x-1,y-1,z ,0.0,1.0)
	AddVertex s,x+1,y-1,z , 1.0,1.0
	AddVertex s,x-1,y+1,z , 0.0,0.0
	AddVertex s,x+1,y+1,z , 1.0,0.0
	AddTriangle s,v+0,v+2,v+1
	AddTriangle s,v+1,v+2,v+3
	For o=0 To 3
		VertexNormal s,v+0,0,0,-1
	Next
	Return v
End Function

Function QuadColor(s%,i%,r%,g%,b%,a#=1.0)
	Local o%
	For o=0 To 3
		VertexColor s,i+o,r,g,b,a
	Next
End Function

Function PositionQuad(s%,i%,x#,y#,z#=0.0)
	Local dx#,dy#,dz# , o%
	dx=x-VertexX(s,i) :	dy=y-VertexY(s,i) : dz=z-VertexZ(s,i)
	For o=0 To 3
		VertexCoords s,i+o,VertexX(s,i+o)+dx,VertexY(s,i+o)+dy,VertexZ(s,i+o)+dz
	Next
End Function

Function RotateQuad(s%,i%,ang#)
  Local x#=VertexX(s,i) , y#=VertexY(s,i) , z#=VertexZ(s,i) , v%
	For v = i To i+3
		VertexCoords s,v,(Cos(ang)*VertexX(s,v))-(Sin(ang#)*VertexY(s,v)),(Sin(ang)*VertexX(s,v))+(Cos(ang)*VertexY(s,v)),VertexZ(s,v)
	Next
	PositionQuad s,i,x,y,z
End Function


Thanks alot JimB :-)

Keith

.

You can also get pixel-perfect 3D planes by placing the camera in the right place, regarding the screen resolution.

example for 800x600:

cam=CreateCamera()
CameraClsMode cam,False,True
PositionEntity cam,(800/2)+0.5,-((600/2)+0.5),-(scrX/2)


.. now, you can control your 3D sprites with 2D coords, but you must negate the Y value.

RESTRICTION: it only runs with 64x64 and 256x256 texture size

.

It's rather easy to do, actually there's already the "sprite" commands that can be used for that. But if you want performance, you might want to use a single surface engine, take a look at the code archives you'll find examples.
Also some people recommend orthographic projections which indeed make things easier, but I personally didn't use it because you might want to use 3 perspective for nice parallax effects and also throw in some 3D elements in your 2d backgrounds from time to time.