Memory Violation - EntityY(ball) ?

Blitz3D Forums/Blitz3D Beginners Area/Memory Violation - EntityY(ball) ?

Captain Wicker (crazy hillbilly)(Posted 2012) [#1]
[bbcode]AppTitle("Ponga3D - Demo")

Graphics3D 800,600,32,2
SetBuffer BackBuffer()

Const PLYR_COLL=1
Const ENOP_COLL=2
Const BALL_COLL=3


Global light=CreateLight()

Global camera=CreateCamera()

Global plyr=CreateCube()
EntityColor plyr,0,0,255
ScaleEntity plyr,1,.25,.25
PositionEntity plyr,0,-2.8,5
EntityType plyr,PLYR_COLL

Global enop=CreateCube()
EntityColor enop,255,0,0
ScaleEntity enop,1,.25,.25
PositionEntity enop,0,2.8,5
EntityType enop,ENOP_COLL

Global ball=CreateSphere(32)
PositionEntity ball,0,0,5
ScaleEntity ball,.25,.25,.25
EntityType ball,BALL_COLL

Repeat

Collisions(BALL_COLL,PLYR_COLL,2,2)
Collisions(PLYR_COLL,BALL_COLL,2,2)
Collisions(BALL_COLL,ENOP_COLL,2,2)
Collisions(ENOP_COLL,BALL_COLL,2,2)

Dynamic_Game()
Dynamic_Ball()


UpdateWorld
RenderWorld
Flip( True ) : Flip(-1)

Until KeyDown(1)



Function Dynamic_Game()


If KeyDown(203)
MoveEntity plyr,-.25,0,0
ElseIf KeyDown(205)
MoveEntity plyr,.25,0,0
EndIf


End Function


Function Dynamic_Ball()

MoveEntity ball,Rand(-1.0,1.0),-1.25,0

If EntityCollided(ball,PLYR_COLL) Or EntityCollided(plyr,BALL_COLL)
EntityY(ball) = EntityY(ball) + -EntityY(ball)
ElseIf EntityCollided(ball,ENOP_COLL)
EntityY(ball) = EntityY(ball) + EntityY(ball)
EndIf


End Function
[/bbcode]
What am I doing wrong to cause a Memory Access Violation?

Last edited 2012


Adam Novagen(Posted 2012) [#2]
Easy. You're making a common mistake in the transition from 2D to 3D programming in Blitz. When you're dealing with a 2D image, you track its X and Y like this:
ball\x = ball\x + ball\xSpeed
ball\y = ball\y + ball\ySpeed

DrawImage ball\image,ball\x,ball\y

In 3D programming, once you've created an entity, Blitz tracks its coordinates for you. EntityX(), EntityY() and EntityZ() don't actually do anything, they only return the X, Y and Z coord of the entity, respectively. So your line:
EntityY(ball) = EntityY(ball) + -EntityY(ball)

is totally invalid. Blitz doesn't know what to do with this since EntityY() can't be used in that way, and the program crashes. You'll just need to replace those two lines with more MoveEntity() calls, and voila!

Last edited 2012


Captain Wicker (crazy hillbilly)(Posted 2012) [#3]
I guess ive gotten so used to 2d programming that 3d seems almost impossible for me now :D