void weapon_update(Entity *ent) { cpBody *body = entity_body(ent); WeaponEntityData *data = entity_data(ent); if (data->owner && !data->player_joint) { cvar_setd_player(entity_owner(ent), "weapon_id", entity_id(ent)); data->player_joint = cpSpaceAddConstraint(game.space, cpPivotJointNew2( body, entity_body(data->owner), cpv(0, -WEAPON_HEIGHT / 2), cpv(1, 0) )); cpConstraintSetErrorBias(data->player_joint, 0); } int mult = 0; if (keymap_is_held("mouse1")) mult++; if (keymap_is_held("mouse2")) mult--; if (mult) { cpVect force_pos = cpv(0, WEAPON_HEIGHT); cpVect weapon_pos = cpBodyGetPosition(body); cpVect world_force_pos = cpvadd(weapon_pos, cpvrotate(cpBodyGetRotation(body), force_pos)); cpVect mouse_delta = cpvsub(keymap_mouse_world(), world_force_pos); cpVect force = cpvmult(cpvnormalize(mouse_delta), mult * WEAPON_SWING_FORCE); cpBodyApplyForceAtWorldPoint(body, force, world_force_pos); } }
void RigidBody2D::AddForce(const Vector2f& force, const Vector2f& point, CoordSys coordSys) { switch (coordSys) { case CoordSys_Global: cpBodyApplyForceAtWorldPoint(m_handle, cpv(force.x, force.y), cpv(point.x, point.y)); break; case CoordSys_Local: cpBodyApplyForceAtLocalPoint(m_handle, cpv(force.x, force.y), cpv(point.x, point.y)); break; } }