0 votes

Hey I want to create a circle shaped polygon2D and I will be putting textures on it. However I can not find any information on how to do so. I want a circle object like wheel that will roll and will change it's texture for wheel and other stuff. How can I achieve this thank you all. Also 1 method I tried is shaping a circle sprite in to polygon2d but it reduces great performance and it does not result in an actual circle instead a 1500~edged polygon.

in Engine by (16 points)

3 Answers

–1 vote

If you use CollisionShape2D, you can selected a CircleShape2d

by (49 points)

Yes I know that, but I am trying to achieve a circle texture base that I will be putting on textures. Is there a way to put textures on CollisionShape2D ?

0 votes

so the numSides is the number of sides/faces and the perimeter length is the length of the perimeter.

func generatePolygonV2(numSides, perimeterLength):
var lineLength : float = perimeterLength/numSides
var lineAngle : float = -360.0000000000/numSides
var currentAngle : float = 0.0000000000

var newPolygon = Polygon2D.new()
var polygonVectors : PoolVector2Array

for i in numSides:
    var newVector

    if i == 0:
        currentAngle += lineAngle
        newVector = Vector2(lineLength * cos(deg2rad(currentAngle)),lineLength * sin(deg2rad(currentAngle)))
        polygonVectors.append(newVector)            
    else:
        currentAngle += lineAngle
        newVector = Vector2(polygonVectors[i-1].x + lineLength * cos(deg2rad(currentAngle)),polygonVectors[i-1].y + lineLength * sin(deg2rad(currentAngle)))
        polygonVectors.append(newVector)

newPolygon.polygon = polygonVectors
newPolygon.color = Color(1,0,1)
node2d.add_child(newPolygon)
polygon = newPolygon

something like this? The only problem with my method is the polygon generated is not centered. I have an idea on how i could fix that but yeah i'm lazy.

by (14 points)
0 votes

Here is some code that would generate a circle polygon:

func generate_circle_polygon(radius: float, num_sides: int, position: Vector2) -> PoolVector2Array:
    var angle_delta: float = (PI * 2) / num_sides
    var vector: Vector2 = Vector2(radius, 0)
    var polygon: PoolVector2Array

    for _i in num_sides:
        polygon.append(vector + position)
        vector = vector.rotated(angle_delta)

    return polygon

This would generate a polygon circle of a certain radius with a certain num_sides with position as the center point.

by (22 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.