/* 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 <string.h>
#include <math.h>
#include "chipmunk.h"
int cp_contact_persistence = 1;
#pragma mark Contact Set Helpers
// Equal function for contactSet.
static int
contactSetEql(cpShape **shapes, cpArbiter *arb)
{
cpShape *a = shapes[0];
cpShape *b = shapes[1];
return ((a == arb->private_a && b == arb->private_b) || (b == arb->private_a && a == arb->private_b));
}
// Transformation function for contactSet.
static void *
contactSetTrans(cpShape **shapes, cpSpace *space)
{
if(space->pooledArbiters->num == 0){
// arbiter pool is exhausted, make more
int count = CP_BUFFER_BYTES/sizeof(cpArbiter);
cpAssert(count, "Buffer size too small.");
cpArbiter *buffer = (cpArbiter *)cpmalloc(CP_BUFFER_BYTES);
cpArrayPush(space->allocatedBuffers, buffer);
for(int i=0; i<count; i++) cpArrayPush(space->pooledArbiters, buffer + i);
}
return cpArbiterInit( (cpArbiter *)cpArrayPop(space->pooledArbiters), shapes[0], shapes[1]);
}
#pragma mark Collision Pair Function Helpers
// Equals function for collFuncSet.
static int
collFuncSetEql(cpCollisionHandler *check, cpCollisionHandler *pair)
{
return ((check->a == pair->a && check->b == pair->b) || (check->b == pair->a && check->a == pair->b));
}
// Transformation function for collFuncSet.
static void *
collFuncSetTrans(cpCollisionHandler *handler, void *unused)
{
cpCollisionHandler *copy = (cpCollisionHandler *)cpmalloc(sizeof(cpCollisionHandler));
(*copy) = (*handler);
return copy;
}
#pragma mark Post Step Function Helpers
typedef struct postStepCallback {
cpPostStepFunc func;
void *obj;
void *data;
} postStepCallback;
static int
postStepFuncSetEql(postStepCallback *a, postStepCallback *b){
return a->obj == b->obj;
}
static void *
postStepFuncSetTrans(postStepCallback *callback, void *ignored)
{
postStepCallback *value = (postStepCallback *)cpmalloc(sizeof(postStepCallback));
(*value) = (*callback);
return value;
}
#pragma mark Misc Helper Funcs
// Default collision functions.
static int alwaysCollide(cpArbiter *arb, cpSpace *space, void *data){return 1;}
static void nothing(cpArbiter *arb, cpSpace *space, void *data){}
// BBfunc callback for the spatial hash.
static cpBB shapeBBFunc(cpShape *shape){return shape->bb;}
// Iterator functions for destructors.
static void freeWrap(void *ptr, void *unused){ cpfree(ptr);}
static void shapeFreeWrap(cpShape *ptr, void *unused){ cpShapeFree(ptr);}
static void bodyFreeWrap(cpBody *ptr, void *unused){ cpBodyFree(ptr);}
static void constraintFreeWrap(cpConstraint *ptr, void *unused){cpConstraintFree(ptr);}
#pragma mark Memory Management Functions
#define CP_CONTACTS_BUFFER_SIZE ((CP_BUFFER_BYTES - sizeof(cpContactBufferHeader))/sizeof(cpContact))
typedef struct cpContactBuffer {
cpContactBufferHeader header;
cpContact contacts[CP_CONTACTS_BUFFER_SIZE];
} cpContactBuffer;
static cpContactBufferHeader *
cpSpaceAllocContactBuffer(cpSpace *space)
{
cpContactBuffer *buffer = (cpContactBuffer *)malloc(sizeof(cpContactBuffer));
cpArrayPush(space->allocatedBuffers, buffer);
return (cpContactBufferHeader *)buffer;
}
static cpContactBufferHeader *
cpContactBufferHeaderInit(cpContactBufferHeader *header, cpSpace *space)
{
header->stamp = space->stamp;
header->next = space->contactBuffersTail;
header->numContacts = 0;
return header;
}
cpSpace *
cpSpaceAlloc(void)
{
return (cpSpace *)cpcalloc(1, sizeof(cpSpace));
}
#define DEFAULT_DIM_SIZE 100.0f
#define DEFAULT_COUNT 1000
#define DEFAULT_ITERATIONS 10
#define DEFAULT_ELASTIC_ITERATIONS 0
#define MAX_CONTACTS 10000
cpCollisionHandler defaultHandler = {0, 0, alwaysCollide, alwaysCollide, nothing, nothing, NULL};
cpSpace*
cpSpaceInit(cpSpace *space)
{
space->iterations = DEFAULT_ITERATIONS;
space->elasticIterations = DEFAULT_ELASTIC_ITERATIONS;
// space->sleepTicks = 300;
space->gravity = cpvzero;
space->damping = 1.0f;
space->locked = 0;
space->stamp = 0;
space->staticShapes = cpSpaceHashNew(DEFAULT_DIM_SIZE, DEFAULT_COUNT, (cpSpaceHashBBFunc)shapeBBFunc);
space->activeShapes = cpSpaceHashNew(DEFAULT_DIM_SIZE, DEFAULT_COUNT, (cpSpaceHashBBFunc)shapeBBFunc);
space->allocatedBuffers = cpArrayNew(0);
space->bodies = cpArrayNew(0);
space->arbiters = cpArrayNew(0);
space->pooledArbiters = cpArrayNew(0);
cpContactBufferHeader *header = cpContactBufferHeaderInit(cpSpaceAllocContactBuffer(space), space);
space->contactBuffersHead = header;
space->contactBuffersTail = header;
header->next = header; // Buffers will form a ring, start the ring explicitly
space->contactSet = cpHashSetNew(0, (cpHashSetEqlFunc)contactSetEql, (cpHashSetTransFunc)contactSetTrans);
space->constraints = cpArrayNew(0);
space->defaultHandler = defaultHandler;
space->collFuncSet = cpHashSetNew(0, (cpHashSetEqlFunc)collFuncSetEql, (cpHashSetTransFunc)collFuncSetTrans);
space->collFuncSet->default_value = &space->defaultHandler;
space->postStepCallbacks = cpHashSetNew(0, (cpHashSetEqlFunc)postStepFuncSetEql, (cpHashSetTransFunc)postStepFuncSetTrans);
return space;
}
cpSpace*
cpSpaceNew(void)
{
return cpSpaceInit(cpSpaceAlloc());
}
void
cpSpaceDestroy(cpSpace *space)
{
cpSpaceHashFree(space->staticShapes);
cpSpaceHashFree(space->activeShapes);
cpArrayFree(space->bodies);
cpArrayFree(space->constraints);
cpHashSetFree(space->contactSet);
cpArrayFree(space->arbiters);
cpArrayFree(space->pooledArbiters);
if(space->allocatedBuffers){
cpArrayEach(space->allocatedBuffers, freeWrap, NULL);
cpArrayFree(space->allocatedBuffers);
}
if(space->postStepCallbacks){
cpHashSetEach(space->postStepCallbacks, freeWrap, NULL);
cpHashSetFree(space->postStepCallbacks);
}
if(space->collFuncSet){
cpHashSetEach(space->collFuncSet, freeWrap, NULL);
cpHashSetFree(space->collFuncSet);
}
}
void
cpSpaceFree(cpSpace *space)
{
if(space){
cpSpaceDestroy(space);
cpfree(space);
}
}
void
cpSpaceFreeChildren(cpSpace *space)
{
cpSpaceHashEach(space->staticShapes, (cpSpaceHashIterator)&shapeFreeWrap, NULL);
cpSpaceHashEach(space->activeShapes, (cpSpaceHashIterator)&shapeFreeWrap, NULL);
cpArrayEach(space->bodies, (cpArrayIter)&bodyFreeWrap, NULL);
cpArrayEach(space->constraints, (cpArrayIter)&constraintFreeWrap, NULL);
}
#pragma mark Collision Handler Function Management
void
cpSpaceAddCollisionHandler(
cpSpace *space,
cpCollisionType a, cpCollisionType b,
cpCollisionBeginFunc begin,
cpCollisionPreSolveFunc preSolve,
cpCollisionPostSolveFunc postSolve,
cpCollisionSeparateFunc separate,
void *data
){
// Remove any old function so the new one will get added.
cpSpaceRemoveCollisionHandler(space, a, b);
cpCollisionHandler handler = {
a, b,
begin ? begin : alwaysCollide,
preSolve ? preSolve : alwaysColli
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
cocos2d-0.99.4-x-0.7.1.zip (1333个子文件)
libxml2.a 3.03MB
libjpeg.a 1.83MB
libpng.a 867KB
Makefile.ARM 20KB
Makefile-dynamic.ARM 20KB
Makefile.ARM 15KB
Makefile.ARM 9KB
Makefile-dynamic.ARM 9KB
Makefile.ARM 6KB
Makefile-dynamic.ARM 6KB
Makefile-dynamic.ARM 3KB
Makefile.ARM 3KB
Makefile.ARM 2KB
Makefile.ARM 2KB
Makefile.ARM 2KB
Makefile.ARM 46B
LICENSE.artwork 2KB
copy_res.bat 742B
copy_res.bat 38B
LICENSE.box2d 886B
cpSpace.c 25KB
cpSpaceHash.c 13KB
cpCollision.c 11KB
cpShape.c 9KB
cpArbiter.c 7KB
cpPolyShape.c 6KB
cpHashSet.c 6KB
cpBody.c 5KB
cpGrooveJoint.c 4KB
cpSlideJoint.c 4KB
cpDampedSpring.c 4KB
cpRatchetJoint.c 4KB
cpPinJoint.c 3KB
cpDampedRotarySpring.c 3KB
cpRotaryLimitJoint.c 3KB
cpPivotJoint.c 3KB
cpGearJoint.c 3KB
chipmunk.c 3KB
cpSimpleMotor.c 3KB
cpArray.c 3KB
cpVect.c 2KB
cpConstraint.c 2KB
cpBB.c 2KB
CHANGELOG 2KB
LICENSE.chipmunk 1KB
doxygen.cocos2d-x 69KB
LICENSE.cocos2d-x 1KB
SpriteTest.cpp 85KB
unzip.cpp 69KB
unzip.cpp 69KB
CCIntervalAction.cpp 42KB
TileMapTest.cpp 34KB
CCTransition.cpp 31KB
ActionsTest.cpp 30KB
ParticleTest.cpp 30KB
CCParticleSystem.cpp 29KB
CCEaseAction.cpp 27KB
b2World.cpp 24KB
CCTiledGridAction.cpp 24KB
CCSprite.cpp 24KB
EaseActionsTest.cpp 23KB
CCNode.cpp 21KB
CCDirector.cpp 21KB
CocosNodeTest.cpp 21KB
CCBitmapFontAtlas.cpp 20KB
CCTMXXMLParser.cpp 19KB
CCParticleExamples.cpp 19KB
CCGrid3DAction.cpp 18KB
b2ContactSolver.cpp 18KB
CCProgressTimer.cpp 18KB
CCTMXLayer.cpp 17KB
AtlasTest.cpp 17KB
CCTexture2D.cpp 17KB
CCMenuItem.cpp 17KB
CCSpriteSheet.cpp 16KB
b2PrismaticJoint.cpp 16KB
CCScheduler.cpp 15KB
CCGrid.cpp 15KB
b2LineJoint.cpp 15KB
SchedulerTest.cpp 14KB
CCXFileUtils_uphone.cpp 14KB
CCMenu.cpp 14KB
drawSpace.cpp 14KB
MenuTest.cpp 14KB
TransitionsTest.cpp 13KB
CCXEGLView_uphone.cpp 13KB
b2Distance.cpp 13KB
MagnetsElectric.cpp 13KB
b2Island.cpp 13KB
b2RevoluteJoint.cpp 13KB
EffectsTest.cpp 12KB
b2TimeOfImpact.cpp 12KB
CCXUIImage_uphone.cpp 12KB
CCXUIImage_win32.cpp 12KB
CCRibbon.cpp 11KB
cocos2dChipmunkDemo.cpp 11KB
EffectsAdvancedTest.cpp 11KB
CCXEGLView_win32.cpp 11KB
CCTouchDispatcher.cpp 11KB
b2PolygonShape.cpp 11KB
共 1333 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
独步夜雨
- 粉丝: 41
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于QT+QML+C++实现的仿 Windows10 画图3D 的颜色选择器+源码
- 32代码四驱简易电动小车蓝牙控制、语音控制、寻光、巡线、避障(内含语音模块为天问ASR-PRO开发板与32单片机串口通信的代码)
- 基于STM32的计步器的设计
- 基于Qt与STM32平台开发的汽车车机系统上位机
- 基于MQTT的智能宠物投喂系统
- 312749069629470selfieU重绘.apk
- http%3A%2F%2Fimg.wsdl.vivo.com.cn%2Fappstore%2Fdeveloper%2Ficon%2F201412%2F201412231038336
- 主要记录B站up主莫烦matplotlib教程中的代码.zip
- Java SE Development Kit 8u411 Windows x64 Installer
- 用于科学绘图的 Matplotlib 样式.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页