Search code examples
javaslick2d

How to create an arc with Slick2D?


I cant seem to figure out how to create an arc with Slick2D. Basically I want to be able to save it as an object so I can do collicion detection with it, but Slick2D only has a g.drawArc() function.

I thought about using Arc2D, which is perfect for what I need, but I can't figure out how to get slick to draw the Arc2D.

Heres is a mockup of what I want to have:

mockup

This is pretty easy to do with Arc2D and then using jFrame to draw it, but I dont know how to create something like this with slick.

Edit: The solution I see that I can do, is just use Arc2D for all the collision detection, and then just use myArc.x, myArc.y, myArc.width, etc. when I need to draw the arc with g.drawArc(), but Arc2D cant use the .interesects() function with slick2d shapes, which are used everywhere else.

Ive created a method that creates an approximate arc based on a curve

public Curve createArc(float centerX, float centerY, float radius, float theta){
        // creates a 180 degree arc around centerX and Y, theta (in  degrees) is used for rotation

    float x1,x2,x3,x4,y1,y2,y3,y4;

    x1 = (float) (radius*(1-Math.cos(Math.toRadians(theta)))) + startX - radius;
    y1 = (float) (radius*(1-Math.sin(Math.toRadians(theta)))) + startY - radius;

    x2 = (float) (Math.sqrt(25*(radius*radius)/9)*(Math.cos(Math.toRadians(theta-36.87-90))))  + startX;
    y2 = (float) (Math.sqrt(25*(radius*radius)/9)*(Math.sin(Math.toRadians(theta-36.87-90)))) + startY;

    x3 = (float) (Math.sqrt(25*(radius*radius)/9)*(Math.cos(Math.toRadians(theta-53.13))))  + startX;
    y3 = (float) (Math.sqrt(25*(radius*radius)/9)*(Math.sin(Math.toRadians(theta-53.13)))) + startY;

    x4 = (float) (radius*(1-Math.cos(Math.toRadians(theta+180)))) - radius + startX;
    y4 = (float) (radius*(1-Math.sin(Math.toRadians(theta+180)))) - radius + startY;



    Vector2f vectorTest1 = new Vector2f(x1,y1);
    Vector2f vectorTest2 = new Vector2f(x2,y2);
    Vector2f vectorTest3 = new Vector2f(x3,y3);
    Vector2f vectorTest4 = new Vector2f(x4,y4);


    Curve curve = new Curve(vectorTest1,vectorTest2,vectorTest3,vectorTest4);

    return curve;
}

Solution

  • Hey you can create a variety of different shapes with Slick2D. You can see them here:

    http://slick.ninjacave.com/javadoc/org/newdawn/slick/geom/package-summary.html

    They all inherit from shape and share the same methods (e.g. intersects());

    There is no specific arc shape but you might be able to use a curve or maybe a polygon (or something else). So for example you simply could do:

        org.newdawn.slick.geom.Curve myCurve = new org.newdawn.slick.geom.Curve(10f, 5f, 9f, 4f);
    

    to create a weird looking curve. Just read the documentation and play a little bit around.

    In your render method you could either call g.drawArc(myCurve) or you call the drawing method on myCurve directly (myCurve.draw());

    I'm sure you will be able to do this if you read the documentation and play a little bit around with the coordinates.

    Furthermore you could create your own Shapes in Slick2D if you create a new class and let it inherit from Shape.