Exemple #1
0
KRShape2D* KRSimulator2D::getShape(const KRVector2D& pos) const
{
    cpShape* shape = cpSpacePointQueryFirst((cpSpace*)mCPSpace, cpv(pos.x, pos.y), GRABABLE_MASK_BIT, 0);
    if (shape != NULL) {
        return (KRShape2D*)shape->data;
    }
    return NULL;
}
Exemple #2
0
__declspec( dllexport ) void shapeunderpoint( const void * _in, int in_size, void * _out, int out_sz )
{
	Variable *var;
	cpShape *first = cpSpacePointQueryFirst(&mSpace,PEEKVECT(INPUT_MEMBLOCK,0),(cpLayers)PEEKUINT(INPUT_MEMBLOCK,8),(cpGroup)PEEKUINT(INPUT_MEMBLOCK,12));
	if (first == NULL)
	{
		POKEINT(OUTPUT_MEMBLOCK,0,0);
		return;
	}
	var = (Variable*)first->data;
	POKEINT(OUTPUT_MEMBLOCK,0,var->mCBPtr);
}
Exemple #3
0
void GameTrack::_surfaceQuery(R2DSurfaceQuery* query) {
	query->grip=grips[0];
	dvect pos=cpVectToDvect(query->pos);

	query->height=0;

	for(vector<GameRoad*>::iterator i=roads.begin();i!=roads.end();i++) {
		if((*i)->pointInside(pos)) {
			query->grip=(*i)->surface_grip;
			query->roughness=(*i)->surface_roughness;
			break;
		}
	}

	//TODO: hax!
	cpShape *s=cpSpacePointQueryFirst(sim->get_space(), query->pos, 0x8000, 0);
	if(s) {
		cpVect local_pos=cpBodyWorld2Local(s->body,query->pos);

		static const dvect p_offset=dvect(0.5,0.5);

		GameCar* obj=(GameCar*)s->body->data;
		if(obj) {
			dvect local_p=cpVectToDvect(local_pos);
			dvect size=(obj->shapes[0].end-obj->shapes[0].start);
			local_p=local_p/size+p_offset;
			query->height=(1-local_p.x);

			/*
			printf("world %f %f local %f %f\n",
					query->pos.x,query->pos.y,
					local_p.x,local_p.y);
			*/
		}
		else {
			query->height=0;
		}
	}

	//query->height=fmax(cos(query->pos.x/5),0);
	//query->height=((float)((int)(query->pos.x*10)%10))/10;
	/*
	if(query->pos.x>0 && query->pos.x<10) {
		query->height=query->pos.x/10;
	}
	else if (query->pos.x>=10 && query->pos.x<13) {
		query->height=(13-query->pos.x)/3;
	}
	else {
		query->height=0;
	}
	*/
}
static void
click(int button, int state, int x, int y)
{
	if(button == GLUT_LEFT_BUTTON){
		if(state == GLUT_DOWN){
			cpVect point = mouseToSpace(x, y);
		
			cpShape *shape = cpSpacePointQueryFirst(space, point, GRABABLE_MASK_BIT, CP_NO_GROUP);
			if(shape){
				cpBody *body = shape->body;
				mouseJoint = cpPivotJointNew2(mouseBody, body, cpvzero, cpBodyWorld2Local(body, point));
				mouseJoint->maxForce = 50000.0f;
				mouseJoint->errorBias = cpfpow(1.0f - 0.15f, 60.0f);
				cpSpaceAddConstraint(space, mouseJoint);
			}
		} else if(mouseJoint){
			cpSpaceRemoveConstraint(space, mouseJoint);
			cpConstraintFree(mouseJoint);
			mouseJoint = NULL;
		}
	}
}
Exemple #5
0
cp::Shape *Space::pointQueryFirst(const cp::Vect& point,cpLayers layers,cpGroup group)
{
		cpShape * temp = cpSpacePointQueryFirst(space,point,layers,group);
		return static_cast<cp::Shape *>(temp ? temp->data : 0);
}
Exemple #6
0
void handleEditor(void) {
    int dir, i;
    cpVect maxv, minv, mousePos;
    palette_object_t *tmp;
    cpConstraint *mouseJoint;
    static cpShape *clickShape = NULL;
    static bool canRotate = false;
    static cpShape *controlPoint = NULL;
    unsigned int w, h;

    mouseJoint = atlMouseJoint();

    if (atlLeftMouseDown()) {
        clickShape = cpSpacePointQueryFirst(g_Space, atlMouseClickPos(),
                                            GRABABLE_MASK_BIT, CP_NO_GROUP);

        if (clickShape && !mouseJoint) {
            if (clickShape->collision_type == PALETTE_TYPE) {
                pullObjectFromPalette(clickShape);
            } else if (clickShape->collision_type == CONTROL_POINT_TYPE) {
                controlPoint = clickShape;
            } else {
                grabObject(clickShape->body);
            }
        }
    } else {
        controlPoint = NULL;
        if (mouseJoint) {
            atlRemoveMouseJoint(g_Space);
        }
    }

    if (controlPoint)
        pullControlPoint(controlPoint);

    if (isKeyPressed(KEY_SPACE) && mouseJoint && canRotate) {
        canRotate = false;
        cpBodySetAngle(mouseJoint->b, cpBodyGetAngle(mouseJoint->b)+45.0f*(M_PI/180));
    } else if (!isKeyPressed(KEY_SPACE)) {
        canRotate = true;
    }

    w = atlWindowWidth();
    h = atlWindowHeight();
    maxv = atlMouseToSpace(w, h);
    minv = cpv(maxv.x-w, maxv.y+h);
    mousePos = atlMousePos();

    dir = 0;

    /* allow the mouse to scroll the screen at the edges of the X-axis. clamp it
     * to maxv.x and minv.x so the mouse being outside the app window doesn't
     * cause side scrolling inadvertently */
    if (isKeyPressed(KEY_RIGHT) ||
        (mousePos.x > maxv.x - 15 && mousePos.x < maxv.x))
        dir = 1;

    if (isKeyPressed(KEY_LEFT) ||
        (mousePos.x < minv.x + 15 && mousePos.x > minv.x))
        dir = -1;

    /* when the editorBody is positioned at (0, 0) it can't move left any more */
    if (editorBody->p.x < 0) {
        dir = 0;
        editorBody->p.x = 0;
    }

    if (dir) {
        editorBody->v = cpvadd(editorBody->v, cpvmult(cpv(1, 0), 30.0f*dir));
        cpBodyActivate(editorBody);
    } else {
        editorBody->v = cpvzero;
        /* recalculate static palette objects' positions */
        for (i = 0; i < MAX_PALETTE_SHAPES; ++i) {
            tmp = paletteObjects[i];
            if (tmp) {
                tmp->body->p = cpv(minv.x+25, minv.y-(45*(i+1)));
            }
        }
        cpSpaceRehashStatic(g_Space);
    }
}