Pymunk body's shapes don't rotate when I rotate the body - pymunk

In Pymunk, when I rotate a body, its shapes are not rotating. When I apply an impulse, the shapes move in sync, as expected. My google searches indicate that the body's shapes should be rotating when the body rotates. Am I fundamentally misunderstanding rotation?
Here is the relevant rotation code:
def selectEntity(self, location):
shapes = self.space.point_query(location)
bodies = set()
for shape in shapes:
bodies.add(shape.body)
for body in bodies:
body.angle += 1.57079633 # + 90 degrees
Here is the initialization code:
def _addShip(self, mass, center, angle = 0.):
radius = 10
groupId = self.getNextBodyId() # shapes in the same group do not generate collisions
body = pymunk.Body(mass, pymunk.moment_for_circle(mass, radius / 10, radius)) # mass, inner radius, outer radius, offset
body.angle = angle
partOne = pymunk.Circle(body, radius, center)
partOne.group = groupId
partOne.color = THECOLORS['blue']
partOne.friction = .8
partTwo = pymunk.Circle(body, radius, (center[0], center[1] + 20))
partTwo.group = groupId
partTwo.color = THECOLORS['blue']
partTwo.friction = .8
ship = (partOne, partTwo, body)
self.space.add(*ship)

The collision tree/hash is not updated immediately, you need to step the space forward first. Another way to update the collision data is to call Space.reindex_shape(shape_that_has_been_moved).
See this Example:
>>> from pymunk import *
>>> s = Space()
>>> b = Body(1,1)
>>> c1 = Circle(b, 10, (-10,0))
>>> c2 = Circle(b, 10, (10,0))
>>> s.add(b,c1,c2)
>>> s.step(.1)
>>> s.point_query((-15,0))
[<pymunk.Circle object at 0x02264690>]
>>> b.angle
0.0
>>> b.angle = 1.57
>>> s.point_query((-15,0))
[<pymunk.Circle object at 0x02264690>]
>>> s.step(.1)
>>> s.point_query((-15,0))
[]
>>> s.point_query((0,-15))
[<pymunk.Circle object at 0x02264690>]
>>> b.angle = 0
>>> s.point_query((-15,0))
[]
>>> s.reindex_shape(c1)
>>> s.reindex_shape(c2)
>>> s.point_query((-15,0))
[<pymunk.Circle object at 0x02264690>]

Related

Pymunk dynamic object overlapping static object

I am starting out with pymunk and have a static floor with a dynamic object (ball). When the ball falls onto the floor there is an initial overlap where the ball is absorbed into the floor before slowly being pushed back up.
Why does this happen? Is this normal?
import pygame
import pymunk
import pymunk.pygame_util
pygame.init()
#game window
SCREEN_WIDTH, SCREEN_HEIGHT = 600, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
#pymunk space
space = pymunk.Space()
space.gravity = (0, 981)
clock = pygame.time.Clock()
fps = 60
dt = 1/60
draw_options = pymunk.pygame_util.DrawOptions(screen)
def create_floor(space, width, height, pos):
body = pymunk.Body(body_type = pymunk.Body.STATIC)
body.position = pos
shape = pymunk.Poly.create_box(body, (width, height))
space.add(body, shape)
def create_ball(space, radius, mass):
body = pymunk.Body()
body.position = (300, 250)
shape = pymunk.Circle(body, radius)
shape.mass = mass
shape.color = (255, 0, 0, 100)
space.add(body, shape)
return shape
ball = create_ball(space, 30, 10)
create_floor(space, SCREEN_WIDTH, 20, (SCREEN_WIDTH / 2, SCREEN_HEIGHT - 10))
run = True
while run:
clock.tick(fps)
space.step(dt)
screen.fill((255, 255, 255))
space.debug_draw(draw_options)
#event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.update()
pygame.quit()
Yes, this is normal. There are however things that can be done to minimize this behavior:
The easiest method is to call space.step() with a smaller timestep (and instead call it more times). Replace space.step(dt) with something like this:
for _ in range(10):
space.step(dt/10)
Its also possible to limit the maximum speed that objects move with. The slower they move, the less overlap there will be. Finally, you can also try to adjust the collision_bias parameter on the space.

in Ipython a function named display gives me an error

