Moving one of 200 cubes?

Blitz3D Forums/Blitz3D Beginners Area/Moving one of 200 cubes?

elcoo(Posted 2007) [#1]
Hi,
I've got a little example here, it creates 200 cubes and it could be even more:

Graphics3D 800,600
cam=CreateCamera()
PositionEntity cam,0,0,10
light=CreateLight()

For i=0 To 200
create_cube(i)
Next

While Not KeyHit(1)
TurnEntity cam,0,-0.1,0

RenderWorld()
Flip
Wend

Type Cube
Field cube_mesh%
End Type

Function create_cube(pos)
a.Cube= New Cube
a\cube_mesh=CreateCube()

PositionEntity a\cube_mesh,10,0,pos*5
End Function

Pretty simple actually. But how can I move or turn etc. for example the seventh cube out of these 200?


GfK(Posted 2007) [#2]
Create an array to use as an index.

Or use For/Each to count through to the 7th cube, but that's potentially much slower.

I can't remember how to do type/array stuff in Blitz3D though.


Boiled Sweets(Posted 2007) [#3]
this will also create 200 surfaces, might not be a problem but it's not all that efficient.


stayne(Posted 2007) [#4]
I think brackets should be used here (arrays)? Something like (not at Blitz atm, this will probably fail miserably and is more than likely inefficient)...

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

Type Cube
  Field Cube_Mesh[200]
End Type

Create_Cube()

;Main Loop
While Not KeyHit(1)
  TurnEntity a\Cube_Mesh[3],0,2,0
  TurnEntity a\Cube_Mesh[27],0,4,0
  TurnEntity a\Cube_Mesh[150],0,0,2
  RenderWorld()
  Flip
Wend
End
;End Main Loop

Function Create_Cube()
  MasterCube = CreateCube()
  CubeCopy = CopyEntity MasterCube
  pos = 1
    For i = 0 to 199
      a.Cube = New Cube
      a\Cube_Mesh[i]=CubeCopy
      PositionEntity a\Cube_Mesh[i],10,0,pos*5
      pos = pos + 1
    Next
  HideEntity MasterCube
End Function



Dreamora(Posted 2007) [#5]
Nope that is actually correct.

BlitzArray (fields) use [] while global arrays declared by Dim use ().

But the indices are wrong. Its 0 to 199 actually, blitzarrays are 0 indexed with #elements = size


stayne(Posted 2007) [#6]
Fixed, thanks :)


Ross C(Posted 2007) [#7]
I don't think you'd really need to do this in real terms. Usually games revolves around you colliding, or picking an entity, and those both commands return the entity involved :o)


elcoo(Posted 2007) [#8]
Er well, for me it doesnt work :(


Ross C(Posted 2007) [#9]
Basically, you:


c.cube = first cube

counter = 0
repeat

counter = counter + 1
c = after c
if c.cube = null then runtimeerror("Does not exist")

until counter = 7



That will loop through until it gets to the 7th cube.


jfk EO-11110(Posted 2007) [#10]
what's wrong with DIm arrays? I mean, searching for the cube that has index 7...somewhat inefficient.
Graphics3D 800,600
cam=CreateCamera()
PositionEntity cam,0,0,10
light=CreateLight()

dim cube(199)
For i=0 To 199
 cube=createcube()
 positionentity cube(i),10,0,i*5
Next

While Not KeyHit(1)
 turnentity cube(7),0,.1,0
 RenderWorld()
 Flip
Wend


so...

cube(7)

replaces:

c.cube = first cube
counter = 0
repeat
counter = counter + 1
c = after c
if c.cube = null then runtimeerror("Does not exist")
until counter = 7


elcoo(Posted 2007) [#11]
I used Dim, but I want to create the cubes in a Function, so that i can create Cubes in while i'm playing. And when i create cubes with a function and Dim, i can't move the cubes.


IPete2(Posted 2007) [#12]
Why do you want to move or rotate the 7th cube?

Sorry if this seems like a dumb question, but if you want to select individual cubes to move/rotate under certain circumstances and as a reaction to something, in code, then using the infamous HANDLE and OBJECT commands would be a good way to go.

I'm going to try to explain this using your original a\cube example...

Your type is a\cube, so as you set up your cubes for the first time, use something like this:

NameEntity a\cube,Handle(a)  

This will store the handle for every instance of your cube 0 - 200 or whatever and by using the OBJECT command in your later code, you can just select any cube for instant retrieval later.

Let's say that you have an entity moving round about these cubes, as though the cubes were formatted in a maze shape or something. You want any cube to react when your character collides with it, and you want to do this efficiently so you can have many cubes all ready to react.

You would need to set up a collision type for the cubes and one for your player.

;for cubes lets say
global collision_cubes = 1
entitytype collision_cubes

;and for your player
global collision_player = 2
entitytype collision_player

;next ensure the collisions are activated for checking against each other and set up the reaction for a collision.

Collisions collision_player ,  collision_cubes,1,2 


(check out each of these commands in the Blitz docs for more info).

Then in your main game loop check your player for a collision with the cube collision type by setting up a variable for collisions let's call it 'collidedwiththewalls' for now...
collidedwiththewalls = EntityCollided (the_entity_name_of_your_player,collision_cubes) 

NOW the clever bit, to find out exactly which cube was collided with by your player entity use the OBJECT command in the following manner.
a.cube = Object.cube(EntityName(collidedwiththewalls)) 

Now you just do this...
RotateEntity a\cube_mesh ,0,1,0

This is a tricky set of commands to get into, but it is a phenomenally superior way to handle (excuse the pun) large groups of entity's on an individual and instant basis.

Hope that helps you think of how you can control individual entitys in a large group.

Merry Christmas

IPete2.


stayne(Posted 2007) [#13]
This is starting to remind me of something...

http://en.wikipedia.org/wiki/Cube_series