0 votes

Hello, I've only been using Godot for about a week now (so complete beginner) and I have a scene that looks like this:

Node 2D (script attached to it)
-- Timer
-- Area2D
----CollisionShape2D

(+ other unimportant stuff)

I need the CollisionShape2D to be a ConvexPolygoneShape2D, however you can only set its points in code... I would like to do this in the script attached to the Node2D, but once I got to it I realized there were no tutorials on how to set this up and I've tried for several hours without success.

I would like to know how to set the points for the ConvexPolygoneShape2D in code (I am using gdscript), and as a bonus, I would like the points of the ConvexPolygonShape2D to change over time (which is why there is a Timer and the script is attached to the Node2D).

in Engine by (15 points)

1 Answer

+1 vote
Best answer

Looks like you need to:

  • Create a new ConvexPolygonShape2D
  • Assign points to the new polygon
  • Assign the polygon to your CollisionShape2D object

Here's an example...

func _ready():
    var collShape = $Area2D/CollisionShape2D
    var polygon = ConvexPolygonShape2D.new()
    var pointArr = PoolVector2Array()
    pointArr.append(Vector2(-32,-32))
    pointArr.append(Vector2(-32, 32))
    pointArr.append(Vector2(32, 32))
    pointArr.append(Vector2(32, -32))
    polygon.points = pointArr
    collShape.shape = polygon
by (13,682 points)
selected by

Thank you so much for both the answer and the example! Now it works exactly as I imagined it would.

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.