PDA

View Full Version : Updating object color


sxu04
12-20-2011, 05:21 AM
I’m really hoping that someone could kindly steer me in the right direction……

I have 12 balls positioned in an environment and (using the ‘Experiment’ example in the Vizard Help) these balls only show their colour when the subject is near them. On approach, the balls will either turn red or green based on a predetermined sequence. However, in addition I need the red balls to have a 1 in 10 chance of turning orange and for this to update every 3 seconds. I am however having issues updating the colour of the balls once the colour has been assigned.

Any help would be very much appreciated!


a1=-37.5
a2=-12.5
a3=12.5
a4=37.5
x=2.75
y=3.85
z=1.1
b1=34.5
b2=16.5
b3=8.5
b4=-8.5
b5=-16.5
b6=-34.5

positions = [[a1,x,b1],[a1,x,b2],[a1,x,b3],[a1,x,b4],[a1,x,b5],[a1,x,b6],[a1,z,b1],[a1,z,b2],[a1,z,b3],[a1,z,b4],[a1,z,b5],[a1,z,b6]]

R=viz.RED
G=viz.GREEN

colors=[R,R,R,R,R,R,G,G,G,G,G,G]


balls=[]

def col():
while True:
yield viztask.waitTime(1)

for i in range(0,12):
model=vizshape.addSphere(1,10,10)
model.setPosition(positions[i])
ball = viz.Data()
ball.model = model

x=random.randint(1,10)

if x == 1:
if colors[i]==R:
#ball.model.clearAttribute(viz.ATTR_COLOR)
#model=vizshape.addSphere(1,10,10)
#model.setPosition(positions[i])
#model.setScale(1.1,1.1,1.1)

ball.color=(viz.ORANGE)

ball.colorShowing = False


balls.append(ball)
#print 'red'
if colors[i]==G:


ball.color=(colors[i])
ball.colorShowing = False
balls.append(ball)


else:
ball.color=(colors[i])
#print 'green'
ball.colorShowing = False
balls.append(ball)


viztask.schedule( col() )

def changeColor():
viewPos = viz.MainView.getPosition()
for ball in balls:
ballPos = ball.model.getPosition()
distance = vizmat.Distance(viewPos, ballPos)

if ball.colorShowing is False:
if distance < 11 :
showColor=vizact.sequence(vizact.waittime(0.1),viz act.fadeTo(ball.color,time=0.7))
ball.model.runAction(showColor)
ball.colorShowing = True
else:
if distance > 11 :
showWhite = vizact.fadeTo(viz.WHITE,time=4)
ball.model.runAction(showWhite)
ball.colorShowing = False

vizact.ontimer(0,changeColor)

Jeff
12-20-2011, 10:04 AM
Please use the code tags when posting code so the indentation is preserved.

sxu04
12-21-2011, 01:27 AM
Sorry this is the first time I've posted on a forum


a1=-37.5
a2=-12.5
a3=12.5
a4=37.5
x=2.75
y=3.85
z=1.1
b1=34.5
b2=16.5
b3=8.5
b4=-8.5
b5=-16.5
b6=-34.5

positions = [[a1,x,b1],[a1,x,b2],[a1,x,b3],[a1,x,b4],[a1,x,b5],[a1,x,b6],[a1,z,b1],[a1,z,b2],[a1,z,b3],[a1,z,b4],[a1,z,b5],[a1,z,b6]]

R=viz.RED
G=viz.GREEN

colors=[R,R,R,R,R,R,G,G,G,G,G,G]

balls=[]

def col():
while True:
yield viztask.waitTime(3)

for i in range(0,12):
model=vizshape.addSphere(1,10,10)
model.setPosition(positions[i])
ball = viz.Data()
ball.model = model

x=random.randint(1,10)

if x == 1:

if colors[i]==R:

ball.color=(viz.ORANGE)

if colors[i]==G:
ball.color=(colors[i])
else:

ball.color=(colors[i])

ball.colorShowing = False
balls.append(ball)



viztask.schedule( col() )

def changeColor():
viewPos = viz.MainView.getPosition()
for ball in balls:
ballPos = ball.model.getPosition()
distance = vizmat.Distance(viewPos, ballPos)

if ball.colorShowing is False:
if distance < 10 :
showColor=vizact.sequence(vizact.waittime(0.1),viz act.fadeTo(ball.color,time=0.7))
ball.model.runAction(showColor)
ball.colorShowing = True
else:
if distance > 10 :
showWhite = vizact.fadeTo(viz.WHITE,time=4)
ball.model.runAction(showWhite)
ball.colorShowing = False

vizact.ontimer(0,changeColor)

Jeff
12-21-2011, 02:33 PM
Each time the code in the while loop is run you're adding balls in the positions where old ones already are. I think the issue you see is related to that. Try adding the shapes once outside of the while loop and just update their colors within.

sxu04
01-04-2012, 06:47 AM
Great, thanks for your help