WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 5.00 average. Display Modes
  #1  
Old 09-10-2008, 12:12 PM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
real time collision on animation path

Hello,

I'd like to simulate some cars running on the road without crash. Cars are animated by animation path and those paths have intersection. I use .collideBox() and .enable(viz.COLLIDE_NOTIFY) for each car for collision. But I don't exactly know how to handle collision event in the callback function. Because once two cars are about to collide, I need one car to speed up and the other to slow down. But my program have no idea which car should speed up and which car should slow down. Does anybody have a solution for that?

Thanks,
whj
Reply With Quote
  #2  
Old 09-11-2008, 05:37 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Thanks for the post. Sorry, we have not had time to answer yet. We will respond tomorrow or early next week.
Reply With Quote
  #3  
Old 09-15-2008, 02:01 PM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
Unhappy

Hi,

After thinking it over, I feel I should not use collision in this case. Because I need to prevent crash instead of allowing collision. Is there any way I can calculate the position after a certain time, e.g. 2 seconds, when a car moving along the animation path according to its speed?

Sorry about confusion. And thanks for your help in advance.
Reply With Quote
  #4  
Old 09-15-2008, 05:02 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
you could create a callback function that is called every time a timer expires. Within that function you could get the position of your cars and calculate the distance between them. If they are to close you could speed up the one in front and slow down the one behind.

Code:
import vizmat

#start a timer that will call onTimer() every 2 seconds
viz.starttimer(0,2,viz.FOREVER)

def onTimer(num):
	

	car1_Pos = car1.getPosition()
	car2_Pos = car2.getPosition()
	distance = vizmat.Distance(car1_Pos, car2_Pos)
	
	if distance is too close:
	
		#increase speed of path of car1
		#decrease speed of path of car2
		
		
		
viz.callback(viz.TIMER_EVENT,onTimer)

is that helpful?
Reply With Quote
  #5  
Old 09-20-2008, 11:34 AM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
Sorry I didn't make it clear. I need to know the position in advance, like where the car could be in the next 2 seconds. So I can predict if they would crash. (I know your sample code is a way to predict crash based on distance. But my research need a way to predict crash based on time.) Since all cars have their defined paths and speed using animation path, I'm wondering if I can get the expected position beforehand? I really appreciate your help.
Reply With Quote
  #6  
Old 09-22-2008, 03:26 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
You could try creating another animation path with identical control points. You can delay playing the second path by two seconds. The second path would have your car linked to it. The first path would have an empty group node linked to it. The group node, which is not visible, would always have the position two seconds ahead of your car. Just get its position anytime you need to make your calculation. If however you change the speed of your car's animation path, you will need to do the same for your group nodes path, so they will always be two seconds apart.

Here is some code with a semi-transparent ball two seconds ahead of another ball. You could replace the semi-transparent ball with an empty group node that just holds the position

Code:
import viz
viz.go()

viz.add('tut_ground.wrl')

#add a ball and make semi-transparent,  this will be 2 seconds ahead of 
#ball2
ball = viz.add('ball.wrl')
ball.alpha(.2)
ball2 = viz.add('ball.wrl')

#Move the viewpoint back
viz.MainView.move(0,0,-8)

#Create the animation paths
path = viz.add(viz.ANIMATION_PATH)
path2 = viz.add(viz.ANIMATION_PATH)

#Initialize an array of control points
positions = [ [0,0,2], [2,0,0], [0,0,-2], [-2,0,0] ]

for x in range(0,len(positions)):
    
    cp = viz.add(viz.CONTROL_POINT)
    cp.setPosition(positions[x])
    path.add(cp,x+1)
    path2.add(cp,x+1)

#Set the initial loop mode to circular
path.loop(viz.CIRCULAR)
path2.loop(viz.CIRCULAR)

#Automatically compute tangent vectors for cubic bezier translations
path.computeTangents()
path2.computetangents()

#Automatically rotate the path
path.setAutoRotate(viz.ON)
path2.setAutoRotate(viz.ON)

#Link ball and ball2 to their paths
ball.link(path)
ball2.link(path2)


#start playing the first animation path immediately
path.play()

#start playing the second animation path after two seconds
vizact.ontimer2(2, 1, path2.play)
Reply With Quote
  #7  
Old 09-26-2008, 03:45 PM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
Hi, Jeff

That's really helpful! Thanks a lot!

I have another question. Is there any way I can get the current speed of the object, like accessing some environment variable? The reason I need it is because when I accelerate cars I need set the new speed based on the current speed.
Reply With Quote
  #8  
Old 09-29-2008, 11:45 AM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Would it work for you to use animationpath.constantspeed when you begin. Then you would know the initial speed and you could change based on that.
Reply With Quote
  #9  
Old 10-01-2008, 11:49 PM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
I do use animationpath.constantspeed when I begin. But what I need to know is not only the initial speed. If I want to accelerate or decelerate a car, I would like to increase or decrease the car based on not the initial speed, but the speed when last timer event triggered.

For example, the initial speed is 20mi/hr. To avoid clash, I calculate certain distance every 20 times a second. While the distance is too close, one car keep speeding up 20 percent and the other car keep slowing down 20 percent. So one car could speed up to
20 + 20*0.2 = 24mi/hr,
24 + 24*0.2 = 28.2mi/hr, (I need to know the current speed is 24)
28.2 + 28.2 *0.2 = 34.56mi/hr, (I need to know the current speed is 28.2)
and so on; and the other car could slow down to
20 - 20*0.2 = 16mi/hr,
16 - 16*0.2 = 12.8mi/hr, (I need to know the current speed is 16)
12.8 - 12.8*0.2 = 10.24mi/hr, (I need to know the current speed is 12.8)
and so on.

Sorry about the trivial details. Do you think it is feasible? Thanks a lot!
Reply With Quote
  #10  
Old 10-02-2008, 01:16 PM
Jeff Jeff is offline
WorldViz Team Member
 
Join Date: Aug 2008
Posts: 2,471
Let me know if I am not understanding this correctly, but couldn't you have a variable for each car that stores its current speed. When you need to accelerate or decelerate just access that variable and change it. Then set the animation path to the new carspeed.
Reply With Quote
  #11  
Old 10-06-2008, 05:38 PM
whj whj is offline
Member
 
Join Date: Apr 2008
Posts: 60
Yes, that's exactly what I want. If there is a system defined variable about current speed, something like get.speed(), then I don't need to maintain them by myself. That's why I'm asking. But if not, I will do what you said.

Thanks a lot.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Real Time Audio Data lomendil Vizard 2 06-02-2008 09:18 PM
Can I get real time Intersense tracking data from another computer on the network? GoldenSun Vizard 4 04-30-2008 08:42 PM
timer question Elittdogg Vizard 5 10-10-2007 03:49 PM
Animation Path djdesmangles Vizard 2 06-11-2007 04:37 PM
Animation Path djdesmangles Vizard 0 06-06-2007 12:03 PM


All times are GMT -7. The time now is 04:22 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC