2d - Collision Polygons and Gravity

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Judd Gledhill
:warning: Old Version Published before Godot 3 was released.

Hello,

I am working to learn the Godot engine. I am new to games in general, but like what I have seen. Anyway, in an effort to learn, I am making some basic games. The first of which is moon lander (an oldie but a goodie). I am hoping to learn how to address things on screen, move between game states, figure out procedural terrain etc.

Which type of collision body should I be using for the lander itself? I am hoping to have gravity exerted on the “sprite” without having to write all that stuff. I do, however, want to have the player be able to apply thrust to the sprite to stop it from crashing.

Right not I am using a CollisionPolygon2d node in my scene. My ship scene looks like this:

Node2D     (just a way to group)
- CollisionPolygon2d (the collision envelope)
- Sprite (the visual on the screen)
  1. Am I doing this node hierarchy correctly? It’s a bit weird this way, but I think it is what I am supposed to do.

  2. How do I apply gravity forces at the containing “Node2d” so all of my stuff moves together? Should I be using a different set of nodes?

Thanks,

Judd

One thing I know that is you should replace Node2D with RigidBody2D or KinematicBody2D to work properly.

volzhs | 2016-08-08 18:28

:bust_in_silhouette: Reply From: ericdl

Replace Node2D with RigidBody2D. Your child nodes are correct. RigidBody2D has a Gravity Scale property which determines how strongly gravity will affect it.

Sample moon lander project attached: https://drive.google.com/file/d/0BwnfZQAEnciANzRCX1IwWjZKRTA/view?usp=sharing
Please excuse the programmer art.

Great, I will check it out.

Is there somewhere to read up about strategies for the node layering? I am still befuddled about how to organize my nodes and scenes.

Judd Gledhill | 2016-08-08 22:13

Thanks @ericdl that was exactly what I was looking for. You made the exact thing I was working towards! Based on the code and compactness of the solution, can I surmise that you have made games before and are familiar with using things like a physics engine?

I am just guessing given that I had no idea how to make something like you put together… The event handling alone was interesting. Thank you again.

Judd Gledhill | 2016-08-08 22:25

I don’t think there is a guide to node layering or anything like that, as far as I can tell it is mostly down to trial & error, asking questions here on QA and Reddit and the IRC, and following tutorials. Luckily there are many friendly and helpful folk around to lend a hand if you ever need it.

Since you asked, I dabble here & there with making games with Godot and Unity, but I enjoy answering questions here on the QA board because it helps sharpen my own skills and gives me a chance to learn something new.

Stick around Godot long enough and you will get the hang of it, the developers have done a wonderful job of simplifying pretty much everything.

ericdl | 2016-08-09 01:28

I am working up to an idea, so I will be around.

Speaking of that, got any 2d examples that show infinite terrain generation? Simple stuff, just looking for the approach around doing stuff like that. I am trying to understand what moves in a game like an infinite runner. Is the player moving with a “camera” fixed to his location, is the terrain moving and the player is standing still? Stuff like that.

Judd Gledhill | 2016-08-09 17:51

Unfortunately I don’t yet have any experience with procedurally generated terrain.

An infinite runner could be made either way you described, my guess is it would depend on the mechanics of your game. You could attach a camera to the player and then allow movement through a level that has either been procedurally-generated or pre-assembled. Pre-assembly could be a level you built in the Godot editor (not really infinite), or chunks of pre-built scenes that are instantiated and assembled at runtime. Some thoughts on level construction can be found here. You might also look into utilizing tilesets and parallax background layers.

ericdl | 2016-08-10 05:00

So I am finally digging into some of this stuff. I started by revisiting your code. Specifically the particles associated with your moon lander object. You instance the lander, and inside of that you instance the particles themselves.

I notice, in that simple code you have on the RigidBody2d node that you get a reference to the particle scene itself, but then do not appear to use it anywhere. Why do you do that?

var fire = preload("res://fire_particles.tscn")     <- this line?
var firenode = null

Is that just for expediency? Does the engine cache it at that point, but you do not have to assign it to anything to make use of it?

Thanks,

J

Judd Gledhill | 2016-08-28 20:15

Looking back at the code, var fire = preload("res://fire_particles.tscn") should have been deleted, so i apologize for the confusion. In it’s current form, it serves no purpose.

I think the original intent was to instance the particles via code, hence the reason the particles were preloaded into var fire. However at some point the particles node was simply added to the lander’s rigidbody2d node, and those particles were accessed via the line firenode = self.get_node("Particles2D"). Basically the two ideas are redundant, and the project ends up using firenode instead of the preloaded var fire.

ericdl | 2016-08-29 13:07

Excellent, that is how I read the code as well. Godot can be a little confusing from a referencing standpoint. You look at the code and it refers to something in the structure of the scene that is hidden in the instancing. So just a little weird.

For instance, you have the root node of that scene as “RigidBody2d” and you are instancing that it. And the particles node is part of that scene - which you refer to in the code at that top level. So it took some tracing to figure out even that stuff. It is just different enough to be confusing, but not different enough to say “it’s hard to figure out”.

Thanks again. Time for terrain tutorials.

J

Judd Gledhill | 2016-08-29 13:25

Hey Ericdl, I am finally getting around to making “Cave Lander”. My toy project to learn Godot. I wanted to make sure that it was ok with you if I based some of it off of the code you pointed me to - in particular I really like those particles you made. For now I am also using your cool ship as a stand in (did you make that or get it from somewhere)?

Judd Gledhill | 2016-10-01 13:07

Please feel free to use anything you like from that example project. And I’d love to see your Cave Lander game in action someday if you ever get around to releasing it!

As for the lander ship, I just quickly drew it as a placeholder graphic instead of spending time searching for something better on OpenGameArt.org or similar.

ericdl | 2016-10-02 03:59