• 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.
Sign in to follow this  
Followers 0
ToastFlambe

Finding the normal to a 2d line

8 posts in this topic

Basically I've got two points: (x1, y1), (x2, y2) that form a line(duh) and I want to find a vector perpendicular to that line so i can turn it into a unit normal. I'm frightened because this seems like it should be real easy to figure out ,but I've searched up and down Gamedev and Google and can't find what I want. The closest I've gotten was something another thread suggested: finding the perp-dot product of the two vectors. But I don't know what to do with it once I've found it :(. Any suggestions?
0

Share this post


Link to post
Share on other sites
There are two normals (if you stay in 2D), one into each half-space defined by the original line.

If the lines are ordered, and you want the counter-clockwise normal, then it's simple to see what to do.

First, generate the rotation matrix that will rotate a point by 90 degrees counterclockwise in 2D. I'm assuming row vertices on the left:

0 1
[x y] * = [Xn Yn]
-1 0


Then, generate the tangent for the line:


[Xt Yt] = [x2 y2]-[x1 y1] = [x2-x1 y2-y1]


Last, rotate through the matrix and simplify:


0 1
[Xn Yn] = [x2-x1 y2-y1] * = [x1-x2 y2-y1]
-1 0


(Watch the order!)

Normalize and you have the actual normal. The clockwise normal would be the 90 degree rotation the other way.
1

Share this post


Link to post
Share on other sites
I should have been able to figure that out :P. Isn't what you showed above the clockwise rotation though? I mean if we have points (4, 0) and (0, 4) putting it into that equation gives

[x1-x2, y2-y1]
[4 - 0, 4 - 0]

[4, 4]

which is clockwise.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603

0 1
[Xn Yn] = [x2-x1 y2-y1] * = [x1-x2 y2-y1]
-1 0


0 1
[Xn Yn] = [x2-x1 y2-y1] * = [y1-y2 x2-x1]
-1 0
Clockwise would be

0 -1
[Xn Yn] = [x2-x1 y2-y1] * = [y2-y1 x1-x2]
1 0
0

Share this post


Link to post
Share on other sites
If the line formula is defined as: y = m * x
where m = dy/dx = (y2-y1)/(x2-x1)

Then the perpendicular line is defined as:
y = m'*x
where m' = -1/m = -(dx/dy) = -dx/dy = dx/-dy

So you have two solutiond for m'
m' = -dx/dy
so m' = x1-x2/y2-y1 (1)

or m' = dx/-dy
so m' = x2-x1/y1-y2 (2)

Which are the solutions HPPlus presented

Luck!
Guimo
0

Share this post


Link to post
Share on other sites
i belive you could just:

v is the direction of the line

n1 is the first "normal"
n2 is the second

n1<-v.y, v.x>
n2<v.y, -v.x>

normalize them if necessary.

of course i'm assuming you're defineing the line as a segment with two endpoints or in terms of a vector and a point.
0

Share this post


Link to post
Share on other sites
Much easier way:

1) Find the direction of the line by subtracting one point from the other
2) Convert the direction into a 3D vector, leave z as 0.
3) do a cross product with (0,0,1)
4) normalize the result

Voila, you have the normal to the line!
-1

Share this post


Link to post
Share on other sites
[quote name='Guimo' timestamp='1136246531' post='3419113']
So you have two solutiond for m'
m' = -dx/dy
so m' = x1-x2/y2-y1 (1)

or m' = dx/-dy
so m' = x2-x1/y1-y2 (2)

Which are the solutions HPPlus presented
[/quote]
Careful with this approach, as you need to handle the case when [tt]y1-y2[/tt] is zero.

[quote name='xytor' timestamp='1327213293' post='4905042']
Much easier way:

1) Find the direction of the line by subtracting one point from the other
2) Convert the direction into a 3D vector, leave z as 0.
3) do a cross product with (0,0,1)
4) normalize the result

Voila, you have the normal to the line!
[/quote]
Not everyone working with 2D space has a 3D math library readily available. Hplus' approach makes the most sense (with an thorough explanation even!) and avoids 6 multiplications, 3 subtractions, and storage overhead for z component and the extra vector.
0

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1327218145' post='4905050']
Not everyone working with 2D space has a 3D math library readily available. Hplus' approach makes the most sense (with an thorough explanation even!) and avoids 6 multiplications, 3 subtractions, and storage overhead for z component and the extra vector.
[/quote]

It's super cheap and easy to make your own cross product function that takes one 2d vector, assumes the z is 0 and crosses it with (0,0,1).

Here, let me give you the exact formula:
Given a 2d vector (a,b), the normal is (x,y):
x = b
y = -a

So basically, flip a and b, and negate the y. Two assignments and one negation. Can't be cheaper!
1

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0