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

Inaccurate y values in the distance function for Bezier clipping

0 posts in this topic

I'm currently working on implementing a curve intersection algorithm known as Bezier clipping, which is described towards the end of [url="http://processingjs.nihongoresources.com/bezierinfo/#clipping_fl"]this article[/url]. I've been following through the article and source code (which can be found [url="http://processingjs.nihongoresources.com/bezierinfo/sketchsource.php?sketch=cubicFatLine1"]here[/url]) to implement it myself. A central part of this algorithm is the calculation of a "distance function" between curve2 and the baseline (a line running from start point to end point) of curve1. This seems to be where I've run into some trouble. When I draw out the distance function, the curve isn't anywhere near the other two curves, despite both clearly intersecting.

From what I can gather from the article (and my understanding could quite possibly be wrong), the calculation of the distance function is as follows.

The baseline is represented in the form [b]x*a + y*b + c = 0[/b], where [b]a[sup]2[/sup] + b[sup]2[/sup] = 1[/b]. The coefficients [b]a[/b], [b]b[/b] and [b]c[/b] are calculated by rearranging the representation [b]y = u*x + v[/b], where [b]u[/b] is the slope of the baseline ([b](y2 - y1)/(x2 - x1)[/b]), and [b]x[/b] and [b]y[/b] are any coordinate pairs that lie on the baseline. The formula can then be rearranged to [b]v = y - u*x[/b], and then rearranged to [b]-u*x + 1*y - v = 0[/b], meaning that [b]a = -u[/b], [b]b = 1[/b], and[b] c = -v[/b]. To assure [b]a[sup]2[/sup] + b[sup]2[/sup] = 1[/b], all three coefficients are divided by a scalar of [b]Math.sqrt(u[sup]2[/sup] + 1)[/b]. This function is then substituted into the parametric formula of curve2, resulting in a bezier curve with[b] Y[sub]i[/sub] = a*x[sub]i[/sub] + b*y[sub]i[/sub] + c [/b](where [b]x[sub]i [/sub][/b]and [b]y[sub]i [/sub][/b]are the control points of curve2)and [b]X[sub]i[/sub] = (1 - t)[sup]3[/sup]*y[sub][size=3]1[/size][/sub] + 3*( - t)[sup]2[/sup]*t*y[sub]2 [/sub]+ 3*(1 - t)*t[sup]2[/sup]*y[sub]3[/sub] + t[sup]3[/sup]*y[sub]3[/sub] for t = {0, 1/3, 2/3, 3}[/b].

Below are some excerpts from the code of the example program on the article (written in a language called Processing) which, oddly, uses a slightly different approach to calculating the alternative representation of the curve,:

[CODE]
/**
* Set up four points, to form a cubic curve, and a static curve that is used for intersection checks
*/
void setupPoints()
{
points = new Point[4];
points[0] = new Point(85,30);
points[1] = new Point(180,50);
points[2] = new Point(30,155);
points[3] = new Point(130,160);

curve = new Bezier3(175,25, 55,40, 140,140, 85,210);
curve.setShowControlPoints(false);
}

...

flcurve = new Bezier3(points[0].getX(), points[0].getY(),
points[1].getX(), points[1].getY(),
points[2].getX(), points[2].getY(),
points[3].getX(), points[3].getY());

...

void drawClipping()
{
double[] bounds = flcurve.getBoundingBox();

// get the distances from C1's baseline to the two other lines
Point p0 = flcurve.points[0];
// offset distances from baseline
double dx = p0.x - bounds[0];
double dy = p0.y - bounds[1];
double d1 = sqrt(dx*dx+dy*dy);
dx = p0.x - bounds[2];
dy = p0.y - bounds[3];
double d2 = sqrt(dx*dx+dy*dy);

...

double a, b, c;
a = dy / dx;
b = -1;
c = -(a * flcurve.points[0].x - flcurve.points[0].y);
// normalize so that a² + b² = 1
double scale = sqrt(a*a+b*b);
a /= scale; b /= scale; c /= scale;

// set up the coefficients for the Bernstein polynomial that
// describes the distance from curve 2 to curve 1's baseline
double[] coeff = new double[4];
for(int i=0; i<4; i++) { coeff[i] = a*curve.points[i].x + b*curve.points[i].y + c; }
double[] vals = new double[4];
for(int i=0; i<4; i++) { vals[i] = computeCubicBaseValue(i*(1/3), coeff[0], coeff[1], coeff[2], coeff[3]); }

translate(0,100);

...

// draw the distance Bezier function
double range = 200;
for(float t = 0; t<1.0; t+=1.0/range) {
double y = computeCubicBaseValue(t, coeff[0], coeff[1], coeff[2], coeff[3]);
params.drawPoint(t*range, y, 0,0,0,255); }

...

translate(0,-100);
}

...

/**
* compute the value for the cubic bezier function at time=t
*/
double computeCubicBaseValue(double t, double a, double b, double c, double d) {
double mt = 1-t;
return mt*mt*mt*a + 3*mt*mt*t*b + 3*mt*t*t*c + t*t*t*d; }[/CODE]

And below is the source code (written in Java) for my attempt to recreate the above process:


[CODE]import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;


