__declspec( dllexport ) void explosion( const void * _in, int in_size, void * _out, int out_sz ) { int i; Variable *var; cpBody *body; cpVect bp; float angle; float dist; float divi; float finalPower; float power = PEEKFLOAT(INPUT_MEMBLOCK,0); cpVect position = PEEKVECT(INPUT_MEMBLOCK,4); for (i = 0;i != mVariableHandler.mSize;i++) { var = mVariableHandler.mPtrArray[i]; if (var == NULL)continue; if (var->mType != VarTypeBody) continue; body = (cpBody*)var->mPtr; if (cpBodyIsStatic(body)) continue; bp = cpvsub(position,cpBodyGetPos(body)); dist = cpvlength(bp)+0.1f; divi = (power/dist); finalPower = -min(power,divi*divi); angle = cpvtoangle(bp); cpBodyApplyImpulse(body,cpv(cosf(angle)*finalPower,sinf(angle)*finalPower),cpBodyWorld2Local(body,position)); } }
static void update(int ticks) { int steps = 1; cpFloat dt = 1.0f/60.0f/(cpFloat)steps; for(int i=0; i<steps; i++){ // turn the control body based on the angle relative to the actual body cpVect mouseDelta = cpvsub(mousePoint, tankBody->p); cpFloat turn = cpvtoangle(cpvunrotate(tankBody->rot, mouseDelta)); cpBodySetAngle(tankControlBody, tankBody->a - turn); // drive the tank towards the mouse if(cpvnear(mousePoint, tankBody->p, 30.0)){ tankControlBody->v = cpvzero; // stop } else { cpFloat direction = (cpvdot(mouseDelta, tankBody->rot) > 0.0 ? 1.0 : -1.0); tankControlBody->v = cpvrotate(tankBody->rot, cpv(30.0f*direction, 0.0f)); } cpSpaceStep(space, dt); } }
int update_brain(Ship* s) { float* inputs = new float[NINPUTS]; float noseang = cpBodyGetAngle(s->body) + s->nose_angle; cpVect rpos = cpBodyGetPosition(s->target->body) - cpBodyGetPosition(s->body); inputs[0] = cpvlength(rpos); inputs[1] = restrictangle(cpvtoangle(rpos)-noseang); cpVect rvel = cpBodyGetVelocity(s->target->body) - cpBodyGetVelocity(s->body); inputs[2] = (rpos.x * rvel.x + rpos.y * rvel.y)/inputs[0]; inputs[3] = (rpos.x * rvel.y - rpos.y * rvel.x)/(inputs[0]*inputs[0]) - cpBodyGetAngularVelocity(s->body); inputs[4] = restrictangle(cpBodyGetAngle(s->target->body) + s->target->nose_angle - noseang); inputs[5] = cpBodyGetAngularVelocity(s->target->body); s->brain->update(inputs); delete inputs; return 0; }
cpFloat bmx_cpvect_toangle(cpVect * vec) { return cpvtoangle(*vec); }
static void update(void) { int steps, i; cpFloat dt; cpVect look, lastPos, curPos; projectile_t *pj; /* temp for looping */ static cpVect delta = {0, 0}; static projectile_t *curProj = NULL; static bool canToggleEditor = true; curPos = atlMousePos(); lastPos = atlMouseLastPos(); /* rotate 'gun' based on mouse position */ look = cpvnormalize(cpvsub(curPos, g_Cannon->body->p)); cpBodySetAngle(g_Cannon->body, cpvtoangle(look)); if (isKeyPressed(KEY_e) && canToggleEditor) { canToggleEditor = false; editorMode = !editorMode; } else if (!isKeyPressed(KEY_e)) { canToggleEditor = true; } if (editorMode) { if (!editorBody) initializeEditor(); handleEditor(); } else { if (editorBody) destroyEditor(); if (atlLeftMouseDown()) { if (!curProj) { /* don't let player hold the left mouse button to fire multiple rounds */ delta = cpvnormalize(cpvsub(atlMouseClickPos(), g_Cannon->body->p)); if (g_Cannon->ai < MAX_PROJECTILES) { curProj = g_Cannon->ammo[g_Cannon->ai++]; if (curProj) { cpSpaceAddBody(g_Space, curProj->body); curProj->body->p = cpvadd(g_Cannon->body->p, cpvmult(look, g_Cannon->length)); cpSpaceAddShape(g_Space, curProj->shape); cpBodyApplyImpulse(curProj->body, cpvmult(cpvforangle(g_Cannon->body->a), 600.0f), cpvzero); cpBodyActivate(curProj->body); } } } } else { curProj = NULL; } } /* treat projectiles as limited resources. only allocated at the start of a level */ for (i = 0; i <= g_Cannon->ai-1; ++i) { pj = g_Cannon->ammo[i]; if (!pj) continue; if (pj->body->v.x >= -0.01f && pj->body->v.x <= 0.01f && pj->body->v.y >= -0.01f && pj->body->v.y <= 0.01f) { /* TODO: mark an object for deletion, but don't delete immediately */ cpSpaceRemoveBody(g_Space, pj->body); cpSpaceRemoveShape(g_Space, pj->shape); free(g_Cannon->ammo[i]); g_Cannon->ammo[i] = NULL; } } steps = 3; dt = 1.0f/60.0f/(cpFloat)steps; for (i = 0; i < steps; ++i) { cpSpaceStep(g_Space, dt); } }
void landerDraw(Lander l) { drawCircle(l->body->p, cpvtoangle(l->body->rot), cpCircleShapeGetRadius(l->shape), RGBAColor(1.0, 1.0, 1.0, 1.0), RGBAColor(0.0, 0.0, 0.0, 1.0)); }