I'm new with pymunk and I would like to implement a n-body simulation (in 2D) like this one: https://www.youtube.com/watch?v=otIGNTFJwpU&feature=youtu.be
I already know how to create the space, shapes and the rendering with pygame. My question: is there a way to add gravitation forces between shapes in pymunk?
Thanks for your help.
There is no built in way to do it directly in pymunk. The easiest is probably to calculate it yourself. Loop the bodies and for each body make a space query to find nearby bodies. Calculate the resulting gravity and use apply impulse.
(Possibly its possible to emualte with one of the constraints, for example the pin joint or damped spring.. but that will require some thinking and experimenting to find out if its possible and looks good)
Related
I'm trying to build a simulation that will take place in a 1000x1000x1000 space. For each point in space, I need to be able to encode 2 or 3 properties.
I also need to be able to do some basic operations on the space, such as, given a point, find the properties of the 26 adjacent 3D neighbors of the point.
All points will lie on vertices in the 1000x1000x1000 space (i.e. every point is discrete).
I wrote up a version in python using numpy and it is much too slow. I've also looked for libraries that would speed it up but couldn't find anything.
Does anyone know of a python library that would provide useful helper functions and would be appropriate for a simulation of this size?
Using Numpy to together with the Numba python-compiler for the more intricate algorithms can take you a long way.
Also, I think you are refering to a "stencil" algorithm, and numba has specific stencil-functionality that could help you.
But start with a smaller grid during development!
I'm trying to import an animation from a file to Maya, but it gives me odd results between the interpolations:
https://i.imgur.com/cP27Yai.mp4
It was weird because they keyframes looked right at first until i looked at the graph editor.
thought this was at first a gimbal lock, so i used the Euler Filter, but it gave no solution to it. Sometimes, the difference between one key and another is 180, which is why, by just seeing the animation, the keys look fine, but the interpolation makes it do a 180 rotation.
So if i go one by one, and subtract the vaule of the key by 180, and then invert the number (to positive or negative depending on the case), i can make it work by tweaking it a bit.
However this is too much work, specially for being biped animations, it could took me forever.
Is this a common issue or something that happened before to anyone else? Is there any way to fix this? Maybe it's the way i'm applying the euler angles, since they were initially Quaternions, but i did not find a way to apply the quaternions directly:
#Taking a rotation from the QUATERNION Array and converting it to Euler.
arot = anim.AnimRot[index].normal().asEulerRotation()
frot = MEulerRotation(arot.x*180/math.pi, arot.y*180/math.pi, arot.z*180/math.pi)
cmds.setAttr((obj + ".rotate"), frot.x, frot.y, frot.z)
cmds.setKeyframe(obj, time=anim.TotalKeys[i])
Is there any way to fix this from the editor or the script? any that fixes it would do me a great favor for importing this biped animation. I believe this is due to euler conversion, but i found no way to apply a quaternion to a bone in the maya API.
If the rotations already are quaternions, you might want to simply set the anim curves to quaternion interpolation using something like
cmds.rotationInterpolation( 'pSphere2.rotateX', 'pSphere2.rotateY', 'pSphere2.rotateZ', c="quaternionSquad")
To be safe I'd set one key, then apply the rotationInterpolation to change the keys to quats, then step through applying your original quaternions. Since you're already in API land you can make an MTransformationMatrix and use its setRotationComponents method to set the quat values so you don't ever convert to eulers.
I'm planning to generate a flower in Maya2016 where the leafs are not flexible, but they are built up by 4-5 segments connected to each other. I already have the python script which gets a segment as an input parameter and generates the leaf. (And also generates multiple leafs in a circle.)
Now the question is, how to animate it. I'm new in maya animation. I've found a few example how to do basic things like rotation or transformation with python. But what I wish to have is a realistic movement where the segments are moving how they should be.
My idea is to generate a "skeleton" bone and joint point for every segment. And then I wish to animate the skeleton.
Do you think it's a good way of animating the leaf?
How can I set up the skeleton?
How can I animate a skeleton?
(For the last two, I expect a good source like a blogpost, but I also wish to know if my entire concept is good or not.)
Thanks!
I'm using turtle and inserting 2 shapes into the program and I am trying to make the program perform a specific function when the objects intersect. Is it possible to do it with an if statement?
I suspect that the proper answer is "no". Turtle graphics does not maintain the shape in a form useful for you to test, nor does it provide shape manipulation methods.
You could develop your own package to represent objects, and include an intersection method, but this takes a lot of work. If you're interested, see the BOOST library shape methods (that's in C++) that Luke Simonson did (2009, I think).
However, if your shapes are regular enough, you can make a proximity detector. For instance, if the shapes are more or less circular, you could simply see whether they've come within r1 + r2 of each other (a simple distance function on their current positions), where r1 & r2 are the radii of the objects. Is that close enough for your purposes?
I'm writing a program in Python. I have a series of shapes (polygons, defined as a sequence of coordinate pairs) and I need to tell if they overlap a particular rectangle.
Is there an easy algorithm for handling this? Or, better, is there a pure Python library that can handle these calculations for me?
Presuming your "arbitrary shapes" are indeed polygons (given that they're described as coordinate pairs), determining if they overlap (in any language) is a relatively trivial calculation. You merely need to compute if any side of polygon A intersects any other side of polygon B.
If you need an example, there's a rather thorough walkthrough at the Drexel Math Forum.
There are a number of Python modules which can assist you in this pursuit, such as Sympy, Numpy, PyGame, etc., but all of them are rather heavy if this is the only geometric calculation you need to make.