• 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
nullsquared

UV coordinate on a 2D quadrilateral

10 posts in this topic

Hey.
[img]http://anyhub.net/file/1X6a-quad_lerp.png[/img]
Given the coordinates a, b, c, d, and p, how would I find the normalized UV coordinates of p? (For example, to sample a texture at that point.)

a, b, c, d, and p are 2D (that is, only X,Y coordinates). p will always be inside abcd.

I have no idea where to start. Any ideas?
0

Share this post


Link to post
Share on other sites
I don't know if this is the most efficient, but you can determine which of 2 triangles* p is located within. Then calculate the barycentric coordinates of p in that triangle and apply those coords to the texture coordinates of the vertices.

*E.g., adc and acb, etc.
0

Share this post


Link to post
Share on other sites
the issue with buckeye's solution is the uv coordinates won't be continuous except for the special case of a paralellogram/rectangle/square

a continous uv mapping could consist of the solutions to:

p = a + ux + vy + uvz

where the four coordinates of your rectangle in order are a,b,c,d, with x = b-a, y = d-a, z = a+c-d-b


i don't however know of a solution to this, and wolframalpha doesn't give you anything pretty for it!

(I come up with that by linearly interpolating accross top and bottom lengths with 'u', then linearly interpolating between those two points with 'v' which gives a unique, continous, but very complex mapping of p to (u,v) coordinates.
0

Share this post


Link to post
Share on other sites
If you use two triangles to stretch a rectangle image like that, you might need to consider the problem described on the following page: [url="http://home.xyzw.us/~cass/qcoord/"]http://home.xyzw.us/~cass/qcoord/[/url]
0

Share this post


Link to post
Share on other sites
Hm, you're right. Any other ideas? I'm basically trying to map out a virtual 2D quadrilateral onto a physical, rectangular screen. (Basically, texture mapping it.)
0

Share this post


Link to post
Share on other sites
Don't know of a closed solution, but of course is it possible to "bilateral binary search" over (u,v) and checking each step in which quadrant p lies. It would even be sufficient to iterate over u only, until the resulting line will be close enough to p, and then solving for v analytically.
1

Share this post


Link to post
Share on other sites
there is a closed solution, but it's very long!

ultimately you can reduce the problem to:
[code]

( u )
( 1 a b c )( uv ) = ( 0 )
( 0 1 d e )( v ) ( 0 )
( 1 )
[/code]

which has a very complex closed solution of: [url="http://www.wolframalpha.com/input/?i=v+%3D+-e/(u%2Bd),+u+%3D+-(bv%2Bc)/(1%2Bav),+solve+for+u,v"]http://www.wolframal...,+solve+for+u,v[/url]

of which only one of the two top solutions should be the correct one.

a,b,c,d,e being rather complex equatinos involving your coordinate p, and the coordinates of the 4 rectangle points starting with:

[code]

( u )
( bx-ax cx-bx dx-ax ax-px )( uv ) = ( 0 )
( by-ay cy-by dy-ay ay-py )( v ) ( 0 )
( 1 )
[/code]

with rectangle being represented with top-left a, top-right b, bottom-right c, bottom-left d. u running left to right, v running top to bottom
0

Share this post


Link to post
Share on other sites
There are lots of reasonable answers here.

We've had this discussion here in the past; if you search the old forums you can probably find those threads. The solutions always boiled down to one of,
1.) Split into triangles and do barycentric interpolation on each (buckeye's answer)
2.) Bilinear interpolation (luca-deltodesco's answer)
Details were given in those threads for both of these approaches.

Here's a third: [EDIT: Removed; was wrong.]
0

Share this post


Link to post
Share on other sites
[quote name='haegarr' timestamp='1298663141' post='4779072']
Yeah, I though of looking at it the following way:

The approach of the bilinear interpolation
[b]p[/b][sub]1[/sub] := [b]a[/b] + ( [b]b[/b] - [b]a[/b] ) * u
[b]p[/b][sub]2[/sub] := [b]d[/b] + ( [b]c[/b] - [b]d[/b] ) * u
[b]p[/b] := [b]p[/b][sub]1[/sub] + ( [b]p[/b][sub]2[/sub] - [b]p[/b][sub]1[/sub] ) * v
means that there is a line between [b]p[/b][sub]1[/sub] and [b]p[/b][sub]2[/sub] that passes through [b]p[/b]. Expressing this line the other way, namely
[b]p[/b] + [b]l[/b] = [b]p[/b][sub]1[/sub]
[b]p[/b] + k * [b]l[/b] = [b]p[/b][sub]2[/sub]
so that [b]l[/b] + k * [b]l[/b] (where k is obviously need to be a negative number) is the said line. Considering that this is done in 2D, we have 4 equations with 4 unkowns (k, l[sub]x[/sub], l[sub]y[/sub], and u) where k * l[sub]x[/sub] and k * l[sub]y[/sub] are the problems. Fortunately, from the lower of the both equations, we can isolate
k * l[sub]x[/sub] = d[sub]x[/sub] - p[sub]x[/sub] + ( c[sub]x[/sub] - d[sub]x[/sub] ) * u
k * l[sub]y[/sub] = d[sub]y[/sub] - p[sub]y[/sub] + ( c[sub]y[/sub] - d[sub]y[/sub] ) * u
and divide both of these, so that
l[sub]x[/sub] / l[sub]y [/sub]= [ d[sub]x[/sub] - p[sub]x[/sub] + ( c[sub]x[/sub] - d[sub]x[/sub] ) * u ] / [ d[sub]y[/sub] - p[sub]y[/sub] + ( c[sub]y[/sub] - d[sub]y[/sub] ) * u ]

Now, using the upper of the equations to get
l[sub]x[/sub] = a[sub]x[/sub] - p[sub]x[/sub] + ( b[sub]x[/sub] - a[sub]x[/sub] ) * u
l[sub]y[/sub] = a[sub]y[/sub] - p[sub]y[/sub] + ( b[sub]y[/sub] - a[sub]y[/sub] ) * u
and setting these into the above l[sub]x[/sub] / l[sub]y[/sub], we get a quadratic equation solely in u which can be solved using the famous p,q-formula.

This gives, as luca-deltodesco has mentioned, 0 or 1 real solutions in general. But due to the fact that [b]p[/b] always lies inside the quad, I expect 1 real solution. With that, v can be determined easily.
[/quote]

Thank you very, very much! This came out to be the easiest solution to implement and it works great!

In case anyone is interested in the same problem, here's some code:
[code]

double C = (double)(a.Y - p.Y) * (d.X - p.X) - (double)(a.X - p.X) * (d.Y - p.Y);
double B = (double)(a.Y - p.Y) * (c.X - d.X) + (double)(b.Y - a.Y) * (d.X - p.X) - (double)(a.X - p.X) * (c.Y - d.Y) - (double)(b.X - a.X) * (d.Y - p.Y);
double A = (double)(b.Y - a.Y) * (c.X - d.X) - (double)(b.X - a.X) * (c.Y - d.Y);

double D = B * B - 4 * A * C;

double u = (-B - Math.Sqrt(D)) / (2 * A);

double p1x = a.X + (b.X - a.X) * u;
double p2x = d.X + (c.X - d.X) * u;
double px = p.X;

double v = (px - p1x) / (p2x - p1x);
[/code]
u and v are then [0,1] coordinates you can sample a texture from, etc.

Thanks again!
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