Python random seed for Perlin noise - python

I've been recently looking to make procedurally generated terrain for games. I saw that Perlin noise was useful for this, and so I gave it a shot. So far, the terrain is generated beautifully. However, whenever I run the program multiple times, the terrain is the exact same. Is there any way to randomize the Perlin noise that's generated?
Code:
from opensimplex import OpenSimplex
import random
from time import time
height = 40
width = height
scale = height / 10
value = [[0 for x in range(width)] for y in range(height)]
gen = OpenSimplex()
def noise(nx, ny):
# Rescale from -1.0:+1.0 to 0.0:1.0
return gen.noise2d(nx, ny) / 2.0 + 0.5
def printBiome(y, x):
if value[y][x] <= 2:
print('O', end = " ")
elif value[y][x] >= 8:
print('M', end = " ")
else:
print('L', end = " ")
for y in range(height):
for x in range(width):
nx = x/width - 0.5
ny = y/height - 0.5
value[y][x] = 10 * noise(1 * scale * nx, 1 * scale * ny) + 0.5 * noise(2 * scale * nx, 2 * scale* ny) + 0.25 * noise(4 * scale * nx, 4 * scale * ny)
for y in range(height):
for x in range(width):
printBiome(y, x)
print()

The OpenSimplex class defaults to using seed=0. To generate a different terrain, input a different seed value:
import uuid
# http://stackoverflow.com/a/3530326/190597
seed = uuid.uuid1().int>>64
gen = OpenSimplex(seed=seed)

Related

Pycharm Not Displaying Line on Plot

I am trying to plot a graph from a for-loop from z=0 to z=3.8 in 0.001 increments. I then have the program solve for X_e and I would like to plot z on the y-axis and X_e on the x-axis. However, when I run the code, the program provides all the values for z and X_e and shows a plot but no line is created. Below is my code and below that is the plot it creates but doesn't finish.
import numpy as np
import matplotlib.pyplot as plt
import math
case=int(input('Which Case [1 (PWR) or 2 (BWR)]? '))
if case == 1: # PWR
H = 3.80 # m
Lc = 3.80 # m
D_rod = 0.0095 # m
pitch = 0.0125 # m
G = 3460 # kg/m^2-s
q_0 = 33000 # W/m - Linear heat rate
P_0 = 15 # MPa - Initial pressure
T_f_in = 551 # Kelvin - Inlet temperature
T_sat = 373 # Kelvin
cp = 4.22 # kJ/kg
hfg = 2256.4 # kJ/kg
heated_parameter = 3.14 * D_rod # m
area = pitch**2 - 0.25 * 3.14 * D_rod**2 # m^2
volume = 0.5 * 3.14 * (D_rod)**2 * H # m^3
circumference = 2*3.14*(D_rod/2) # m
for z in np.arange(0,3.8,0.001):
print(z)
X_e = - q_0 * heated_parameter / (G * circumference * area * hfg) * H/3.14 * math.cos(3.14 * z/H) - cp * (T_sat - T_f_in)/hfg
print(X_e)
plt.plot(X_e,z)
plt.show()
This is the graph that is created.
You're passing single points to plt.plot. Use np.cos (which is vectorized) and skip the loop:
z = np.arange(0, 3.8, 0.001)
X_e = -q_0 * heated_parameter / (G * circumference * area * hfg) * H/3.14 * np.cos(3.14 * z/H) - cp * (T_sat - T_f_in)/hfg
plt.plot(X_e, z)
Output:
You might also consider using np.pi instead of 3.14.

Perlin noise generator isn't working, doesn't look smooth

I watched some tutorials and tried to create a Perlin noise generator in python.
It takes in a tuple for the number of vectors in the x and y directions and a scale for the distance in pixels between the arrays, then calculates the dot product between each pixel and each of the 4 arrays surrounding it, It then interpolates them bilinearly to get the pixel's value.
here's the code:
from PIL import Image
import numpy as np
scale = 16
size = np.array([8, 8])
vectors = []
for i in range(size[0]):
for j in range(size[1]):
rand = np.random.rand() * 2 * np.pi
vectors.append(np.array([np.cos(rand), np.sin(rand)]))
interpolated_map = np.zeros(size * scale)
def interpolate(x1, x2, w):
t = (w % scale) / scale
return (x2 - x1) * t + x1
def dot_product(a, b):
return a[0] * b[0] + a[1] * b[1]
for i in range(size[1] * scale):
for j in range(size[0] * scale):
dot_products = []
for m in range(4):
corner_vector_x = round(i / scale) + (m % 2)
corner_vector_y = round(j / scale) + int(m / 2)
x = i - corner_vector_x * scale
y = j - corner_vector_y * scale
if corner_vector_x >= size[0]:
corner_vector_x = 0
if corner_vector_y >= size[1]:
corner_vector_y = 0
corner_vector = vectors[corner_vector_x + corner_vector_y * (size[0])]
distance_vector = np.array([x, y])
dot_products.append(dot_product(corner_vector, distance_vector))
x1 = interpolate(dot_products[0], dot_products[1], i)
x2 = interpolate(dot_products[2], dot_products[3], i)
interpolated_map[i][j] = (interpolate(x1, x2, j) / 2 + 1) * 255
img = Image.fromarray(interpolated_map)
img.show()
I'm getting this image:
but I should be getting this:
I don't know what's going wrong, I've tried watching multiple different tutorials, reading a bunch of different articles, but the result is always the same.

Perlin Noise in Ursina

Is there a way to incorporate Perlin Noise into the Ursina game engine in Python to give it a Minecraft feeling? I think I may be onto something, but for some reason, the y value is not varying. Do you mind helping me out?
The code that I have so far:
from ursina import *
from ursina.prefabs.first_person_controller import FirstPersonController
from perlin_noise import PerlinNoise;
app = Ursina()
noise = PerlinNoise(octaves=10, seed=1)
# Define a Voxel class.
# By setting the parent to scene and the model to 'cube' it becomes a 3d button.
class Voxel(Button):
def __init__(self, position=(0,0,0)):
super().__init__(
parent = scene,
position = position,
model = 'cube',
origin_y = .5,
texture = 'white_cube',
color = color.color(0, 0, random.uniform(.9, 1.0)),
highlight_color = color.lime,
)
def input(self, key):
if self.hovered:
if key == 'left mouse down':
voxel = Voxel(position=self.position + mouse.normal)
if key == 'right mouse down':
destroy(self)
for z in range(8):
for x in range(8):
y = .25 + noise([x, z])
voxel = Voxel(position=(x, y,z))
player = FirstPersonController()
app.run()
As shown in the usage examples, you'll have to scale the noise value with an additional division:
for z in range(8):
for x in range(8):
y = .25 + noise([x/8, z/8])
voxel = Voxel(position=(x, y,z))
You'll probably want to replace that magic number with a constant.
You can use this:
seed=random.randint(1,1000000)
noise = PerlinNoise(octaves=3,seed=seed)
for z in range(8):
for x in range(8):
y = noise([x * 0.02,z * 0.02])
y = math.floor(y * 7.5)
voxel = Voxel(position=(x,y,z))
First, you did not say "Perlin Noise" correctly, so remove the semi-colon, then you'll use this code:
noise = PerlinNoise(octaves=intgeer,seed=0)
for z in range(8):
for x in range(8):
y = 0. 25 noise([x * 0.02,z * 0.02])
voxel = Voxel(position=(x,y,z))
It sounds like Voxel(...) places a voxel at the specified position, right? If so, then you would need to loop over the whole column to place the blocks into.
And if your noise library doesn't handle frequency and amplitude scaling internally, then you need to do that externally. Multiply the input coordinates by fractions, and multiply the output value by larger values.
min_height = 4.0
max_height = 16.0
for z in range(8):
for x in range(8):
height = # some noise formula
# if noise() range is 0 to 1: min_height + noise([x * 0.05, z * 0.05]) * (max_height - min_height)
# if noise() range is -1 to 1: min_height + (noise([x * 0.05, z * 0.05]) * 0.5 + 0.5) * (max_height - min_height)
for y in range(height): # might need to convert height to an integer
voxel = Voxel(position=(x,y,z))
Also please keep in mind the problems of Perlin for noise in its unmitigated form. The algorithm's popularity by name far outweighs its canonical form's true merit in light of its square alignment issues and the broader space of options we have now, and in spite of the overwhelming amount of content that still teaches it in a vacuum. See the final paragraph from my last answer for more info: https://stackoverflow.com/a/73348943
I would suggest this: https://pypi.org/project/pyfastnoiselite/
from pyfastnoiselite.pyfastnoiselite import (
FastNoiseLite, NoiseType, FractalType,
)
min_height = 4.0
max_height = 16.0
noise = FastNoiseLite(0)
noise.noise_type = NoiseType.NoiseType_OpenSimplex2
noise.fractal_type = FractalType.FractalType_FBm
noise.fractal_octaves = 4
noise.frequency = 0.03
for z in range(8):
for x in range(8):
height = min_height + (noise.get_noise(x, z) * 0.5 + 0.5) * (max_height - min_height)
for y in range(height): # might need to convert height to an integer
Voxel(position=(x,y,z))
or for 3D noise to enable overhangs, you vary that heightmap within Y as well:
from pyfastnoiselite.pyfastnoiselite import (
FastNoiseLite, NoiseType, FractalType,
)
min_height = 4.0
max_height = 16.0
noise = FastNoiseLite(0)
noise.noise_type = NoiseType.NoiseType_OpenSimplex2
noise.fractal_type = FractalType.FractalType_FBm
noise.fractal_octaves = 4
noise.frequency = 0.03
for z in range(8):
for x in range(8):
for y in range(min_height): # might need to convert bounds to integers
Voxel(position=(x,y,z))
for y in range(min_height, max_height): # might need to convert bounds to integers
effective_height = min_height + (noise.get_noise(x, y, z) * 0.5 + 0.5) * (max_height - min_height)
if effective_height > y:
Voxel(position=(x,y,z))
Examples untested. There may be some changes to parameters or even syntax to get these working! Feel free to make the needed edits to this answer to get it working properly.
Also min_height + (noise.get_noise(x, y, z) * 0.5 + 0.5) * (max_height - min_height) can be simplified down to noise.get_noise(x, y, z) * factor + offset, where
factor = 0.5 * (max_height - min_height)
offset = factor + min_height

projecting points onto a parallel grid at a depth from camera (maya)

I am attempting to create a grid of locators that serve as projected points onto a parallel finite plane from a camera in maya at a specified depth. The grid should line up with a specified resolution so as to match rendered output.
At the moment my calculations are off and I am looking for some help to ascertain how my formula for ascertaining the projected points is incorrect.
I have a self contained python script and image showing the current position of locators that are spawned as an example.
image showing current spawned locators are off on y and z axis
import maya.cmds as mc
import maya.OpenMaya as om
res = [mc.getAttr('defaultResolution.width'),
mc.getAttr('defaultResolution.height')]
print res
grid = [5, 5]
def projectedGridPoint(camera, coord, depth, res):
selList = om.MSelectionList()
selList.add(camera)
dagPath = om.MDagPath()
selList.getDagPath(0,dagPath)
dagPath.extendToShape()
camMtx = dagPath.inclusiveMatrix()
fnCam = om.MFnCamera(dagPath)
mFloatMtx = fnCam.projectionMatrix()
projMtx = om.MMatrix(mFloatMtx.matrix)
#center of camera
eyePt = fnCam.eyePoint()
#offset position
z = eyePt.z - depth
#calculated xy positions
x = (2 * z * coord[0] / res[0]) - z
y = (2 * z * coord[1] / res[1]) - z
return om.MPoint(x,y,depth) * camMtx * projMtx.inverse()
for y in range(grid[1] + 1):
for x in range(grid[0] + 1):
coord = ( x / float(grid[0]) * res[0], y / float(grid[1]) * res[1] )
pt = projectedGridPoint('camera1', coord, 10, res)
mc.spaceLocator(a=1, p=[pt.x, pt.y, pt.z])
Once I adjusted Theodox's answer to account for all possible grid divisions, such that the ndc_x and ndc_y was always in the range of -1 and 1. I was able to get a working solution.
import maya.api.OpenMaya as om
import maya.cmds as cmds
def projectedGridPoint(camera, coord, depth):
selList = om.MGlobal.getSelectionListByName(camera)
dagPath = selList.getDagPath(0)
dagPath.extendToShape()
view = dagPath.inclusiveMatrix()
fnCam = om.MFnCamera(dagPath)
projection = om.MMatrix(fnCam.projectionMatrix())
viewProj = projection * view
r = om.MPoint(coord[0],coord[1], -1 * depth) * projection.inverse()
return r.homogenize() * view
xx, yy = (6, 6)
for y in range(yy + 1):
for x in range(xx + 1):
ndc_x = -1
ndc_y = -1
if x > 0:
ndc_x = (x / float(xx) * 2) - 1
if y > 0:
ndc_y = (y / float(yy) * 2) - 1
coord = ( ndc_x, ndc_y)
print coord
pt = projectedGridPoint('camera1', coord, 0)
c,_ = cmds.polyCube(w = 0.1, d = 0.1, h = 0.1)
cmds.xform(c, t = (pt[0], pt[1], pt[2]))
I think you want something a bit more like this (note, i converted it to API 2 to cut down on the boilerplate)
import maya.api.OpenMaya as om
import maya.cmds as cmds
def projectedGridPoint(camera, coord, depth):
selList = om.MGlobal.getSelectionListByName(camera)
dagPath = selList.getDagPath(0)
dagPath.extendToShape()
view = dagPath.inclusiveMatrix()
fnCam = om.MFnCamera(dagPath)
projection = om.MMatrix(fnCam.projectionMatrix())
viewProj = projection * view
r = om.MPoint(coord[0],coord[1], -1 * depth) * projection.inverse()
return r.homogenize() * view
xx, yy = (2, 2)
for y in range(yy):
for x in range(xx):
ndc_x = 2.0 * x / float(xx - 1) - 1
ndc_y = 2.0 * y / float(yy - 1) - 1
coord = ( ndc_x, ndc_y)
pt = projectedGridPoint('camera1', coord,0)
c,_ = cmds.polyCube(w = 0.1, d = 0.1, h = 0.1)
cmds.xform(c, t = (pt[0], pt[1], pt[2]))
The coords are supplied as normalized device coordinates (from -1,-1 to 1, 1 at the corners of the view) and the depth goes from the near to far clip planes -- a depth of 1 is right on the near plane and a depth of 0 is on the far plane. I think in practice I'd lock the depth at 0 and use the clip plane setting on the camera to set the depth
edit I rationalized the original, wonky method of converting index values to NDC coordinates

