libgdx collision with line and polygon (Intersector) - java

My libgdx game has a laser and I want to know if the beam hits it's target.
I had some trouble with the function (always getting true) so I made a very simple test and I still get true!
Did I miss something?
Why is this function returning true?
if(Intersector.intersectLinePolygon(new Vector2(100, 100), new Vector2(200, 100), new Polygon(new float[] {0, 0, 5, 0, 5, 5}))) {
System.out.println("true");
}
Thanks in advance!

I got caught by this when I first used Intersector too.
The intersectLinePolygon() method works on a line that extends infinitely, and not just between the two points you specify.
Use the intersectSegmentPolygon() method which does what you want...
if(Intersector.intersectSegmentPolygon(new Vector2(100, 100), new Vector2(200, 100), new Polygon(new float[] {0, 0, 5, 0, 5, 5}))) {
System.out.println("true");
}

Related

Translate C++ code to Java with Mat object

I have working code in Visual Studio with C ++ on a watershed algorithm and I need to pass it to Android Studio. I have added the OpenCV library to the app and the simple operations work. Now, my problem is that when I want to pass the code to Java there is a specific line type that I don't know how to pass. The part of the code is:
cv::Mat markers(original.size(), CV_8U, cv::Scalar(-1));
//Rect(topleftcornerX, topleftcornerY, width, height);
//top rectangle
markers(Rect(0, 0, original.cols, 5)) = Scalar::all(1);
//bottom rectangle
markers(Rect(0, original.rows - 5, original.cols, 5)) = Scalar::all(1);
//left rectangle
markers(Rect(0, 0, 5, original.rows)) = Scalar::all(1);
//right rectangle
markers(Rect(original.cols - 5, 0, 5, original.rows)) = Scalar::all(1);
What would be the creation of the object would be something like this:
Mat markers = new Mat(rgba.size(), CvType.CV_8UC3, new Scalar(-1));
But the rest of the lines do not know how they would be transformed. Help please?
markers.submat(0, 0, original.cols, 5).setTo(new Scalar(1, 1, 1));
or
Imgproc.rectangle (
markers,
new Point(0, 0),
new Point( original.cols, 5),
new Scalar(1, 1, 1),
-1
);

Flickering on simple triangle render in openGL

Our current assignment requires us to use older fixed-pipeline methods in openGL. We're using LWJGL 2.9.3. The following code displays a triangle. The problem is, it flickers like crazy. The Display.swapBuffers() method doesn't throw an exception, and it doesn't make any difference if I include it or not. I created this example based off this StackOverflow question:
gluPerspective, glViewport, gluLookAt and the GL_PROJECTION and GL_MODELVIEW Matricies
glViewport(0, 0, Display.getWidth(), Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 10, 0, 10, -1, 100);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
gluLookAt(0, 0, 0, 0, 0, 1, 0, 1, 0);
glBegin(GL_POLYGON);
glVertex3d(1, 1, 1);
glVertex3d(1, 5, 1);
glVertex3d(5, 5, 1);
glEnd();
glFlush();
try {
Display.swapBuffers();
} catch (Exception e) {
e.printStackTrace();
}
EDIT
One other thing. If I call glLoadIdentity() after glMatrixMode(GL_MODELVIEW), it's as if the gluLookAt() method is ignored. I just see a blank black screen if I do that. But if I don't, it keeps multiplying with itself (if I change the eye position from 0,0,0).
The reason for the flickering was that the gluLookAt() function was swapping back and forth between looking different directions.

How to tell where the texture is drawn in OpenGL / libgdx?