# Kepler's Laws.py
# plots the orbit of a planet in an eccentric orbit to illustrate
# the sweeping out of equal areas in equal times, with sun at focus
# The eccentricity of the orbit is random and determined by the initial velocity
# program uses normalised units (G =1)
# program by Peter Borcherds, University of Birmingham, England
from vpython import *
from random import random
from IPython import display
import pandas as pd
def MonthStep(time, offset=20, whole=1): # mark the end of each "month"
global ccolor # have to make it global, since label uses it before it is updated
if whole:
Ltext = str(int(time * 2 + dt)) # end of 'month', printing twice time gives about 12 'months' in 'year'
else:
Ltext = duration + str(time * 2) + ' "months"\n Initial speed: ' + str(round(speed, 3))
ccolor = color.white
label(pos=planet.pos, text=Ltext, color=ccolor,
xoffset=offset * planet.pos.x, yoffset=offset * planet.pos.y)
ccolor = (0.5 * (1 + random()), random(), random()) # randomise colour of radial vector
return ccolor
scene = display(title="Kepler's law of equal areas", width=1000, height=1000, range=3.2)
duration = 'Period: '
sun = sphere(color=color.yellow, radius=0.1) # motion of sun is ignored (or centre of mass coordinates)
scale = 1.0
poss = vector(0, scale, 0)
planet = sphere(pos=poss, color=color.cyan, radius=0.02)
while 1:
velocity = -vector(0.7 + 0.5 * random(), 0, 0) # gives a satisfactory range of eccentricities
##velocity = -vector(0.984,0,0) # gives period of 12.0 "months"
speed = mag(velocity)
steps = 20
dt = 0.5 / float(steps)
step = 0
time = 0
ccolor = color.white
oldpos = vector(planet.pos)
ccolor = MonthStep(time)
curve(pos=[sun.pos, planet.pos], color=ccolor)
while not (oldpos.x > 0 and planet.pos.x < 0):
rate(steps * 2) # keep rate down so that development of orbit can be followed
time += dt
oldpos = vector(planet.pos) # construction vector(planet.pos) makes oldpos a varible in its own right
# oldpos = planet.pos makes "oldposs" point to "planet.pos"
# oldposs = planet.pos[:] does not work, because vector does not permit slicing
denom = mag(planet.pos) ** 3
velocity -= planet.pos * dt / denom # inverse square law; force points toward sun
planet.pos += velocity * dt
# plot orbit
curve(pos=[oldpos, planet.pos], color=color.red)
step += 1
if step == steps:
step = 0
ccolor = MonthStep(time)
curve(pos=[sun.pos, planet.pos], color=color.white)
else:
# plot radius vector
curve(pos=[sun.pos, planet.pos], color=ccolor)
if scene.kb.keys:
print
"key pressed"
duration = 'Duration: '
break
MonthStep(time, 50, 0)
label(pos=(2.5, -2.5, 0), text='Click for another orbit')
scene.mouse.getclick()
for obj in scene.objects:
if obj is sun or obj is planet: continue
obj.visible = 0 # clear the screen to do it again
I copied Kepler's Laws code in google and compiled it on pycharm.
But there is an error that
scene = display(title="Kepler's law of equal areas", width=1000, height=1000, range=3.2)
TypeError: 'module' object is not callable
I found some information on google that "pandas" library can improve this error so I tried it but I can't improve this error.
What should I do?
Replace "display" with "canvas", which is the correct name of this entity.

Paraview python 'Zoom to Box'

