Merge pull request #395 from aismann/Chipmunk2D

Improve Chipmunk2D -TestBed and DebugDraw too
This commit is contained in:
halx99 2021-06-24 18:02:56 +08:00 committed by GitHub
commit fe6d6794f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 151 additions and 539 deletions

View File

@ -1,6 +1,7 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -49,6 +50,25 @@ Vec2 physicsDebugNodeOffset;
as the private API may change with little or no warning.
*/
static const cpVect spring_verts[] = {
{0.00f, 0.0f},
{0.20f, 0.0f},
{0.25f, 3.0f},
{0.30f, -6.0f},
{0.35f, 6.0f},
{0.40f, -6.0f},
{0.45f, 6.0f},
{0.50f, -6.0f},
{0.55f, 6.0f},
{0.60f, -6.0f},
{0.65f, 6.0f},
{0.70f, -3.0f},
{0.75f, 6.0f},
{0.80f, 0.0f},
{1.00f, 0.0f},
};
static const int spring_count = sizeof(spring_verts) / sizeof(cpVect);
static Color4F ColorForBody(cpBody *body)
{
if (CP_BODY_TYPE_STATIC == cpBodyGetType(body) || cpBodyIsSleeping(body))
@ -154,11 +174,35 @@ static void DrawConstraint(cpConstraint *constraint, DrawNode *renderer)
}
else if(cpConstraintIsDampedSpring(constraint))
{
// TODO: uninplemented
cpDampedSpring* spring = (cpDampedSpring*) constraint;
cpVect a = cpTransformPoint(body_a->transform, spring->anchorA);
cpVect b = cpTransformPoint(body_b->transform, spring->anchorB);
renderer->drawDot(cpVert2Point(a), 3.0, CONSTRAINT_COLOR);
renderer->drawDot(cpVert2Point(b), 3.0, CONSTRAINT_COLOR);
cpVect delta = cpvsub(b, a);
cpFloat cos = delta.x;
cpFloat sin = delta.y;
cpFloat s = 1.0f / cpvlength(delta);
cpVect r1 = cpv(cos, -sin * s);
cpVect r2 = cpv(sin, cos * s);
cpVect* verts = (cpVect*) alloca(spring_count * sizeof(cpVect));
for (int i = 0; i < spring_count; i++) {
cpVect v = spring_verts[i];
verts[i] = cpv(cpvdot(v, r1) + a.x, cpvdot(v, r2) + a.y);
}
for (int i = 0; i < spring_count - 1; i++) {
renderer->drawSegment(cpVert2Point(verts[i]), cpVert2Point(verts[i+1]), 1.0, CONSTRAINT_COLOR);
}
}
else
{
// printf("Cannot draw constraint\n");
CCLOG("Cannot draw constraint");
}
}

View File

@ -1,6 +1,7 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,7 +1,8 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
*
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* 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

View File

@ -1,6 +1,7 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,6 +1,7 @@
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
* Copyright (c) 2012 cocos2d-x.org
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal

View File