Get circle to orbit with given equations using Python graphics.py module

I was given this equation in order to get the circle to orbit. I created an infinite loop assuming it should orbit forever. x = cx + r*cos(t) and y = cy + r*sin(t)
Am I doing something wrong?
from graphics import *
import math
def main():
win=GraphWin("Cirlce",600,600)
x=250
y=70
c=Circle(Point(x,y),18)
c.draw(win)
v=True
while v==True:
c.undraw()
x = x + c.getRadius()*math.cos(2)
y = y + c.getRadius()*math.sin(2)
c=Circle(Point(x,y),18)
c.draw(win)
main()
The problem is here:
x = x + c.getRadius()*math.cos(2)
y = y + c.getRadius()*math.sin(2)
You are moving in a straight line. And, as your code runs quite fast, it probably goes out of scope quite quickly. The correct version would be:
x0, y0 = 0, 0 # Coordinates of the centre
r = 2 # Radius
t = 0
dt = 0.01 # Or anything that looks smooth enough.
while True: # No need for an extra variable here
c.undraw() # I don't know this graphics library
# I will assume what you did is correct
x = x0 + r * math.cos(t)
y = y0 + r * math.sin(t)
c=Circle(Point(x,y),18)
c.draw(win)
t += dt
time.sleep(0.01)
At the end of the loop I send it to sleep for a bit so it goes at a finite pace. Some graphics libraries include a rate function, that allows you to run it at a fixed number of frames per second, independently of how fast the loop is in your machine.
c.undraw() # I don't know this graphics library, I will assume what
you did is correct
Using c.undraw(), c = Circle(...), and c.draw() on every loop iteration seems wasteful when GraphicsObjects can move(dx, dy). However, the tricky part is the movement is relative so you have to calculate the difference to your next position:
import math
import time
from graphics import *
WINDOW_WIDTH, WINDOW_HEIGHT = 600, 600
win = GraphWin("Circle", WINDOW_WIDTH, WINDOW_HEIGHT)
win.setCoords(-WINDOW_WIDTH / 2, -WINDOW_HEIGHT / 2, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)
ORBIT_RADIUS = 200
PLANET_RADIUS = 18
SOLAR_RADIUS = 48
x0, y0 = 0, 0 # Coordinates of the center
t = 0.0
dt = 0.01 # Or anything that looks smooth enough.
delay = 0.01
star = Circle(Point(x0, y0), SOLAR_RADIUS)
star.setFill("yellow")
star.draw(win)
orbit = Circle(Point(x0, y0), ORBIT_RADIUS)
orbit.setOutline("lightgray")
orbit.draw(win)
planet = Circle(Point(x0 + ORBIT_RADIUS * math.cos(t), y0 + ORBIT_RADIUS * math.sin(t)), PLANET_RADIUS)
planet.setFill("blue")
planet.draw(win)
while True:
x, y = x0 + ORBIT_RADIUS * math.cos(t), y0 + ORBIT_RADIUS * math.sin(t)
center = planet.getCenter()
planet.move(x - center.getX(), y - center.getY())
t = (t + dt) % (2 * math.pi)
if win.checkMouse() is not None:
break
time.sleep(delay)
win.close()
I added a star as it's hard to appreciate that something is orbiting without seeing what's being orbited:
You can click on the window to exit cleanly.

Categories