I am trying to make a script which does basically what tool 'Zoom to Box' does. Track option is out of question as it doesn't track camera movement.
I've found this online, fixed it to work, but keep getting '3d cam position is yet TODO' This is very old and maybe there are new options to do this? Thanks for the tips...
I could also try doing it by using classic camera commands like:
source=GetActiveSource()
#view = GetRenderView()
#view.CameraFocalPoint = [1, 0, 0]
#view.CameraViewAngle = 90
#view.CameraViewUp = [0, 0, 0]
#view.CameraPosition = [0, 0, 0]
#view.ViewSize = [1528, 542]
#view.ResetCamera()
But I'm not sure there is a way to zoom?
Fixed script from the link above:
source=GetActiveSource()
rep = Show(source)
# run the pipeline here to get the bounds
Render()
bounds = source.GetDataInformation().GetBounds()
bounds_dx = bounds[1] - bounds[0]
bounds_dy = bounds[3] - bounds[2]
bounds_dz = bounds[5] - bounds[4]
bounds_cx = (bounds[0] + bounds[1])/2.0
bounds_cy = (bounds[2] + bounds[3])/2.0
bounds_cz = (bounds[4] + bounds[5])/2.0
if bounds_dx == 0:
# yz
dimMode = 2
aspect = bounds_dz/bounds_dy
elif bounds_dy == 0:
# xz
dimMode = 1
aspect = bounds_dz/bounds_dx
elif bounds_dz == 0:
# xy
dimMode = 0
aspect = bounds_dy/bounds_dx
else:
# 3d
dimMode = 3
aspect = 1.0 # TODO
lastObj = source
view = GetRenderView()
# view.ViewTime = steps[step] # unwanted
# view.UseOffscreenRenderingForScreenshots = 0 # obsolete
rep = Show(lastObj)
# rep.Representation = 'Outline' # unwanted
Render()
# position the camera
# far = config.camFac
far = 1
near = 0
if dimMode == 0:
# xy
pos = max(bounds_dx, bounds_dy)
camUp = [0.0, 1.0, 0.0]
camPos = [bounds_cx, bounds_cy, pos*far]
camFoc = [bounds_cx, bounds_cy, -pos*near]
elif dimMode == 1:
# xz
pos = max(bounds_dx, bounds_dz)
camUp = [0.0, 0.0, 1.0]
camPos = [bounds_cx, -pos*far, bounds_cz]
camFoc = [bounds_cx, pos*near, bounds_cz]
elif dimMode == 2:
# yz
pos = max(bounds_dy, bounds_dz)
camUp = [0.0, 0.0, 1.0]
camPos = [ pos*far, bounds_cy, bounds_cz]
camFoc = [-pos*near, bounds_cy, bounds_cz]
else:
# 3d
print('3d cam position is yet TODO')
camUp=[0,0,0]
camPos=[1,0,0]
camFoc=[0,0,0]
view = GetRenderView()
view.CameraViewUp = camUp
view.CameraPosition = camPos
view.CameraFocalPoint = camFoc
#view.UseOffscreenRenderingForScreenshots = 0 # obsolete
view.CenterAxesVisibility = 0
ren = Render()
#width = int(config.outputWidth)
#height = int(config.outputWidth*aspect)
So I just went ahead and did it this way:
import sys
from paraview.simple import *
# Camera Position
# positional coordinates of the camera
# zoom is achieved by adjusting x, y, z values (moving camera closer/further away) depending on focal point
# e. g. [10,0,4] - camera will be looking at object from this coordinate
CamPos = sys.argv[1]
# Camera Focal Point
# point of interest for the camera
# the point will be in the center of the screen and camera will rotate towards him in its position
# e. g. [0,0,0] - camera will focus its center on this coordinate
CamFocPoint = sys.argv[2]
# Camera View Up
# defining which way is up in the view
# uses values <-1;1> for each vector component
# to achieve angled view, use same value '1' for two vector components
# e. g. [0,0,1] - achieves having highest z component at the top
CamViewUp = sys.argv[3]
# getting active view
camera = GetActiveCamera()
# setting based on user definition
camera.SetPosition(CamPos[0], CamPos[1], CamPos[2])
camera.SetFocalPoint(CamFocPoint[0], CamFocPoint[1], CamFocPoint[2])
camera.SetViewUp(CamViewUp[0], CamViewUp[1], CamViewUp[2])
# making sure angle is right
camera.SetViewAngle(30)
# rendering view
Render()

How to create Bezier curves from B-Splines in Sympy?

