Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proof-of-concept for Physics.simulate() for discussion purposes #49976

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions core/physics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*************************************************************************/
/* physics.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#include "core/physics.h"

#include "core/engine.h"
#include "core/message_queue.h"
#include "scene/main/scene_tree.h"

Physics *Physics::singleton = NULL;

Physics::Physics() {
ERR_FAIL_COND(singleton != NULL);
singleton = this;
}

Physics::~Physics() {
singleton = NULL;
}

Physics *Physics::get_singleton() {
return singleton;
}

void Physics::_bind_methods() {
ClassDB::bind_method(D_METHOD("simulate"), &Physics::simulate);
}

void Physics::simulate() {
MessageQueue::get_singleton()->flush();

// Copy from Nodes to the objects inside the physics server.
SceneTree::get_singleton()->flush_transform_notifications();

// Run the physics tick.
int physics_fps = Engine::get_singleton()->get_iterations_per_second();
float frame_slice = 1.0 / physics_fps;
float time_scale = Engine::get_singleton()->get_time_scale();
PhysicsServer::get_singleton()->step(frame_slice * time_scale);
Physics2DServer::get_singleton()->step(frame_slice * time_scale);

MessageQueue::get_singleton()->flush();

// Update the colliding bodies and areas.
PhysicsServer::get_singleton()->flush_queries();
Physics2DServer::get_singleton()->flush_queries();
}
54 changes: 54 additions & 0 deletions core/physics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*************************************************************************/
/* physics_server.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/

#ifndef PHYSICS_H
#define PHYSICS_H

#include "object.h"

class Physics : public Object {

GDCLASS(Physics, Object);

static Physics *singleton;

protected:
static void _bind_methods();

public:
static Physics *get_singleton();

void simulate();

Physics();
~Physics();
};

#endif
8 changes: 8 additions & 0 deletions core/register_core_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#include "core/os/main_loop.h"
#include "core/packed_data_container.h"
#include "core/path_remap.h"
#include "core/physics.h"
#include "core/project_settings.h"
#include "core/translation.h"
#include "core/undo_redo.h"
Expand All @@ -88,6 +89,7 @@ static _Marshalls *_marshalls = nullptr;
static _JSON *_json = nullptr;

static IP *ip = nullptr;
static Physics *physics = nullptr;

static _Geometry *_geometry = nullptr;

Expand Down Expand Up @@ -212,6 +214,7 @@ void register_core_types() {
ClassDB::register_virtual_class<ResourceImporter>();

ip = IP::create();
physics = memnew(Physics);

_geometry = memnew(_Geometry);

Expand Down Expand Up @@ -250,6 +253,7 @@ void register_core_singletons() {
ClassDB::register_class<InputMap>();
ClassDB::register_class<_JSON>();
ClassDB::register_class<Expression>();
ClassDB::register_class<Physics>();

Engine::get_singleton()->add_singleton(Engine::Singleton("ProjectSettings", ProjectSettings::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("IP", IP::get_singleton()));
Expand All @@ -264,6 +268,7 @@ void register_core_singletons() {
Engine::get_singleton()->add_singleton(Engine::Singleton("Input", Input::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("InputMap", InputMap::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("JSON", _JSON::get_singleton()));
Engine::get_singleton()->add_singleton(Engine::Singleton("Physics", Physics::get_singleton()));
}

void unregister_core_types() {
Expand Down Expand Up @@ -300,6 +305,9 @@ void unregister_core_types() {
if (ip) {
memdelete(ip);
}
if (physics) {
memdelete(physics);
}

ResourceLoader::finalize();

Expand Down