@ -1,7 +1,5 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@ -52,12 +50,14 @@ cpBool ChipmunkDemoRightClick;
cpBool ChipmunkDemoRightDown;
cpBool ChipmunkDemoLeftDown = cpFalse;
cpBody* mouse_body = cpBodyNewKinematic();
cpConstraint* mouse_joint = NULL;
char const* ChipmunkDemoMessageString = NULL;
float ChipmunkDebugDrawPointLineScale = 1.0f;
static size_t VertexCount, IndexCount;
#define GRABBABLE_MASK_BIT (1 << 31)
cpShapeFilter GRAB_FILTER = {CP_NO_GROUP, GRABBABLE_MASK_BIT, GRABBABLE_MASK_BIT};
@ -70,10 +70,50 @@ void ChipmunkDemoDefaultDrawImpl(cpSpace* space){};
void ChipmunkDebugDrawDot(cpFloat size, cpVect pos, cpSpaceDebugColor fillColor){};
void ChipmunkDebugDrawSegment(cpVect a, cpVect b, cpSpaceDebugColor color){};
void ChipmunkDebugDrawFatSegment(
cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor){};
void ChipmunkDebugDrawFatSegment(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor){};
void ChipmunkDebugDrawBB(cpBB bb, cpSpaceDebugColor outlineColor){};
void ChipmunkDemoPrintString(char const* fmt, ...){};
static Vertex* push_vertexes(size_t vcount, const Index* index_src, size_t icount){};
//// cpAssertHard(VertexCount + vcount <= VERTEX_MAX && IndexCount + icount <= INDEX_MAX, "Geometry buffer full.");
//
// Vertex* vertex_dst = Vertexes + VertexCount;
// size_t base = VertexCount;
// VertexCount += vcount;
//
// Index* index_dst = Indexes + IndexCount;
// for (size_t i = 0; i < icount; i++)
// index_dst[i] = index_src[i] + (Index) base;
// IndexCount += icount;
//
// return vertex_dst;
//}
static char PrintStringBuffer[1024 * 8];
static char* PrintStringCursor;
cocos2d::Label* label;
void ChipmunkDemoPrintString(char const* fmt, ...) {
if (PrintStringCursor == NULL) {
return;
}
ChipmunkDemoMessageString = PrintStringBuffer;
if (ChipmunkDemoMessageString) {
label->setString(ChipmunkDemoMessageString);
ChipmunkDemoMessageString = "";
}
va_list args;
va_start(args, fmt);
int remaining = sizeof(PrintStringBuffer) - (PrintStringCursor - PrintStringBuffer);
int would_write = vsnprintf(PrintStringCursor, remaining, fmt, args);
if (would_write > 0 && would_write < remaining) {
PrintStringCursor += would_write;
} else {
// encoding error or overflow, prevent further use until reinitialized
PrintStringCursor = NULL;
}
va_end(args);
}
cpSpaceDebugColor RGBAColor(float r, float g, float b, float a) {
cpSpaceDebugColor color = {r, g, b, a};
@ -138,91 +178,61 @@ static Rect getRect(Node* node) {
}
ChipmunkTestBed::ChipmunkTestBed() {
// halx99: since adxe init scene default camera at 'initWithXXX' function, only change design sze at scene construct is ok
// see also: https://github.com/adxeproject/adxe/commit/581a7921554c09746616759d5a5ca6ce9d3eaa22
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
Size designSize(960 * 0.8, 640 * 0.8);
glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
//// Resize (expand) window
// static Size resourceSize(1280, 720);
// auto director = Director::getInstance();
// GLViewImpl* view = (GLViewImpl*) Director::getInstance()->getOpenGLView();
// view->setWindowed(resourceSize.width, resourceSize.height);
// orgSize = view->getDesignResolutionSize();
// view->setDesignResolutionSize(480, 320, ResolutionPolicy::NO_BORDER);
// auto director = Director::getInstance();
// auto glview = director->getOpenGLView();
// Size designSize(960, 640);
// glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
// creating a keyboard event listener
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event) {
char buf[100] = {0};
sprintf(buf, "Key %d was pressed!", (int) keyCode);
switch ((int) keyCode) {
case 28:
case 28: // Up
ChipmunkDemoKeyboard.y++;
break;
case 29:
case 29: // Down
ChipmunkDemoKeyboard.y--;
break;
case 27:
case 27: // Right
ChipmunkDemoKeyboard.x++;
break;
case 26:
case 26: // Left
ChipmunkDemoKeyboard.x--;
break;
}
CCLOG("%s", buf);
};
listener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event) {
char buf[100] = {0};
sprintf(buf, "Key %d was released!", (int) keyCode);
auto label = static_cast<Label*>(event->getCurrentTarget());
CCLOG("%s", buf);
};
//
//
// Line 65 : cpVect ChipmunkDemoKeyboard;
// Line 444 : case SAPP_KEYCODE_UP : ChipmunkDemoKeyboard.y += (event->type == SAPP_EVENTTYPE_KEY_DOWN ? 1.0 :
// -1.0); break; Line 445 : case SAPP_KEYCODE_DOWN : ChipmunkDemoKeyboard.y += (event->type ==
// SAPP_EVENTTYPE_KEY_DOWN ? -1.0 : 1.0); break; Line 446 : case SAPP_KEYCODE_LEFT : ChipmunkDemoKeyboard.x +=
// (event->type == SAPP_EVENTTYPE_KEY_DOWN ? -1.0 : 1.0); break; Line 447 : case SAPP_KEYCODE_RIGHT :
// ChipmunkDemoKeyboard.x += (event->type == SAPP_EVENTTYPE_KEY_DOWN ? 1.0 : -1.0); break;
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
// creating a mouse event listener
_mouseListener = EventListenerMouse::create();
_mouseListener->onMouseMove = CC_CALLBACK_1(ChipmunkTestBed::onMouseMove, this);
_mouseListener->onMouseUp = CC_CALLBACK_1(ChipmunkTestBed::onMouseUp, this);
_mouseListener->onMouseDown = CC_CALLBACK_1(ChipmunkTestBed::onMouseDown, this);
_mouseListener->onMouseScroll = CC_CALLBACK_1(ChipmunkTestBed::onMouseScroll, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
// ChipmunkDemoMessageString
label = Label::createWithTTF("", "fonts/Marker Felt.ttf", 10.0f);
label->setPosition(VisibleRect::center().x, VisibleRect::top().y - 8);
label = Label::createWithTTF("", "fonts/Marker Felt.ttf", 16.0f);
label->setAnchorPoint(Vec2(0, 1));
label->setPosition(10, VisibleRect::top().y - 100);
label->setColor(Color3B::GREEN);
this->addChild(label, -1);
scheduleUpdate();
}
ChipmunkTestBed::~ChipmunkTestBed() {
ChipmunkDemoFreeSpaceChildren(_space);
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
// halx99: since we restore design resolution at BasetTest, this is not necessary.
// Size designSize(480, 320);
// glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
_eventDispatcher->removeEventListener(_mouseListener);
}
@ -242,6 +252,8 @@ void ChipmunkTestBed::update(float delta) {
#else
cpHastySpaceStep(_space, delta);
#endif
}
void ChipmunkTestBed::createResetButton() {
@ -257,11 +269,6 @@ void ChipmunkTestBed::reset(Ref* sender) {
}
void ChipmunkTestBed::onEnter() {
/*auto director = Director::getInstance();
auto glview = director->getOpenGLView();
Size designSize(960 * 0.8, 640 * 0.8);
glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);*/
TestCase::onEnter();
physicsDebugNodeOffset = VisibleRect::center();
ChipmunkDemoMessageString = "";
@ -292,13 +299,11 @@ void ChipmunkTestBed::onMouseDown(Event* event) {
cpSpaceAddConstraint(_space, mouse_joint);
}
} else if ((int) e->getMouseButton() == 1) {
if (mouse_joint) {
cpSpaceRemoveConstraint(_space, mouse_joint);
cpConstraintFree(mouse_joint);
mouse_joint = NULL;
}
ChipmunkDemoLeftDown = cpFalse;
ChipmunkDemoRightDown = cpTrue;
ChipmunkDemoRightClick = cpTrue;
@ -329,13 +334,6 @@ void ChipmunkTestBed::onMouseMove(Event* event) {
cpBodySetPosition(mouse_body, ChipmunkDemoMouse);
}
void ChipmunkTestBed::onMouseScroll(Event* event) {
EventMouse* e = (EventMouse*) event;
CCLOG("Mouse Scroll detected, X: %i ", e->getScrollX());
CCLOG("Mouse Scroll detected, Y: %i ", e->getScrollY());
}
//------------------------------------------------------------------
//
// LogoSmashDemo
@ -643,7 +641,6 @@ void SpringiesDemo::update(float delta) {
//------------------------------------------------------------------
void ShatterDemo::onEnter() {
ChipmunkTestBed::onEnter();
initPhysics();
}
@ -804,7 +801,6 @@ void PlatformerPlayerDemo::update(float delta) {
PlatformerPlayer.updateFunc(_space, PlatformerPlayer.timestep);
}
//------------------------------------------------------------------
//
// QueryDemo
@ -826,6 +822,8 @@ void QueryDemo::initPhysics() {
}
void QueryDemo::update(float delta) {
PrintStringBuffer[0] = 0;
PrintStringCursor = PrintStringBuffer;
Query.updateFunc(_space, Query.timestep);
}
@ -850,6 +848,8 @@ void ContactGraphDemo::initPhysics() {
}
void ContactGraphDemo::update(float delta) {
// PrintStringBuffer[0] = 0;
PrintStringCursor = PrintStringBuffer;
ContactGraph.updateFunc(_space, ContactGraph.timestep);
}

View File

@ -1,8 +1,6 @@
/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
* Copyright (c) 2021 @aismann; Peter Eismann, Germany; dreifrankensoft
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
@ -88,16 +86,12 @@ public:
void onMouseDown(cocos2d::Event* event);
void onMouseUp(cocos2d::Event* event);
void onMouseMove(cocos2d::Event* event);
void onMouseScroll(cocos2d::Event* event);
cpSpace* _space; // strong ref
cocos2d::Size orgSize;
private:
cocos2d::extension::PhysicsDebugNode* _debugLayer; // weak ref
cocos2d::EventListenerMouse* _mouseListener;
cocos2d::Label* label;
cocos2d::Node* _trackNode;
};
@ -291,12 +285,6 @@ public:
virtual void update(float dt) override;
};
class QueryDemo : public ChipmunkTestBed {
public:
CREATE_FUNC(QueryDemo);
@ -318,7 +306,6 @@ public:
virtual void update(float dt) override;
};
class BuoyancyDemo : public ChipmunkTestBed {
public:
CREATE_FUNC(BuoyancyDemo);
@ -329,7 +316,6 @@ public:
virtual void update(float dt) override;
};
class SliceDemo : public ChipmunkTestBed {
public:
CREATE_FUNC(SliceDemo);
@ -340,7 +326,6 @@ public:
virtual void update(float dt) override;
};
class UnicycleDemo : public ChipmunkTestBed {
public:
CREATE_FUNC(UnicycleDemo);
@ -351,10 +336,6 @@ public:
virtual void update(float dt) override;
};
class ExampleDemo : public ChipmunkTestBed {
public:
CREATE_FUNC(ExampleDemo);
@ -364,12 +345,6 @@ public:
void initPhysics() override;
virtual void update(float dt) override;
};
DEFINE_TEST_SUITE(ChipmunkTestBedTests);
#endif /* __CHIPMUNKTESTBED_H__ */

View File

@ -51,7 +51,26 @@ frand_unit_circle(){
return (cpvlengthsq(v) < 1.0f ? v : frand_unit_circle());
}
typedef struct {
float x, y;
} float2;
typedef struct {
uint8_t r, g, b, a;
} RGBA8;
typedef struct {
float2 pos;
float2 uv;
float r;
RGBA8 fill, outline;
} Vertex;
typedef uint16_t Index;
// Meh, just max out 16 bit index size.
#define VERTEX_MAX (64 * 1024)
#define INDEX_MAX (4 * VERTEX_MAX)
static Vertex Vertexes[VERTEX_MAX];
static Index Indexes[INDEX_MAX];
extern int ChipmunkDemoTicks;
@ -60,11 +79,10 @@ extern cpVect ChipmunkDemoKeyboard;
extern cpVect ChipmunkDemoMouse;
extern cpBool ChipmunkDemoRightClick;
extern cpBool ChipmunkDemoRightDown;
extern float ChipmunkDebugDrawPointLineScale;
extern char const *ChipmunkDemoMessageString;
void ChipmunkDemoPrintString(char const *fmt, ...);
extern void ChipmunkDemoPrintString(char const* fmt, ...);
extern cpShapeFilter GRAB_FILTER;
extern cpShapeFilter NOT_GRABBABLE_FILTER;

View File

@ -1,143 +0,0 @@
/* Copyright (c) 2007 Scott Lembcke
*
* 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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "chipmunk/chipmunk_private.h"
#include "ChipmunkDemo.h"
static cpBool NeverCollide(cpArbiter *arb, cpSpace *space, void *data){return cpFalse;}
static void
update(cpSpace *space)
{
int steps = 1;
cpFloat dt = 1.0f/60.0f/(cpFloat)steps;
for(int i=0; i<steps; i++){
cpSpaceStep(space, dt);
}
}
static cpSpace *
init(void)
{
cpSpace *space = cpSpaceNew();
cpSpaceSetIterations(space, 5);
cpSpaceSetDamping(space, 0.1f);
cpSpaceSetDefaultCollisionHandler(space, NeverCollide, NULL, NULL, NULL, NULL);
{
cpFloat mass = 1.0f;
cpFloat length = 100.0f;
cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f);
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
cpBodySetPos(body, cpv(-160.0f, -80.0f));
cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 30.0f));
}
{
cpFloat mass = 1.0f;
cpFloat length = 100.0f;
cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f);
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
cpBodySetPos(body, cpv(-160.0f, 80.0f));
cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 20.0f));
}
{
cpFloat mass = 1.0f;
const int NUM_VERTS = 5;
cpVect verts[NUM_VERTS];
for(int i=0; i<NUM_VERTS; i++){
cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
verts[i] = cpv(40*cos(angle), 40*sin(angle));
}
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
cpBodySetPos(body, cpv(-0.0f, -80.0f));
cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
}
{
cpFloat mass = 1.0f;
const int NUM_VERTS = 4;
cpVect verts[NUM_VERTS];
for(int i=0; i<NUM_VERTS; i++){
cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
verts[i] = cpv(60*cos(angle), 60*sin(angle));
}
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
cpBodySetPos(body, cpv(-0.0f, 80.0f));
cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
}
{
cpFloat mass = 1.0f;
cpFloat r = 60.0f;
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, INFINITY));
cpBodySetPos(body, cpv(160.0, -80.0f));
cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
}
{
cpFloat mass = 1.0f;
cpFloat r = 40.0f;
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, INFINITY));
cpBodySetPos(body, cpv(160.0, 80.0f));
cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
}
return space;
}
static void
destroy(cpSpace *space)
{
ChipmunkDemoFreeSpaceChildren(space);
cpSpaceFree(space);
}
ChipmunkDemo ContactPoints = {
"ContactPoints",
1.0/60.0,
init,
update,
ChipmunkDemoDefaultDrawImpl,
destroy,
};

View File

@ -156,7 +156,7 @@ destroy(cpSpace *space)
}
ChipmunkDemo Example = {
"Your Example from scratch",
"Your Example from scratch (Logo Smash)",
1.0/60.0,
init,
update,

View File

@ -1,148 +0,0 @@
/* Copyright (c) 2007 Scott Lembcke
*
* 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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "chipmunk_private.h"
#include "chipmunk_unsafe.h"
#include "ChipmunkDemo.h"
static cpShape *shape1, *shape2;
static void
update(cpSpace *space, cpFloat dt)
{
cpSpaceStep(space, dt);
}
static void
draw(cpSpace *space)
{
ChipmunkDemoDefaultDrawImpl(space);
struct cpContact arr[CP_MAX_CONTACTS_PER_ARBITER];
// cpCollideShapes(shape1, shape2, (cpCollisionID[]){0}, arr);
cpCollisionInfo info = cpCollideShapes(shape2, shape1, 0x00000000, arr);
}
static cpSpace *
init(void)
{
cpSpace *space = cpSpaceNew();
cpSpaceSetIterations(space, 5);
space->damping = 0.1;
cpFloat mass = 1.0f;
{
cpFloat size = 100.0;
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
cpBodySetPosition(body, cpv(100.0, 50.0f));
shape1 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
shape1->group = 1;
}{
cpFloat size = 100.0;
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size)));
cpBodySetPosition(body, cpv(120.0, -40.0f));
cpBodySetAngle(body, 1e-2);
shape2 = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0));
shape2->group = 1;
}
// {
// cpFloat size = 100.0;
// const int NUM_VERTS = 5;
//
// cpVect verts[NUM_VERTS];
// for(int i=0; i<NUM_VERTS; i++){
// cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
// verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle));
// }
//
// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
// cpBodySetPosition(body, cpv(100.0, 50.0f));
//
// shape1 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
// shape1->group = 1;
// }
// {
// cpFloat size = 100.0;
// const int NUM_VERTS = 4;
//
// cpVect verts[NUM_VERTS];
// for(int i=0; i<NUM_VERTS; i++){
// cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
// verts[i] = cpv(size/2.0*cos(angle), size/2.0*sin(angle));
// }
//
// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
// cpBodySetPosition(body, cpv(100.0, -50.0f));
//
// shape2 = cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
// shape2->group = 1;
// }
//
// {
// cpFloat size = 150.0;
// cpFloat radius = 25.0;
//
// cpVect a = cpv( size/2.0, 0.0);
// cpVect b = cpv(-size/2.0, 0.0);
// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
// cpBodySetPosition(body, cpv(0, 25));
//
// shape1 = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, radius));
// shape1->group = 1;
// }
// {
// cpFloat radius = 50.0;
//
// cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero)));
// cpBodySetPosition(body, cpv(0, -25));
//
// shape2 = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero));
// shape2->group = 1;
// }
return space;
}
static void
destroy(cpSpace *space)
{
ChipmunkDemoFreeSpaceChildren(space);
cpSpaceFree(space);
}
ChipmunkDemo GJK = {
"GJK",
1.0f/60.0f,
init,
update,
draw,
destroy,
};

View File

@ -140,7 +140,7 @@ init(void)
body->velocity_func = playerUpdateVelocity;
playerBody = body;
shape = cpSpaceAddShape(space, cpBoxShapeNew2(body, cpBBNew(-15.0, -27.5, 15.0, 27.5), 10.0));
shape = cpSpaceAddShape(space, cpBoxShapeNew2(body, cpBBNew(-15.0, -27.5, 15.0, 27.5), 1.0));
// shape = cpSpaceAddShape(space, cpSegmentShapeNew(playerBody, cpvzero, cpv(0, radius), radius));
shape->e = 0.0f; shape->u = 0.0f;
shape->type = 1;

View File

@ -30,7 +30,7 @@ static void
eachBody(cpBody *body, void *unused)
{
cpVect pos = cpBodyGetPosition(body);
if(pos.y < -260 || cpfabs(pos.x) > 340){
if(pos.y < -260){
cpFloat x = rand()/(cpFloat)RAND_MAX*640 - 320;
cpBodySetPosition(body, cpv(x, 260));
}
@ -80,10 +80,10 @@ init(void)
};
// Create the static triangles.
for(int i=0; i<9; i++){
for(int i=0; i<10; i++){
for(int j=0; j<6; j++){
cpFloat stagger = (j%2)*40;
cpVect offset = cpv(i*80 - 320 + stagger, j*70 - 240);
cpVect offset = cpv(i*80 - 360 + stagger, j*70 - 200);
shape = cpSpaceAddShape(space, cpPolyShapeNew(staticBody, 3, tris, cpTransformTranslate(offset), 0.0));
cpShapeSetElasticity(shape, 1.0f);
cpShapeSetFriction(shape, 1.0f);

View File

@ -38,7 +38,7 @@ update(cpSpace *space, double dt)
cpFloat radius = 10.0;
ChipmunkDebugDrawSegment(start, end, RGBAColor(0,1,0,1));
ChipmunkDemoPrintString("Query: Dist(%f) Point(%5.2f, %5.2f), ", cpvdist(start, end), end.x, end.y);
ChipmunkDemoPrintString("Query: Dist(%f) Point(%5.2f, %5.2f),\n", cpvdist(start, end), end.x, end.y);
cpSegmentQueryInfo segInfo = {0};
if(cpSpaceSegmentQueryFirst(space, start, end, radius, CP_SHAPE_FILTER_ALL, &segInfo)){
@ -55,9 +55,9 @@ update(cpSpace *space, double dt)
ChipmunkDebugDrawDot(3, point, RGBAColor(1,0,0,1));
ChipmunkDemoPrintString("Segment Query: Dist(%f) Normal(%5.2f, %5.2f)", segInfo.alpha*cpvdist(start, end), n.x, n.y);
ChipmunkDemoPrintString("Segment Query: Dist(%f) Normal(%5.2f, %5.2f)\n", segInfo.alpha*cpvdist(start, end), n.x, n.y);
} else {
ChipmunkDemoPrintString("Segment Query (None)");
ChipmunkDemoPrintString("Segment Query (None)\n");
}
// Draw a fat green line over the unoccluded part of the query

View File

@ -1,141 +0,0 @@
/* Copyright (c) 2007 Scott Lembcke
*
* 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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "chipmunk/chipmunk_private.h"
#include "ChipmunkDemo.h"
static cpBool DrawContacts(cpArbiter *arb, cpSpace *space, void *data){
cpContactPointSet set = cpArbiterGetContactPointSet(arb);
for(int i=0; i<set.count; i++){
cpVect p = set.points[i].point;
ChipmunkDebugDrawDot(6.0, p, RGBAColor(1, 0, 0, 1));
ChipmunkDebugDrawSegment(p, cpvadd(p, cpvmult(set.points[i].normal, 10.0)), RGBAColor(1, 0, 0, 1));
}
return cpFalse;
// return cpTrue;
}
static void
update(cpSpace *space)
{
int steps = 1;
cpFloat dt = 1.0f/60.0f/(cpFloat)steps;
for(int i=0; i<steps; i++){
cpSpaceStep(space, dt);
}
}
#define MAX(a, b) (a > b ? a : b)
#define MIN(a, b) (a < b ? a : b)
static cpSpace *
init(void)
{
cpSpace *space = cpSpaceNew();
cpSpaceSetIterations(space, 5);
cpSpaceSetDamping(space, 0.1f);
cpSpaceSetDefaultCollisionHandler(space, NULL, DrawContacts, NULL, NULL, NULL);
{
cpFloat mass = 1.0f;
cpFloat length = 100.0f;
cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f);
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b)));
cpBodySetPos(body, cpv(-160.0f, 80.0f));
cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 30.0f));
}
{
cpFloat mass = 1.0f;
const int NUM_VERTS = 5;
cpVect verts[NUM_VERTS];
for(int i=0; i<NUM_VERTS; i++){
cpFloat angle = -2*M_PI*i/((cpFloat) NUM_VERTS);
verts[i] = cpv(40*cos(angle), 40*sin(angle));
}
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero)));
cpBodySetPos(body, cpv(-0.0f, 80.0f));
cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpvzero));
}
{
cpFloat mass = 1.0f;
cpFloat r = 60.0f;
cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, INFINITY));
cpBodySetPos(body, cpv(160.0, 80.0f));
cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
}
cpBody *staticBody = cpSpaceGetStaticBody(space);
cpVect terrain[] = {
cpv(-320, -200),
cpv(-200, -100),
cpv( 0, -200),
cpv( 200, -100),
cpv( 320, -200),
};
int terrainCount = sizeof(terrain)/sizeof(*terrain);
for(int i=1; i<5; i++){
cpVect v0 = terrain[MAX(i-2, 0)];
cpVect v1 = terrain[i-1];
cpVect v2 = terrain[i];
cpVect v3 = terrain[MIN(i+1, terrainCount - 1)];
cpShape *seg = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, v1, v2, 10.0));
cpSegmentShapeSetNeighbors(seg, v0, v3);
}
return space;
}
static void
destroy(cpSpace *space)
{
ChipmunkDemoFreeSpaceChildren(space);
cpSpaceFree(space);
}
ChipmunkDemo Smooth = {
"Smooth",
1.0f/60.0f,
init,
update,
ChipmunkDemoDefaultDrawImpl,
destroy,
};