Polygon* newPolygonL(List L) { unsigned int i = 0, nbVx = lstCount(&L); Polygon* newPoly = (Polygon*) malloc(sizeof(Polygon)); Node* it = lstFirst(&L); /*Initialisation des Dynamic Arrays */ newPoly->Rigids = da(); newPoly->Vertices = da(); newPoly->InternalRigids = da(); daReserve(&newPoly->Rigids, nbVx); daReserve(&newPoly->Vertices, nbVx); newPoly->Center = NULL; newPoly->Fixed = FALSE; newPoly->GridPos.Valid = FALSE; newPoly->Collided = FALSE; /* Ajoute les Vertices */ while(!nodeEnd(it)) { daAdd(&newPoly->Vertices, (Vertex*) nodeGetData(it)); it = nodeGetNext(it); } /* Construit les limites, i.e. Créé un nouveau Rigid à partir de deux Vertices de la liste et la distance les séparant, puis l'ajoute à la liste */ for(i = 0; i < nbVx; i++) daAdd(&newPoly->Rigids, newRigid((Vertex*)daGet(&newPoly->Vertices, i), (Vertex*)daGet(&newPoly->Vertices, (i+1)%nbVx), vec2Length(vec2Sub(vxGetPosition((Vertex*)daGet(&newPoly->Vertices, i)), vxGetPosition((Vertex*)daGet(&newPoly->Vertices, (i+1)%nbVx)))))); return newPoly; }
void polyInit(Polygon* P, unsigned int nbVx, ...) { unsigned int i; va_list ap; /* Initialisation des Dynamic Arrays */ P->Rigids = da(); P->Vertices = da(); P->InternalRigids = da(); daReserve(&P->Rigids, nbVx); daReserve(&P->Vertices, nbVx); P->Center = NULL; P->Fixed = FALSE; P->GridPos.Valid = FALSE; P->Collided = FALSE; va_start(ap, nbVx); /* Ajoute les Vertices */ for(i = 0; i < nbVx; i++) daAdd(&P->Vertices, va_arg(ap, Vertex*)); va_end(ap); /* Construit les limites, i.e. Créé un nouveau Rigid à partir de deux Vertices de la liste et la distance les séparant, puis l'ajoute à la liste */ for(i = 0; i < nbVx; i++) daAdd(&P->Rigids, newRigid((Vertex*)daGet(&P->Vertices, i), (Vertex*)daGet(&P->Vertices, (i+1)%nbVx), vec2Length(vec2Sub(vxGetPosition((Vertex*)daGet(&P->Vertices, i)), vxGetPosition((Vertex*)daGet(&P->Vertices, (i+1)%nbVx)))))); }
Polygon* newPolygon(unsigned int nbVx, ...) { unsigned int i; va_list ap; Polygon* newPoly = (Polygon*) malloc(sizeof(Polygon)); /* Pas d'appel à polyInit possible à cause de la liste d'arguments... Initialisation des Dynamic Arrays */ newPoly->Rigids = da(); newPoly->Vertices = da(); newPoly->InternalRigids = da(); daReserve(&newPoly->Rigids, nbVx); daReserve(&newPoly->Vertices, nbVx); newPoly->Center = NULL; newPoly->Fixed = FALSE; newPoly->GridPos.Valid = FALSE; newPoly->Collided = FALSE; va_start(ap, nbVx); /* Ajoute les Vertices */ for(i = 0; i < nbVx; i++) daAdd(&newPoly->Vertices, va_arg(ap, Vertex*)); va_end(ap); /* Construit les limites, i.e. Créé un nouveau Rigid à partir de deux Vertices de la liste et la distance les séparant, puis l'ajoute à la liste */ for(i = 0; i < nbVx; i++) daAdd(&newPoly->Rigids, newRigid((Vertex*)daGet(&newPoly->Vertices, i), (Vertex*)daGet(&newPoly->Vertices, (i+1)%nbVx), vec2Length(vec2Sub(vxGetPosition((Vertex*)daGet(&newPoly->Vertices, i)), vxGetPosition((Vertex*)daGet(&newPoly->Vertices, (i+1)%nbVx)))))); return newPoly; }
int main(int argc, char **argv) { vec3 vector, vectorMultiplied; vec2 add; mat3x3 transform; vector.x = 3; vector.y = 0; vector.z = 1; add.x = 1000; add.y = 1000; printVec3(vector); vectorMultiplied = vector; mat3x3Identity(transform); mat3x3Rotate2D(transform, PI / 4); mat3x3Scale2D(transform, 2); mat3x3Translate(transform, add); printMat3x3(transform); printVec3(mat3x3GetCol(transform, 1)); printf("Applying transformation matrix...\n\n"); vectorMultiplied = mat3x3MultiplyVector(transform, vector); printVec3(vectorMultiplied); printf("Length: %.2f\n", vec2Length(vector.vec2)); return 0; }
void lnResolve(Lenght* R) { /* Vecteur V1V2 */ Vec2 Vect = vec2Sub(vxGetPosition(R->V2), vxGetPosition(R->V1)); /* Précalcul de la distance V1V2 */ float acLength = vec2Length(Vect), factor; if (acLength <= R->MinLenght-5.f) factor = (acLength - R->MinLenght); else if (acLength >= R->MaxLenght-5.f) factor = (acLength - R->MaxLenght); else return; //printf("Lenght: %f \n", factor); /* Normalisation du vecteur (pas besoin de vec2Normalized(), on a déjà acLength) */ if(acLength != 0.f) Vect = vec2Div(Vect, acLength); else Vect = vec2(1.f, 0.f); /* Vecteur quelconque en cas de deux points superposés, il ne peut raisonnablement être déduit des poistions précédentes, en effet, que faire si les points étaient déjà superposés à la frame précédente ?... */ if(vxIsFixe(R->V2)) vxCorrectPosition(R->V1, vec2Prod(Vect, factor)); else if(vxIsFixe(R->V1)) vxCorrectPosition(R->V2, vec2Prod(Vect, -factor)); else vxCorrectPosition(R->V2, vec2Prod(Vect, -factor*0.5f)), vxCorrectPosition(R->V1, vec2Prod(Vect, factor*0.5f)); }
void plGrabL(Player* P, World* W, float MouseX, float MouseY) { Vertex* tmpVx = wdGetNearest(W, MouseX, MouseY); if(tmpVx != NULL && vec2Length(vec2Sub(vxGetPosition(tmpVx), vxGetPosition(plGetVxUL(P)))) < 60.f) { P->GrabL = newRigid(plGetVxUL(P), tmpVx, 50.f); wdAddRigid(W, P->GrabL); } }
void jnInit(Joint* J, Vertex* C, Vertex* M, Vertex* S) { J->C = C; J->M = M; J->S = S; J->MinAng = DEG2RAD(190); J->MaxAng = DEG2RAD(360); float L1, L2; L1 = vec2Length(vec2Sub(vxGetPosition(J->M), vxGetPosition(J->C))); L2 = vec2Length(vec2Sub(vxGetPosition(J->S), vxGetPosition(J->C))); J->Length = L1 + L2; J->Factor = L2/L1; Vec2 CM = vec2Sub(vxGetPosition(J->M), vxGetPosition(J->C)); Vec2 tmp = vec2Prod(vec2Rotate(CM, vec2(0.f, 0.f), DEG2RAD(-10.f)), J->Factor); //CM = vec2Add(CM, vxGetPosition(C)); //tmp = vec2Sub(tmp, vxGetPosition(C)); J->MaxDot = vec2Dot(tmp, CM); printf("tmp : %f, %f; cm : %f, %f\n", tmp.x, tmp.y, CM.x, CM.y); printf("factor: %f; factored : %f, %f\n", J->Factor, CM.x*J->Factor, CM.y*J->Factor); }
void plUpdate(Player* P, s_SharedResources* SR) { P->GroundVec = vec2Ortho(P->Normal); // Mise à jour quelques States Vec2 RdBottom = vec2Normalized(vec2Sub(vxGetPosition(P->VxDR), vxGetPosition(P->VxDL))); if(RdBottom.x < 0.f) { P->State = P->State | PL_UPSIDEDOWN; P->State = P->State | PL_FALLING; } else { if(RdBottom.y < -0.5f) { P->State = P->State | PL_FALLING; P->State = P->State | PL_FALLING_L; } else if(RdBottom.y > 0.5f) { P->State = P->State | PL_FALLING; P->State = P->State | PL_FALLING_R; } } if(P->GrabL != NULL) P->State = P->State | PL_GRABL; if(P->GrabR != NULL) P->State = P->State | PL_GRABR; Vec2 v = vec2Sub(vxGetPosition(P->VxUL), vxGetPosition(P->VxDL)); P->PrevCenter = P->Center; P->Center = polyComputeCenter(P->Shape); v = vec2Normalized(v); vxSetPosition(P->vxBodyParts[bpBase], vec2Add(P->Center, vec2Prod(v, -15.f))); Vec2 speed = vec2Sub(P->Center, P->PrevCenter); //printf("speed:%f,%f\n",speed.x, speed.y); if (!(P->State & PL_ON_GROUND) && ABS(speed.y) > 0.1f) { if (speed.y<2.f) P->CurrentAnim = P->aniJump; else P->CurrentAnim = P->aniFall; P->timer.restart(); } else { if (ABS(speed.x) > 1.f) P->CurrentAnim = P->aniRun, P->timer.restart(); else if (P->timer.getElapsedTime().asSeconds() < 5.f) P->CurrentAnim = P->aniStand; else P->CurrentAnim = P->aniHello; } if (P->timer.getElapsedTime().asSeconds() > 20.f) P->timer.restart(); unsigned int LastState; if (aniGetType(P->CurrentAnim) == ANIM_POSITIONS) LastState = P->Positions.CurrentState; else LastState = P->Angles.CurrentState; if (ABS(speed.x) > 1.f && P->CurrentAnim == P->aniRun) { float spp = vec2Length(speed); aniUpdate(P->CurrentAnim, P, MAX(0.25f, spp/10.f)); } else aniUpdate(P->CurrentAnim, P, 1.f); if (aniGetType(P->CurrentAnim) == ANIM_POSITIONS) { if (P->CurrentAnim == P->aniRun && LastState != P->Positions.CurrentState) sndmPlay(shSoundManager(SR), P->SndFoot[LastState]); } else { if (P->CurrentAnim == P->aniRun && LastState != P->Angles.CurrentState) sndmPlay(shSoundManager(SR), P->SndFoot[LastState]); } }