How do we know which texture is related to which mesh in OpenGL? In this example, we tell the mesh we use texture coordinates, but we don't say which texture (if we have more than one), and we don't tell the texture where to be drawn. How does it work? (I know the concept of UVs, but I don't know the concept of "where" the texture is drawn) :
mesh = new Mesh(true, 4, 6,
new VertexAttribute(VertexAttributes.Usage.Position, 3,"attr_Position"),
new VertexAttribute(Usage.TextureCoordinates, 2, "attr_texCoords"));
texture = new Texture(Gdx.files.internal("data/img.png"));
mesh.setVertices(new float[] {
-1024f, -1024f, 0, 0, 1,
1024f, -1024f, 0, 1, 1,
1024f, 1024f, 0, 1, 0,
-1024f, 1024f, 0, 0, 0
});
#Override
public void render() {
// Texturing --------------------- /
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glEnable(GL10.GL_TEXTURE_2D);
texture.bind();
mesh.render(GL10.GL_TRIANGLES);
}
OK I actually found the solution : https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/DecalTest.java

Find circles image processing

I'm using opencv and java to find circles on an image, I have the image below so far.
I'm using Hough to find the circles with the code like this:
Imgproc.medianBlur(result, result, 3);
Imgproc.medianBlur(result, result, 3);
Imgproc.medianBlur(result, result, 3);
Mat circles = new Mat();
Imgproc.HoughCircles(result, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 1, 200, 100, 30, 40);
System.out.println(circles.dump());
But I get an empty Mat for result with and without the blur.
How should I fix this code?
EDIT :
Hi guys!
Thanks to you I have this picture now. I'm using these parameters:
Imgproc.HoughCircles(result, circles, Imgproc.CV_HOUGH_GRADIENT, 1, 20, 50, 10, 10, 40);
I'm still using the medianBlur before the detection.
The only question left is why does it detect these small circles? I've attached the result of the canny detection, I think the circles are pretty seeable .
First of all, the circles structure should not be cv::Mat but should be std::vector<cv::Vec3f>; I think that is why you aren't getting any results.. Please refer to the documentation on the HoughCircles for details..
Playing around with the values for 5 minutes, I have this starting point for you:
The parameters I used are,
cv::medianBlur(test_Circle, test_Circle, 7);
std::vector<cv::Vec3f> circles; // <- not that "circles" is not cv::Mat
cv::HoughCircles(test_Circle, circles, CV_HOUGH_GRADIENT, 1, 1, 300, 10, 10, 50);
You can get much more defined result after you played around with the values a bit.
PS - Since I am a C++ user, please excuse me for putting all my structures in that format. You can easily extend the logic to Java. :)
Are you sure you are providing radius and not diameter? Try wider range of radiuses (10-100 for example).
Using OpenCV to cheat in Zuma? :)
I've tested my code, it writtent in C# (i think java is the same) and get the result:1
You can find my code in HoughAlgorithm.cs class
My demo Project Here
//DP_Resolution: 1
//MinDistance :32
//CannyThreshold: 10
//AccuThreshold: 10
//MinRadius: 13
//MaxRadius: 20
public static CvSeq DetectCircles(IplImage pImage, CamEnum _camName)
{
try
{
CvMemStorage memStorage = cvlib.CvCreateMemStorage(0);
return cvlib.CvHoughCircles(ref pImage, memStorage.ptr, 3, 1, 32, 10, 10, 13, 20);
}
catch
{
throw;
}
}
http://i.stack.imgur.com/pUqbh.png

Java: gc.drawimage and transparency

I am creating custom SWT widget but I have got problem with transparency. My class extends canvas, I have got png with alpha image in my resources, when I simply write:
this.setBackgroundImage(Colors.getMenuButton()); //getMenuButton returns Image object
everything works ok (with transparency), but my object has to be resizeable so I decided to create funcion:
protected Image BGHelper(Image src) {
Image i2 = new Image(Display.getDefault(),2,26);
GC gc2 = new GC(i2);
Image image = new Image(Display.getDefault(),this.getBounds().width,26);
GC gc = new GC(image);
gc.drawImage(src, 0, 0, 3, 25, 0, 0, 3, 26);
gc2.drawImage(src, 3, 0, 2, 25, 0, 0, 2, 26);
gc.drawImage(i2, 0, 0, 2, 25, 3, 0, this.getBounds().width-6, 26);
gc.drawImage(src,53, 0, 3, 26, this.getBounds().width-3, 0, 3, 26);
gc.dispose();
gc2.dispose();
return image;
}
it cuts left border from source and paste into result, cut center from source, resize and paste into result, cut right border from source and paste into result. Resizing works but there is no transparency (white pixel). Why?
please look at following doc.It might help you to solve your issue.
http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html#Transparency

Categories

Resources