2
$\begingroup$

I was trying to create the illusion of a building being destroyed as soon as there is a collision using python. Everything seems to work fine when I use a cube, but it breaks as soon as I use the real building

I have attached a simple version of the problem which illustrates both the cube and the building. Whenever the "player" cube collides with the red cube, the addobject works perfectly. The same thing done with a building produces weird results. (Note they have the exact same settings and they both use the same python module)

Can someone please help me resolve this mystery. I have been cracking my head for ever. Here's the blend file (Use the arrow keys to move the player cube and collide it with different objects) https://www.dropbox.com/s/q3gajcxasv1c2dt/break_demo.blend?dl=0

ANSWER:

If anybody is stuck with a similar problem, then look into scaling. Here's my final code to get it done without using any empty's and rescaling in fly

def decimate(cont):
    self = cont.owner
    scene = self.scene
    self.scaling = [1,1,1]
    scene.addObject(placer_ID, self)
    self.endObject()

Also if it seems that the object gets added, but at the wrong location. Then it is due to the fact that the origin is not set. Try to set the origin to center of geometry or center of mass (Ctrl + Shift + Alt + C). You have to set this by selecting the empty associated with the group. Look at this question for more details Replace objects with a group of objects using Python

$\endgroup$
  • 3
    $\begingroup$ Please show your code separately, and preferably use a simple-as-possible blend. Also, some screenshots couldn't hurt. Help us help you :) $\endgroup$ – Greg Zaal Oct 28 '14 at 6:26
  • $\begingroup$ Do you want to do it with code? There are other ways of doing it , Like , use the fracture tool , And fracture your object . Then when you dragon is colliding use a rigidbody on the object , then depending on the speed it will break and look realistic $\endgroup$ – Knuckles209cp Oct 28 '14 at 9:08
  • $\begingroup$ Yes a code is preferred cause I have like 90 objects and would end up adding different constraints $\endgroup$ – chettyharish Oct 29 '14 at 5:54
  • 1
    $\begingroup$ I made a simple code example which still illustrates the point $\endgroup$ – chettyharish Nov 2 '14 at 4:54
3
+50
$\begingroup$

There is one notable difference between the cube and the building -> The cube has a unit scale (1, 1, 1), but the building is scaled down. Since spawned objects adopt the local scale of the spawner, the behavior you observe, while not desired, is perfectly normal.

To get desired behavior, you need to specify a spawner object (typically an empty) with a unit scale, and use that to spawn the relevant group.

So, the script would now look like this:

def decimate(cont):
    self = cont.owner
    scene = self.scene
    spawner = scene.objects["Spawner"]
    spawner.worldPosition = self.worldPosition
    spawner.worldOrientation = self.worldOrientation
    scene.addObject("cube_parts", spawner)
    self.endObject()

Demo: break_demo_spawner.blend

$\endgroup$
  • 1
    $\begingroup$ Thanks a lot, I never looked at scaling! I did not want to add spawner's as that would multiply my amount of work, so I just rescaled the building before adding the group. $\endgroup$ – chettyharish Nov 3 '14 at 6:29
  • $\begingroup$ @chettyharish Good thinking. $\endgroup$ – Goran Milovanovic Nov 3 '14 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.