I need to draw a smooth curve through some points, which I then want to show as an SVG path. So I create a B-Spline with scipy.interpolate, and can access some arrays that I suppose fully define it. Does someone know a reasonably simple way to create Bezier curves from these arrays?
import numpy as np
from scipy import interpolate
x = np.array([-1, 0, 2])
y = np.array([ 0, 2, 0])
x = np.r_[x, x[0]]
y = np.r_[y, y[0]]
tck, u = interpolate.splprep([x, y], s=0, per=True)
cx = tck[1][0]
cy = tck[1][1]
print( 'knots: ', list(tck[0]) )
print( 'coefficients x: ', list(cx) )
print( 'coefficients y: ', list(cy) )
print( 'degree: ', tck[2] )
print( 'parameter: ', list(u) )
The red points are the 3 initial points in x and y. The green points are the 6 coefficients in cx and cy. (Their values repeat after the 3rd, so each green point has two green index numbers.)
Return values tck and u are described scipy.interpolate.splprep documentation
knots: [-1.0, -0.722, -0.372, 0.0, 0.277, 0.627, 1.0, 1.277, 1.627, 2.0]
# 0 1 2 3 4 5
coefficients x: [ 3.719, -2.137, -0.053, 3.719, -2.137, -0.053]
coefficients y: [-0.752, -0.930, 3.336, -0.752, -0.930, 3.336]
degree: 3
parameter: [0.0, 0.277, 0.627, 1.0]
Not sure starting with a B-Spline makes sense: form a catmull-rom curve through the points (with the virtual "before first" and "after last" overlaid on real points) and then convert that to a bezier curve using a relatively trivial transform? E.g. given your points p0, p1, and p2, the first segment would be a catmull-rom curve {p2,p0,p1,p2} for the segment p1--p2, {p0,p1,p2,p0} will yield p2--p0, and {p1, p2, p0, p1} will yield p0--p1. Then you trivially convert those and now you have your SVG path.
As demonstrator, hit up https://editor.p5js.org/ and paste in the following code:
var points = [{x:150, y:100 },{x:50, y:300 },{x:300, y:300 }];
// add virtual points:
points = points.concat(points);
function setup() {
createCanvas(400, 400);
tension = createSlider(1, 200, 100);
}
function draw() {
background(220);
points.forEach(p => ellipse(p.x, p.y, 4));
for (let n=0; n<3; n++) {
let [c1, c2, c3, c4] = points.slice(n,n+4);
let t = 0.06 * tension.value();
bezier(
// on-curve start point
c2.x, c2.y,
// control point 1
c2.x + (c3.x - c1.x)/t,
c2.y + (c3.y - c1.y)/t,
// control point 2
c3.x - (c4.x - c2.x)/t,
c3.y - (c4.y - c2.y)/t,
// on-curve end point
c3.x, c3.y
);
}
}
Which will look like this:
Converting that to Python code should be an almost effortless exercise: there is barely any code for us to write =)
And, of course, now you're left with creating the SVG path, but that's hardly an issue: you know all the Bezier points now, so just start building your <path d=...> string while you iterate.
A B-spline curve is just a collection of Bezier curves joined together. Therefore, it is certainly possible to convert it back to multiple Bezier curves without any loss of shape fidelity. The algorithm involved is called "knot insertion" and there are different ways to do this with the two most famous algorithm being Boehm's algorithm and Oslo algorithm. You can refer this link for more details.
Here is an almost direct answer to your question (but for the non-periodic case):
import aggdraw
import numpy as np
import scipy.interpolate as si
from PIL import Image
# from https://stackoverflow.com/a/35007804/2849934
def scipy_bspline(cv, degree=3):
""" cv: Array of control vertices
degree: Curve degree
"""
count = cv.shape[0]
degree = np.clip(degree, 1, count-1)
kv = np.clip(np.arange(count+degree+1)-degree, 0, count-degree)
max_param = count - (degree * (1-periodic))
spline = si.BSpline(kv, cv, degree)
return spline, max_param
# based on https://math.stackexchange.com/a/421572/396192
def bspline_to_bezier(cv):
cv_len = cv.shape[0]
assert cv_len >= 4, "Provide at least 4 control vertices"
spline, max_param = scipy_bspline(cv, degree=3)
for i in range(1, max_param):
spline = si.insert(i, spline, 2)
return spline.c[:3 * max_param + 1]
def draw_bezier(d, bezier):
path = aggdraw.Path()
path.moveto(*bezier[0])
for i in range(1, len(bezier) - 1, 3):
v1, v2, v = bezier[i:i+3]
path.curveto(*v1, *v2, *v)
d.path(path, aggdraw.Pen("black", 2))
cv = np.array([[ 40., 148.], [ 40., 48.],
[244., 24.], [160., 120.],
[240., 144.], [210., 260.],
[110., 250.]])
im = Image.fromarray(np.ones((400, 400, 3), dtype=np.uint8) * 255)
bezier = bspline_to_bezier(cv)
d = aggdraw.Draw(im)
draw_bezier(d, bezier)
d.flush()
# show/save im
I didn't look much into the periodic case, but hopefully it's not too difficult.

Create 3D globe of Earth with one polygon for each country

I am creating a 3D sphere of the Earth for Unity and I need to have one polygon for each country. Each country has a set of 2D points which are proyected into 3D points based on proyection. But when I create the polygon some vertices are joining incorrectly.
I used Maya to create the polygons with this code
with open("C:\\Users\\patilanz\\Downloads\\geo_countries_medium.json") as f:
d = json.load(f)
def to_3d(x,y):
#convert from degrees to radians
longitude = math.radians(x)
latitude = math.radians(y)
# select a radius:
radius = 10
# project to 3d
return (
radius * math.cos(latitude) * math.cos(longitude),
radius * math.cos(latitude) * math.sin(longitude),
radius * math.sin(latitude)
)
for feat in d.get("features"):
r = []
coords = feat.get("geometry").get("coordinates")
type = feat.get("geometry").get("type")
for coord in coords:
for c in coord:
if type == "MultiPolygon":
r = []
for a in c:
r.append(to_3d(a[0],a[1]))
poly = cmds.polyCreateFacet(p=r)
poly = cmds.rename(feat.get("properties").get("name"))
else:
r.append(to_3d(c[0],c[1]))
if not type == "MultiPoligon":
poly = cmds.polyCreateFacet(p=r)
poly = cmds.rename(feat.get("properties").get("name"))
cmds.polySphere(r = 10 * 0.98)
The problem of incorrect joining vertices
I found that this guy was trying to do the same think https://forum.processing.org/one/topic/drawing-countries-on-top-of-a-3d-sphere-from-set-of-boundaries.html
But I don't know how to implement that in Maya or in Unity.