superstrict oop example

BlitzMax Forums/MiniB3D Module/superstrict oop example

Barbapapa(Posted 2007) [#1]
Hi, I am trying to translate one of the B3D examples (birdie\blobs\main.bb) into a full fledged oop example. I have noticed that all the MiniB3D examples are more or less exactly like the B3D examples.
But I must admit that I have failed ;/ so if someone could please be so kind and tell me what is wrong with my code and why?

I can't seem to add new blobs on the picked surface of a blob. This version now does really weird things. I guess that I fundamentally misunderstand some important thing about oop here ;)

Here's the original example:
;
;	Camera Pick and parenting entities
;	dave@...
;	David Bird(Birdie)
;
Graphics3D 1024,768,32,2
SetBuffer BackBuffer()

cam=CreateCamera()
PositionEntity cam,0,0,-6

light=CreateLight()
TurnEntity light,45,45,0

Type blob
	Field ent		;entity eg blob
	Field x#,y#,z#	;rotation
End Type

Global sp=CreateSphere(5)
EntityPickMode sp,2
HideEntity sp

;Stage pivot
stage=CreatePivot()

;Add the First Blob the the scene
Add_Blob(stage,0,0,0)

While Not KeyDown(1)
	xm=MouseX()
	ym=MouseY()
	
	; Left Click the add a blob
	If MouseHit(1) Then
		ent=CameraPick(cam,xm,ym)
		If ent<>0 Then
			PickedEntity
			Add_Blob(ent,PickedX(),PickedY(),PickedZ())
		End If
	End If
	
	;Spin the stage around
	If MouseDown(2) Then
		TurnEntity stage,Float(MouseXSpeed()/10),0,Float(MouseYSpeed()/10)
		MoveMouse 320,200
	End If
 
	If KeyHit(57) Then f=1-f
		;Update all the blobs If Not frozen
	If f=0 Then Update_Blobs()

	;Update the world and render
	UpdateWorld
	RenderWorld

	;Draw the cursor	
	Color Rand(100,255),0,0
	Line xm-3,ym,xm+3,ym
	Line xm,ym-3,xm,ym+3
	
	Color 255,255,255
	Text 0,0,"Left Click the add a blob. Right Click n Hold to Spin the stage."
	Text 0,15,"Last entity Picked:"+ent+" PX:"+PickedX()+" PY:"+PickedY()+" PZ:"+PickedZ()
	If f=0 Then a$="Running." Else a$="Frozen"
	Text 0,30,"Press Space the Freeze rotation:"+a$
	Text 0,45,\"dave@..."
	Flip
Wend

EndGraphics
End

Function Update_Blobs()
	For a.blob=Each blob
		TurnEntity a\ent,a\x,a\y,a\z
	Next
End Function

Function Add_Blob(par,x#,y#,z#)
	a.blob=New blob
	a\ent=CopyEntity(sp,par)
	PositionEntity a\ent,x,y,z,True
	EntityColor a\ent,Rand(100,255),Rand(100,255),Rand(100,255)
	s#=Rnd(.8,1)
	ScaleEntity a\ent,s,s,s
	a\x=Rnd(-1,1)
	a\y=Rnd(-1,1)
	a\z=0
End Function


and here is my miserable attempt :
'**************************************************************************************************
' This program was written with BLIde
' Application:
' Author:
' License:
'**************************************************************************************************

Import "../MiniB3D.bmx"

SuperStrict

Const WIDTH:Int = 800, HEIGHT:Int = 600, DEPTH:Int = 32, MODE:Int = 2

Graphics3D WIDTH, HEIGHT, DEPTH, MODE

Type TBlob
	
	Global Anzahl:Int
	Global blobs:TList = CreateList()
	Field ent:TEntity
	Field x:Float, y:Float, z:Float

	Method New()
		Anzahl :+ 1
	End Method

	Method addBlob(sp:TEntity,par:TEntity, x:Float, y:Float, z:Float)
		Local blob:TBlob = New TBlob
		Local ent:TEntity = CopyEntity(sp)
		DebugLog"x: " + String(Self.x)
		DebugLog"par: " + par.toString()
		PositionEntity ent,Self.x,Self.y,Self.z,True
		EntityColor ent,Rand(100,255),Rand(100,255),Rand(100,255)
		Local s:Float = Rnd(0.8,1)
		ScaleEntity ent,s,s,s
		Self.x = Rnd(-1,1)
		DebugLog"x2: " + String(Self.x)
		Self.y = Rnd(-1,1)
		Self.z = 0
		blobs.AddLast(ent) 
	End Method
	Method updateBlob()
		Local content:TEntity
		For content = EachIn blobs
			TurnEntity content, Self.x, Self.y, Self.z
		Next		
	End Method
	
End Type

Global cam:TCamera = CreateCamera()
PositionEntity cam,0,0,-6

Local lit:TLight = CreateLight()
TurnEntity lit,45,45,0


Global sp:TMesh = CreateSphere(5)
ENTITYPICKMODE sp,2
HideEntity sp	
Global stage:TPivot = CreatePivot()

Local blob:TBlob = New TBlob
blob.addBlob(sp,stage,0,0,0)

While Not KeyDown(KEY_ESCAPE)
	Cls
	Local xm:Float = MouseX()
	Local ym:Float = MouseY()
	'Left Click to add a blob
	If MouseHit(1)
		Local sel:TEntity = CAMERAPICK(cam,xm,ym)
		If sel <> Null
			sel = PICKEDENTITY()
			DebugLog "Picked"
			DebugLog "PickedX(): "+String(PICKEDX())
			DebugLog "PickedY(): "+String(PickedY())
			DebugLog "PickedZ(): "+String(PickedZ())
			DebugLog "PickedEntity(): "+ PICKEDENTITY().toString()
			DebugLog "SelectedEntity(): "+ sel.toString()
			DebugLog "Anzahl: " + TBlob.Anzahl
			blob.addBlob(sp,sel,PICKEDX(),PICKEDY(),PICKEDZ())
		Else 
			DebugLog "Nada"
		End If
	End If

	If MouseDown(2)
		TurnEntity stage, Float(MouseXSpeed()/10), Float(MouseYSpeed()/10),0
	End If
	Local f:Int
	If KeyHit(57) Then f = 1-f
	If f = 0 Then blob.updateBlob()
	
	UpdateWorld
	RenderWorld

	Flip
Wend


Edit: I replaced my code with a former working one, where I had to take the 'par' Parameter from CopyEntity out. This code now matches my last description.

Unfortunately I will be gone for the weekend, so I won't be able to investigate further. Maybe... hopefully... some BMax/MiniB3D Guru will be able to give me some helpful hints :) I WANT this thing going the oo way ;)





Dreamora(Posted 2007) [#2]
Tried to move global blobs:tlist into the type so it is a type specific global instead of somewhere else defined.

And on your problem: This shouldn't even work. You have local set defined in scope "if mousehit" so you cant redefine it within the for once again. Define that one as sel1 or something similar.


Barbapapa(Posted 2007) [#3]
I removed the For EachIn Next loops, they didn't help me.
I placed the createlist in the Typedef again.
When I remove the par from
Local ent:TEntity = CopyEntity(sp,par)
then I get the blobs again, but all centered around the first one + I cannot click on them. That's why I started the list anyhow, but without success. Hmmm????


Barbapapa(Posted 2007) [#4]
Bump!

I hope I'm not bumping to soon, but I couldn't get the thoughts of my head about what's going wrong.
- If I take out the par Parameter from the CopyEntity command I can create new blobs, but can't click on them, only on the first blob
- If I add the par Parameter no blobs are created.

I don't understand this...please help.....


simonh(Posted 2007) [#5]
Change the following line in addblobs...

PositionEntity ent,Self.x,Self.y,Self.z,True


...to this...

PositionEntity ent,x,y,z,True


The turning doesn't work correctly however as MiniB3D's TurnEntity isn't yet the same as B3D's.


Barbapapa(Posted 2007) [#6]
I'm afraid this doesn't help me ... I tried this before but the biggest problem I have right now, is that I can't seem to be able to use the par parameter. Any ideas? should it work? I haven't seen any examples using it!


H&K(Posted 2007) [#7]
1) Why not put the New blob command inside AddBlob (And make it a Function) As apposed to haveing two News
2) Point in your code to where the parameters passed to AddBlob, are actualy made part of any blob. (If you notice you dont actualy give self.x,y,z any values untill after you have already tried to use them) EDit: I see this is what SimonH has pointed out


Barbapapa(Posted 2007) [#8]
I already tried this too , but the procedure is that I must first create ONE blob at 0,0,0 which serves as parent blob.
Therefore it is called once before the main loop.

Normally every new blob I create in the loop is created at the pick surface position and is parent of the picked blob.

Have a look at the original sample pasted in my first post.

If I set ent to global
Global ent:TEntity = CopyEntity(sp,par)
I get the new blobs now when I have the par parameter on. But the other blobs aren't drawn anymore?? It did this with methods and functions, all alike, as long as ent is Global. My poor head....

Can it really be so difficult to convert this sample to superstrict oop?


simonh(Posted 2007) [#9]
I wouldn't get so hung up about superstrict oop! You're just converting it to BlitzMax, superstrict and oop aren't really big factors here.

Strangely if you comment out ScaleEntity, it works.

Here's a modified working version:



I'm still investigating why this is at the moment...


simonh(Posted 2007) [#10]
FINALLY found the problem! And it's a devious little bug within MiniB3D itself.

What's happening is that when you pick the first sphere, you are in fact picking something else - the hidden sp entity that is created at the start.

Because it is hidden, it really shouldn't be pickable - but this has been overlooked in MiniB3D. So then all further new spheres are hidden too!

The ScaleEntity thing really threw me off the scent - the reason that removing ScaleEntity works is because with it, the first sphere is always smaller than the sp sphere, so you always pick sp.

Apologies for the hair-pulling this may have caused you! At least I've rooted a bug out from it.

Here's the fully working version, with the entity types moved around so that sp isn't pickable.




Barbapapa(Posted 2007) [#11]
SuperDuper :) Can't thank you enough, saved me before growing totally insane ;)

What you actually did was simply move the EntityPickMode command in the type definition. This seems totally logical to me, don't know why I overlooked this? If there wasn't the bug then that would have meant that no blob would have been created at all, and with the bug it was either invisible or only created on the root blob, correct?

But as I understand it, the EntityPickMode command definitely must be in the type definition, true?

It's really fun working with MiniB3D :)


simonh(Posted 2007) [#12]
If there wasn't the bug then that would have meant that no blob would have been created at all

No, without the bug it would have worked. CopyEntity would have copied the entity pick mode from sp to the next blob and so on.

and with the bug it was either invisible or only created on the root blob, correct?

With the bug all new blobs were invisible, that's all.

But as I understand it, the EntityPickMode command definitely must be in the type definition, true?

No, it doesn't matter. In the above example I just made sure sp had no pick mode so it couldn't be picked.


Barbapapa(Posted 2007) [#13]
With the bug all new blobs were invisible, that's all.

well I had
Local ent:TEntity = CopyEntity(sp)
and then I had all blobs drawn around the hidden parent blob. But of course this wasn' t the main intention ;)
Having
Global ent:TEntity = CopyEntity(sp,par)
only the newest blob was visible, so your workaround definitely did the trick.

Will you fix the bug in one of the next versions?


H&K(Posted 2007) [#14]
What's happening is that when you pick the first sphere, you are in fact picking something else - the hidden sp entity that is created at the start
hahahaha. Thats what I had thought the answer was until you posted that it was in scaleentity, so I stopped working on it.

(Well to be exact, Id gotten as far as showing PickedX,y and Z were not picking what I was pointing at)