wierd type problem...

Blitz3D Forums/Blitz3D Programming/wierd type problem...

SSS(Posted 2003) [#1]
hi all, i am writing a small space combat game type thing and i was trying to do a smart camera thing. every frame a new type Vector3 is made that logs the ships position at that time, when the ship is a certain distance from the camera the camera starts going through these points following the ship but i have run into a very strange problem with types and i could really use some help... i am not a novice at typs yet i have no idea why this is not working

Function PopCamera(this.Ship)
	v.Vector3 = First Vector3
	PositionEntity Camera,v\x,v\y,v\z
	PointEntity Camera,this\Model
	Delete v
End Function


without the delete v the code works but with it it seems to delete the this\Model a blitz .x model but it does not touch the ships type!!!!!


thanks for any help :D
bye


Floyd(Posted 2003) [#2]
'Delete v' certainly won't delete the ship, but it will probably mess up the camera.

Notice that *every* call to PopCamera deletes one vector.
So after enough calls they are all gone.

Consider this example:

Type Vector3
	Field x#,y#,z#
End Type

Local v1.Vector3, v2.Vector3  ; Will be vectors, right now they are Null.

If v1 = Null Then Print "v1 is Null"

v1 = New Vector3  ; creates actual vector for v1.

v2 = v1  ; v2 and v1 both point at the same object (vector).

v1\x = 123

Print "v2\x = " + v2\x  ; v2 is just another name for v1

Delete v2

Print v1\x  ; Error, object pointed to by v1 no longer exists

WaitKey 
End



SSS(Posted 2003) [#3]
yeah i know but every frame i add another Vector3 to the list... :( so it should never run out besides the error message im getting when the delete v command is there is an Object does not exist pointing to the Model variable in the ship type please help


John Pickford(Posted 2003) [#4]
I would definately check that v is valid

v.Vector3 = First Vector3

if v=null then return

I'm assuming that FIRST will return null if there are none of the particular type.


SSS(Posted 2003) [#5]
thats what i thought at first as well but the error im getting is in this line of code in a different function
TranslateEntity this\Model,this\Velocity\x,this\Velocity\y,this\Velocity\z


skidracer(Posted 2003) [#6]
So you have to trace back in the function where it is breaking (because somehow I think you are making this=null? due to a delete ship or just uninitialized variable due to strange state you elude to in your method of emptying lists...)


big10p(Posted 2003) [#7]

thats what i thought at first as well but the error im getting is in this line of code in a different function
TranslateEntity this\Model,this\Velocity\x,this\Velocity\y,this\Velocity\z



...and how is the var 'this' defined in that function? Is it passed in as a param? You dont have 'this' defined alsewhere as a global, do you?

Also, can you show us how all the relevant Types are defined, please. :)


John Pickford(Posted 2003) [#8]
this.ship is passed as a parameter.


big10p(Posted 2003) [#9]
Yeah, it is in the PopCamera() function but he said the error occured in another func.


yinch(Posted 2003) [#10]
I think you should expand the line:
TranslateEntity this\Model,this\Velocity\x,this\Velocity\y,this\Velocity\z 


to
if (this = null) Then RuntimeError("Invalid Type Pointer")
Mo = this\Model
v.Vector3 = this\Velocity
if (v = null) Then RuntimeError("Null Velocity Vector")
Translate Mo, v\x, v\y, v\z


Looking at it your code, I am pretty sure that the problem is that the Velocity that 'this' points at is being deleted by the PopCamera function.
So initially you set the
Ship\Velocity = <some Vector3>

and then subsequently the
delete V (first Vector3)

deletes the Vector3 that is pointed at by Ship\Velocity


You could try (just for debugging) changing the translateentity line to:
TranslateEntity this\Model, 0,0,0 

And see if that eliminates the error you are currently getting. It will confirm that this\Model is intact and the problem is with the Velocity part of the line.


SSS(Posted 2003) [#11]
hi all, thanks for all the help, its stil not working and it is very annoying, here is the function that the translate entity is from + all the type defanitions
this exists but it is the Model which does not... even tho i am not deleting it anywhere in my code?!?!?!?


Function UpdateShip(this.Ship)
	TranslateEntity this\Model,this\Velocity\x,this\Velocity\y,this\Velocity\z
	If this\Velocity\y<1 Then this\Velocity\y = this\Velocity\y-0.001
	this\Velocity\z=this\Velocity\z-this\Velocity\z/30
	this\Velocity\y=this\Velocity\y-this\Velocity\y/30
	this\Velocity\x=this\Velocity\x-this\Velocity\x/30
	this\Rotation\x=this\Rotation\x-this\Rotation\x/5
	this\Rotation\z=this\Rotation\z-this\Rotation\z/10
	PushCamera(this)
End Function 


;A Positional Vector
Type Vector3
	Field x#,y#,z#
End Type


;The Ship Type
Type Ship
	Field Acceleration#;the acceleration of our ship
	Field Position.Vector3;the position of our ship
	Field Velocity.Vector3;the Velocity of our ship
	Field Rotation.Vector3;the Direction our ship is going
	Field Model;the model our ship is using
	Field Size.Vector3;the size of our ship
	Field Name$
End Type 



yinch(Posted 2003) [#12]
if you just put a "return" as the first line of the UpdateShip function, does the program work? i.e. do you see the models being drawn on screen?

Also can you show the code where you load the model and assign it to the Model Field of the ship.

y'03


yinch(Posted 2003) [#13]
Can you show the code that creates the new Vector3s?

y'03


Who was John Galt?(Posted 2003) [#14]
You are storing loads of different stuff as 'vector3' type instances- velocities, rotations, etc. When you define ship\rotation, for example, this creates a new 'vector3' in memory, with ship\rotation just holding a handle or reference to it. If you go about deleting the vector3s from the list, what's to say that the first vector3 you are deleting is not ship\velocity or whatever? I think new command adds a new vector at the end of the list, so all your previous ship positions are added at the end and all your other vectors will get deleted before you get to them.


SSS(Posted 2003) [#15]
thanks for all your ideas and help, i still have not found the problem so im going to release all of the source for you guys to see maybe that will help... i just started so it shouldnt matter to much, the types are not finalized and many of the fields are not used

here are the functions
;A Positional Vector
Type Vector3
	Field x#,y#,z#
End Type

;A Color Vector
Type Vector4
	Field a,r,g,b
End Type

;The Ship Type
Type Ship
	Field Acceleration#;the acceleration of our ship
	Field Position.Vector3;the position of our ship
	Field Velocity.Vector3;the Velocity of our ship
	Field Rotation.Vector3;the Direction our ship is going
	Field Model;the model our ship is using
	Field Size.Vector3;the size of our ship
	Field Name$
End Type 
	

Function CreateVector3.Vector3(x#,y#,z#)
	v.Vector3 = New Vector3
	v\x = x#
	v\y = y#
	v\z = z#
	Return v
End Function

Function CreateVector4.Vector4(a,r,g,b)
	v.Vector4 = New Vector4
	v\a = a
	v\r = r
	v\g = g
	v\b = b
	Return v
End Function 

Function Initialize()
	Graphics3D 640,480,32,1
	SetBuffer BackBuffer()
	Camera = CreateCamera()
	PositionEntity Camera,0,5,-10
End Function 

Function CreateShip.Ship(filename$)
	;Create The object and zero all variables
	this.Ship = New Ship
	this\Position = CreateVector3(0.0,0.0,0.0)
	this\Velocity = CreateVector3(0.0,0.0,0.0)
	this\Rotation = CreateVector3(0.0,0.0,0.0)
	this\Size = CreateVector3(0.0,0.0,0.0)
	
	corrected = False
	in = ReadFile(filename)
	;While not the end of the file
	While Not Eof(in)
		inLine$ = ReadLine$(in)
		If Instr(inLine$,"Ship Config 0.1")
			corrected = True
		EndIf
		If corrected = True And Instr(inLine$,"Ship.Name")
			e = Instr(inLine$,"=")
			this\Name = Mid$(inLine$,e+2,Len(inLine$))
		EndIf
		If corrected = True And Instr(inLine$,"Ship.Model")
			e = Instr(inLine$,"=")
			this\Model = LoadMesh(Mid$(inLine$,e+2,Len(inLine$)))
		EndIf
		If corrected = True And Instr(inLine$,"Ship.Size.x")
			e = Instr(inLine$,"=")
			this\Size\x = Mid$(inLine$,e+2,Len(inLine$))
		EndIf
		If corrected = True And Instr(inLine$,"Ship.Size.y")
			e = Instr(inLine$,"=")
			this\Size\y = Mid$(inLine$,e+2,Len(inLine$))
		EndIf
		If corrected = True And Instr(inLine$,"Ship.Size.z")
			e = Instr(inLine$,"=")
			this\Size\z = Mid$(inLine$,e+2,Len(inLine$))
		EndIf
		If corrected = True And Instr(inLine$,"Ship.Acceleration")
			e = Instr(inLine$,"=")
			this\Acceleration = Mid$(inLine$,e+2,Len(inLine$))
		EndIf
		If corrected = True And Instr(inLine$,"Ship.YRotate")
			e = Instr(inLine$,"=")
			RotateMesh this\Model,0, Mid$(inLine$,e+2,Len(inLine$)),0
		EndIf
	Wend
	CloseFile(in)
	If this\Size\x>0 And this\Size\y>0 And this\Size\z> 0
		FitMesh this\Model,(this\Size\x/2)*-1,(this\Size\y/2)*-1,(this\Size\z/2)*-1,this\Size\x,this\Size\y,this\Size\z
	EndIf
	Return this
End Function

Function PushCamera(this.Ship)
	v.Vector3 = New Vector3
	v\x = EntityX#(this\Model)
	v\y = EntityY#(this\Model)
	v\z = EntityZ#(this\Model)
End Function

Function PopCamera(this.Ship)
	v.Vector3 = First Vector3
	If v=Null Then Return
	PositionEntity Camera,v\x,v\y,v\z
	PointEntity Camera,this\Model
	Delete v
End Function

Function Distance#(this.Ship)
	Return Sqr#((EntityX#(this\Model)-EntityX#(Camera))*(EntityX#(this\Model)-EntityX#(Camera))+(EntityY#(this\Model)-EntityY#(Camera))*(EntityY#(this\Model)-EntityY#(Camera))+(EntityZ#(this\Model)-EntityZ#(Camera))*(EntityZ#(this\Model)-EntityZ#(Camera)))
End Function 
	
Function UpdateShip(this.Ship)

	TranslateEntity this\Model,this\Velocity\x,this\Velocity\y,this\Velocity\z
	If this\Velocity\y<1 Then this\Velocity\y = this\Velocity\y-0.001
	this\Velocity\z=this\Velocity\z-this\Velocity\z/30
	this\Velocity\y=this\Velocity\y-this\Velocity\y/30
	this\Velocity\x=this\Velocity\x-this\Velocity\x/30
	this\Rotation\x=this\Rotation\x-this\Rotation\x/5
	this\Rotation\z=this\Rotation\z-this\Rotation\z/10
	PushCamera(this)
End Function 


main code


Include "def.bb"

Global Camera
Initialize()
PositionEntity Camera, 0,10,-10
player.Ship = CreateShip("Ships\test.ship")
PositionMesh player\Model,0,MeshHeight#(player\Model)/2,0


ground = CreatePlane()
PositionEntity ground,0,-10,0

For i = 0 To 20
	c=CreateCube()
	ScaleEntity c,30,100,30
	t = LoadTexture("Textures\SkyScraper0"+Rand(1,7)+".jpg")
	EntityTexture c,t
	PositionEntity c,Rand(1000)-500,50,Rand(1000)-500
Next

While Not KeyDown(1)
	PointEntity Camera,player\Model
	If KeyDown(200) And player\Rotation\x<10 Then player\Rotation\x = player\Rotation\x+0.5
	If KeyDown(208) And player\Rotation\x>-10 Then player\Rotation\x = player\Rotation\x-0.5
	If KeyDown(203) And player\Rotation\z<10 Then player\Rotation\z= player\Rotation\z+0.5
	If KeyDown(205) And player\Rotation\z>-10 Then player\Rotation\z=player\Rotation\z-0.5
	If KeyDown(57) And Sqr#(player\Velocity\x*player\Velocity\x+player\Velocity\y*player\Velocity\y+player\Velocity\z*player\Velocity\z)<7
		player\Velocity\x = player\Velocity\x - Sin#(EntityYaw#(player\Model))*player\Acceleration
		player\Velocity\y = player\Velocity\y - Sin#(EntityPitch#(player\Model))*player\Acceleration
		player\Velocity\z = player\Velocity\z + Cos#(EntityYaw#(player\Model))*player\Acceleration
	EndIf
	If Distance#(player)>20 
		PopCamera(player)
	EndIf
	TurnEntity player\Model,player\Rotation\x,0,player\Rotation\z
	UpdateShip(player)
	RenderWorld()
	Flip()
Wend



Who was John Galt?(Posted 2003) [#16]
I'm getting a really stretched screen when I display this
thread now- did I do that?

Try having a separate type
Type campoint
field x#,y#,z#
end type

Use v.campoint for all your pusham/popcam routines- this avoids confusion of camera position list with any other vectors and should run fine. Another one to remember is that if you create a ship then delete it, it will not delete the vectors associated with it. Need to delete them separately before you delete the ship.


SSS(Posted 2003) [#17]
hi it worked really well.. dunno why, thanks alot... sorry bout the streched screen


Who was John Galt?(Posted 2003) [#18]
no worries!