public class ReplicateBezierClippingPanel extends JPanel {

CubicCurveExtended curve1, curve2;

public ReplicateBezierClippingPanel(CubicCurveExtended curve1, CubicCurveExtended curve2) {

this.curve1 = curve1;
this.curve2 = curve2;

}

public void paint(Graphics g) {

super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(1));
g2d.setColor(Color.black);
drawCurve1(g2d);
drawCurve2(g2d);
drawDistanceFunction(g2d);

}

public void drawCurve1(Graphics2D g2d) {

double range = 200;

double t = 0;

double prevx = curve1.x1*(1 - t)*(1 - t)*(1 - t) + 3*curve1.ctrlx1*(1 - t)*(1 - t)*t + 3*curve1.ctrlx2*(1 - t)*t*t + curve1.x2*t*t*t;
double prevy = curve1.y1*(1 - t)*(1 - t)*(1 - t) + 3*curve1.ctrly1*(1 - t)*(1 - t)*t + 3*curve1.ctrly2*(1 - t)*t*t + curve1.y2*t*t*t;

for(t += 1.0/range; t < 1.0; t += 1.0/range) {

double x = curve1.x1*(1 - t)*(1 - t)*(1 - t) + 3*curve1.ctrlx1*(1 - t)*(1 - t)*t + 3*curve1.ctrlx2*(1 - t)*t*t + curve1.x2*t*t*t;
double y = curve1.y1*(1 - t)*(1 - t)*(1 - t) + 3*curve1.ctrly1*(1 - t)*(1 - t)*t + 3*curve1.ctrly2*(1 - t)*t*t + curve1.y2*t*t*t;

g2d.draw(new LineExtended(prevx, prevy, x, y));

prevx = x;
prevy = y;

}

}

public void drawCurve2(Graphics2D g2d) {

double range = 200;

double t = 0;

double prevx = curve2.x1*(1 - t)*(1 - t)*(1 - t) + 3*curve2.ctrlx1*(1 - t)*(1 - t)*t + 3*curve2.ctrlx2*(1 - t)*t*t + curve2.x2*t*t*t;
double prevy = curve2.y1*(1 - t)*(1 - t)*(1 - t) + 3*curve2.ctrly1*(1 - t)*(1 - t)*t + 3*curve2.ctrly2*(1 - t)*t*t + curve2.y2*t*t*t;

for(t += 1.0/range; t < 1.0; t += 1.0/range) {

double x = curve2.x1*(1 - t)*(1 - t)*(1 - t) + 3*curve2.ctrlx1*(1 - t)*(1 - t)*t + 3*curve2.ctrlx2*(1 - t)*t*t + curve2.x2*t*t*t;
double y = curve2.y1*(1 - t)*(1 - t)*(1 - t) + 3*curve2.ctrly1*(1 - t)*(1 - t)*t + 3*curve2.ctrly2*(1 - t)*t*t + curve2.y2*t*t*t;

g2d.draw(new LineExtended(prevx, prevy, x, y));

prevx = x;
prevy = y;

}

}

public void drawDistanceFunction(Graphics2D g2d) {

double a = (curve1.y2 - curve1.y1)/(curve1.x2 - curve1.x1);
double b = -1;
double c = -(a*curve1.x1 - curve1.y1);

double scale = Math.sqrt(a*a + b*b);

a /= scale;
b /= scale;
c /= scale;

double y1 = a*curve2.x1 + b*curve2.y1 + c;
double y2 = a*curve2.ctrlx1 + b*curve2.ctrly1 + c;
double y3 = a*curve2.ctrlx1 + b*curve2.ctrly2 + c;
double y4 = a*curve2.x2 + b*curve2.y2 + c;

double range = 200;
double t = 0;
double prevx = t*range;
double prevy = (1 - t)*(1 - t)*(1 - t)*y1 + 3*(1 - t)*(1 - t)*t*y2 + 3*(1 - t)*t*t*y3 + t*t*t*y4;

for(t += 1.0/range; t < 1.0; t += 1.0/range) {

double x = t*range;
double y = (1 - t)*(1 - t)*(1 - t)*y1 + 3*(1 - t)*(1 - t)*t*y2 + 3*(1 - t)*t*t*y3 + t*t*t*y4;

g2d.draw(new LineExtended(prevx, prevy, x, y));

prevx = x;
prevy = y;

}
}



}[/CODE]

Where CubicCurveExtended and LineExtended are simple extensions of CubicCurve2D.Double and Line2D.Double. Curves 1 and 2 are rotated so that the slope of curve1's baseline is zero before they are passed into the constructor. The output for this panel for curve1 defined as (485, 430, 580, 60, 430, 115, 530, 160) and curve2 (575, 25, 455, 60, 541, 140, 486, 210) defined as is shown below (the distance function is the curve in the upper left corner):

[img]http://i.imgur.com/GqYnh.png[/img]

As you can see, the distance function is nowhere near the two curves, despite both clearly intersecting. I recognize I may need to calculate the x values at intervals of the baseline rather than the function itself, but what I'm really confused about is the y values. I've been working at this for a while with no luck. If someone could explain to me why the y values are so far off, I'd be extremely grateful. Sorry if this is in the incorrect forum or the question is generally not suitable for this site.
0

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