Created
June 14, 2015 21:49
-
-
Save sbarisic/0dd47951ef31b1016fef to your computer and use it in GitHub Desktop.
Polycode demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Polycode.h> | |
#include <Polycode2DPhysics.h> | |
#include "Polyspace.h" | |
#include "PolycodeView.h" | |
#include "stdafx.h" | |
using namespace Polycode; | |
POLYCODE_CORE* core; | |
PhysicsScene2D* PhyScene; | |
Entity* LastEnt; | |
bool Update() { | |
if ((KeyDown(PolyKEY::KEY_LALT) && KeyDown(PolyKEY::KEY_F4)) || KeyDown(PolyKEY::KEY_ESCAPE)) | |
core->Shutdown(); | |
if (KeyDown(PolyKEY::KEY_F1)) | |
core->captureMouse(true); | |
else if (KeyDown(PolyKEY::KEY_F2)) | |
core->captureMouse(false); | |
if (MouseDown(CoreInput::MOUSE_BUTTON1)) { | |
Ray MouseRay = PhyScene->projectRayFromCameraAndViewportCoordinate(PhyScene->getDefaultCamera(), | |
CoreService->getInput()->getMousePosition()); | |
if (LastEnt == NULL) { | |
Entity* Ent = PhyScene->getEntityAtPosition(MouseRay.origin.x, MouseRay.origin.y); | |
if (Ent != NULL) { | |
String EntName = Ent->getTagAtIndex(0); | |
if (EntName.length() > 0) { | |
printf("Grabbed entity %s\n", EntName.c_str()); | |
LastEnt = Ent; | |
Ent->setColor(1, 1, 1, 1); | |
PhyScene->getPhysicsByEntity(Ent)->setLinearDamping(2); | |
} | |
} | |
} else { | |
PhysicsScene2DEntity* PhysEnt = PhyScene->getPhysicsByEntity(LastEnt); | |
Vector2 MousePos = Vector2(MouseRay.origin.x, MouseRay.origin.y); | |
Vector2 EntPos = LastEnt->getPosition2D(); | |
Number Angle = Vector2(0, 0).angle(EntPos - MousePos) + PI; | |
Number PullPower = pow(EntPos.distance(MousePos) + 4, 2); | |
//printf("Angle: %f Ent(%f, %f) Mouse(%f, %f)\n", Angle * TODEGREES, EntPos.x, EntPos.y, MousePos.x, MousePos.y); | |
PhysEnt->applyForce(Vector2(cos(Angle) * PullPower, sin(Angle) * PullPower)); | |
} | |
} else if (LastEnt != NULL) { | |
LastEnt->setColor(0.5, 0.5, 0.5, 1); | |
PhyScene->getPhysicsByEntity(LastEnt)->setLinearDamping(0); | |
LastEnt = NULL; | |
} | |
return core->updateAndRender(); | |
} | |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { | |
PolycodeView* View = new PolycodeView(hInstance, nCmdShow, L"Polyspace", false, true); | |
core = new Win32Core(View, 800, 600, false, true, 4, 4, 60); | |
log("Focusing window", CenterAndFocus(View->hwnd)); | |
log("Creating physics scene", { | |
PhyScene = new PhysicsScene2D(0.1, 60); | |
PhyScene->clearColor = Color(42, 42, 42, 255); | |
PhyScene->useClearColor = true; | |
}); | |
ScenePrimitive* Shape; | |
log("Creating floor", { | |
Shape = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 1.0, 0.05); | |
Shape->setColor(0.0, 0.0, 0.1, 1.0); | |
Shape->setPosition(0, -0.4); | |
PhyScene->addPhysicsChild(Shape, PhysicsScene2DEntity::ENTITY_RECT, true); | |
}); | |
log("Creating physics boxes", { | |
for(int i = 0; i < 20; i++) { | |
Shape = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE, 0.05, 0.05); | |
Shape->setRoll(rand() % 360); | |
Shape->setColor(0.5, 0.5, 0.5, 1); | |
Shape->setPosition(-0.3 + (RANDOM_NUMBER * 0.6), RANDOM_NUMBER); | |
Shape->addTag(String("Entity_") + String(String::IntToString(i))); | |
PhyScene->addPhysicsChild(Shape, PhysicsScene2DEntity::ENTITY_RECT, false, 0.9); | |
} | |
}); | |
MSG Msg; | |
do { | |
while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) { | |
TranslateMessage(&Msg); | |
DispatchMessage(&Msg); | |
} | |
core->Render(); | |
} while (Update()); | |
log("Shutting down core", core->Shutdown()); | |
return Msg.wParam; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment