Notifications
Clear all

[Closed] Move in opposite direction

I simply have two objects in a scene and I want object A to move in the opposite direction of B. So if you were to draw a straight line from A.center to B.center and then move object A in the opposite direction along that path.


delete objects
A = teapot radius:26 pos:[-7,11,0]
B = teapot radius:26 pos:[7,-11,0]

offset = units.decodevalue "1m" 

if intersects A B do (
	--move object A in the opposite direction of B by amount 'offset'
	--move object B in the opposite direction of A by amount 'offset'
)

12 Replies

How do I then apply the offset to move the object in the direction opposite of the other object?


delete objects
A = teapot radius:26 pos:[-7,11,0]
B = teapot radius:26 pos:[7,-11,10]

point pos:A.pos
point pos:B.pos

offset = units.decodevalue "1m" 

if intersects A B do (
	p3 = (B.center - A.center)*2
	A.pos += p3 --move object A in the opposite direction of B by amount 'offset'
	B.pos += -p3 --move object B in the opposite direction of A by amount 'offset'
)

do you want to move objects out of their average center?

2 Replies
(@jokermartini)
Joined: 11 months ago

Posts: 0

Yes that is correct.

(@denist)
Joined: 11 months ago

Posts: 0

you already solved it but because i wrote the function anyway:


fn moveThemOut nodes: offset:1 = 
(
	if nodes == unsupplied do nodes = selection as array
	center = [0,0,0]
	for node in nodes do center += node.transform.pos
	center /= nodes.count
	for node in nodes do
	(
		dir = normalize (node.transform.pos - center)
		move node (dir*offset)
	)
)


delete objects
A = teapot radius:26 pos:[-7,11,0]
B = teapot radius:26 pos:[7,-11,10]

point pos:A.pos wirecolor:red
point pos:B.pos wirecolor:red

offset = units.decodevalue "1.0m" 

if intersects A B do (
	p3 = (B.center - A.center)
	p3= normalize p3
	
	m = A.pos + (offset * p3)
	A.pos = -m
	B.pos = m
)

I’m trying to make a script which takes a selection of objects and moves them in an outward direction away from any object they may be intersecting with.

The below script works but the method of seperating the objects based of the vector created by it’s intersecting object is more ideal.


/* Scene Setup */
delete objects
for c = 0 to 2 do (
	for i = 0 to 2 do	(
		for r = 0 to 2 do	(
			boxSize = 10
			local thisBox = box height:10 width:10 length:10 pos:[i*8,(r*8),(c*8)] wirecolor:(random white black)
		)
	)
)

fn isNodeIntersecting obj objArray = (
	testNodes = for itm in objArray where itm != obj collect itm
		
	for itm in testNodes do
	(
		if (intersects obj itm) do return false
	)
	true
)

curSel = objects as array

for o in curSel do
(
	conflict = isNodeIntersecting o curSel
	
	while conflict != true do
	( 
		move o [10,10,10]
		conflict = isNodeIntersecting o curSel
	)
)

the way that you are doing it is a ‘ping-pong’. you move one object away from some other but it might make it intersected with the third one, and have to move it again, and again, and again… the question is… what direction has it to be.

there is another way. make 3D virtual grid buffer. check what cells are taken by nodes. find intersected nodes. and move one of them to closest empty cell. for the same size objects it has to work fast and well.

This is an older method I was using. I figured implementing the movement of each object to be in the opposite direction of it’s intersector would be better. From what you’re saying these is a bit more to it than just that.


/* Scene Setup */
delete objects
for c = 0 to 2 do (
	for i = 0 to 2 do	(
		for r = 0 to 2 do	(
			boxSize = 10
			local thisBox = box height:10 width:10 length:10 pos:[i*8,(r*8),(c*8)] wirecolor:(random white black)
		)
	)
)

fn isNodeIntersecting obj objArray = (
	testNodes = for itm in objArray where itm != obj collect itm
		
	for itm in testNodes do
	(
		if (intersects obj itm) do return false
	)
	true
)

curSel = objects as array

for o in curSel do
(
	conflict = isNodeIntersecting o curSel
	
	while conflict != true do
	( 
		move o [random -5 5,random -5 5,random -5 5]
		conflict = isNodeIntersecting o curSel
	)
)

i still think that your method may cause the infinite loop.

i still think that your method may cause infinite loop.

i still think that your method may cause infinite loop.

Page 1 / 2