• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
  • comments
  • views

Logarithmic Spiral Distance Field

Sign in to follow this  
Followers 0


I have been playing around with distance field rendering, inspired by some of Inigo Quilez's work. Along the way I needed to define analytic distance functions for a number of fairly esoteric geometric primitives, among them the logarithmic spiral:
The distance function for this spiral is not particularly hard to derive, but the derivation isn't entirely straightforward, and it isn't documented anywhere else, so I thought I would share. I am only going to deal with logarithmic spirals centered on the origin, but the code is trivial to extend for spirals under translation.

Spirals are considerably more tractable in polar coordinates, so we start with the polar coordinate form of the logarithmic spiral equation:

latex.php?latex=r+%3D+ae%5E%7Bb%5CTheta%7D&bg=ffffff&fg=666666&s=0 (1)

Where (roughly) a controls the starting angle, and b controls how tightly the spiral is wound.

Since we are given an input point in x,y Cartesian form, we need to convert that to polar coordinates as well:


Now, we can observe that the closest point on the spiral to our input point must be on the line running through our input point and the origin - draw the line on the graph above if you want to check for yourself. Since the logarithmic spiral passes through the same radius line every 360?, this means than the closest point must be at an angle of:

latex.php?latex=%5CTheta_%7Bfinal%7D+%3D+%5CTheta_%7Btarget%7D+%2B+n+%2A360%5E%7B%5Ccirc%7D&bg=ffffff&fg=666666&s=0 (2)

Where n is a non-negative integer. We can combine (1) and (2), to arrive at an equation for r in terms of n:

latex.php?latex=r+%3D+ae%5E%7Bb%28%5CTheta_%7Btarget%7D+%2B+n%2A360%5E%7B%5Ccirc%7D%29%7D&bg=ffffff&fg=666666&s=0 (3)

Which means we can find r if we know n. Unfortunately we don't know n, but we do know r[sub]target[/sub], which is an approximation for the value of r. We start by rearranging equation (3) in terms of n:

latex.php?latex=n+%3D+%5Cfrac%7B%5Cfrac%7Bln%28%5Cfrac%7Br%7D%7Ba%7D%29%7D%7Bb%7D+-+%5CTheta_%7Btarget%7D%7D%7B360%5E%7B%5Ccirc%7D%7D&bg=ffffff&fg=666666&s=0 (4)

Now, feeding in the value of r[sub]target[/sub] for r will give us an approximate value for n. This approximation will be a real (float, if you prefer), and we can observe from the graph above that the closest point must be at either the next larger or smaller integer value of n.

If we take the floor and ceil of our approximation for n, we will have both integer quantities, and can feed each value back into equation (3) to determine the two possible values of r, r[sub]1[/sub] and r[sub]2[/sub]. The final step involves finding which of these is the closest, and the distance thereof:


And there you have it:

spiral.png?w=510&h=510Distance field for a logarithmic spiral

The python source code below produces the image shown above, as a 1000x1000 pixel image PNM image written to stdout. If you aren't familiar with the PNM format, it is an exceedingly simple ascii-based analogue of a bitmap image, and can be loaded directly in GIMP.

import mathdef spiral(x, y, a=1.0, b=1.0): # calculate the target radius and theta r = math.sqrt(x*x + y*y) t = math.atan2(y, x) # early exit if the point requested is the origin itself # to avoid taking the logarithm of zero in the next step if (r == 0): return 0 # calculate the floating point approximation for n n = (math.log(r/a)/b - t)/(2.0*math.pi) # find the two possible radii for the closest point upper_r = a * math.pow(math.e, b * (t + 2.0*math.pi*math.ceil(n))) lower_r = a * math.pow(math.e, b * (t + 2.0*math.pi*math.floor(n))) # return the minimum distance to the target point return min(abs(upper_r - r), abs(r - lower_r))# produce a PNM image of the resultif __name__ == '__main__': print 'P2' print '# distance field image for spiral' print '1000 1000' print '255' for i in range(-500, 500): for j in range(-500, 500): print '%3d' % min( 255, int(spiral(i, j, 1.0, 0.5)) ), print
401 401 401 401 401 401 401 b.gif?host=swiftcoder.wordpress.com&blog=3224297&post=401&subd=swiftcoder&ref=&feed=1


Sign in to follow this  
Followers 0


There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now