Adding objects with linearVelocities, won't accept integer values as integers?

so, I’m trying to make a small lil sim of particles zipping and bouncing around. I’ve an enclosed space, where the camera is at one end and particles (protons, neutrons, electrons) will bounce around and interact. but for this of course, I’ll need a fair number of these particles, preferably placed randomly, with random velocities in each direction.

the best way I can see to do this is have empties spawn them in randomly, just beyond the cameras view, and have them with their random velocities and directions, move into view. I’m hitting an issue however with giving them velocities once their added in. specifically, I’m getting an
“list indices must be integers, or slices, not tuple” error.

I’ve tried everything I can imagine to fix it, and been looking up the issue for a couple hours now with no luck. here is my code:

import bge
import mathutils
import random

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner

#actuators --------------------------

Add_elec = cont.actuators['Add elec']
Add_neut = cont.actuators['Add neut']
Add_pro  = cont.actuators['Add pro']

#empty ------------------------------

for ob in own.children:
    if 'kid' in ob:
        child = ob

#propertys --------------------------

Type = own['Sp Ty']
Time = own['Time Elapsed']

#do stuff ---------------------------

Time += 60 

Vco = 3
Vch = random.randint(0,1)

Vx = 0
Vy = 0
Vz = 0

if Vch == 1:    #chance to set x velocity or leave 0
    Vx = random.randint(-5,5)
else: Vco += -1

Vch = random.randint(0,1)

if Vch == 1:
    Vy = random.randint(-5,5)
else: Vco += -1

Vch = random.randint(0,1)

if Vch == 1:
    Vz = random.randint(-5,5)
         Vco += -1

if Vco == 0:       #if all velocities are 0, make one not 0
    Vch = random.randint(1,3)
    if Vch == 1:
        Vx = random.randint(-5,5)
    elif Vch == 2:
        Vy = random.randint(-5,5)
        Vz = random.randint(-5,5)

#based on output of random actuator, stored in property 'Type', determine what to spawn.

if Type == -1:
elif Type == 0:

would seem to be the problem lines. but “Vx, Vy, and Vz” are each single digit INTEGERS between -5 and 5. (even when setting them all to 0, or 1, the same error occurs.)

the initial linear velocity of added objects.

list [vx, vy, vz] 

youre doing it wrong.

obj.worldLinearVelocity = [x,y,z] #this is a variable attr
obj.worldLinearVelocity[0] = x #only modify the X value

obj.setLinearVelocity([x,y,z], False) #this is a function, where "False" means if its in local coords
1 Like

THANK YOU! :smiley: (I thought I’d tried that? guess not…)

now if only I knew why the objects aren’t moving after they’re created. I’ve currently got only 1 empty spawning them infront of the camera, so I can see them being added but not going anywhere. I’ve added print statements so I can see which one is added, and what the linearVelocity is of the addObject actuator that’s adding them.

will I have to just instantAdd them and then set the Velocity to the objects directly?

use newobj = scene.addObject("thing", spawner, 0) instead of a logic brick, its much nicer to work with.

also, you need a loop to continually update the velocity, im not sure how well the physics engine could handle such chaos. what you might need to do is raycast and manually calculate reflection vectors and move the objects along the ray paths. this sound like a job for @BluePrintRandom! que heroic music

setting damping to 0 and 0 will prevent the objects from slowing down due to faked air friction

1 Like