1. pygame
  2. Untitled project
  3. pygame

Commits

minz  committed 118c8e6

collision is nearly right(needing position correction)

  • Participants
  • Parent commits 1078fef
  • Branches physics

Comments (0)

Files changed (4)

File Test/Main.c

View file
 #include <GL/glut.h>
-#include <string.h>
-#include <stdlib.h>
-#include <time.h>
-#include <stdarg.h>
-#include <stdio.h>
 
 #define HEIGHT 600
 #define WIDTH 600
-#define MAX_STR_LEN 1000
 
 /*-------------------------------测试工具----------------------------*/
 
 #include "pgPhysicsRenderer.h"
 #include "pgBodyObject.h"
+#include "glTestSuite.h"
 
 static pgWorldObject* s_world = NULL;
-static double s_updateTime = 0.0;
-
-//跑表
-static double s_time;
-
-void watch_start()
-{
-	s_time = (double)clock();
-}
-
-//计算从跑表打开到停止共经历了多少秒
-double watch_stop()
-{
-	return ((double)clock() - s_time)/CLOCKS_PER_SEC;
-}
-
-//渲染文字, 仅限英文,(x, y)是文字矩阵左上角坐标
-//每个字符字符宽9像素,高15像素
-void draw_text(int x, int y, char *str)
-{
-	int len, i, w, h;
-
-	glMatrixMode(GL_PROJECTION);
-	glPushMatrix();
-	glLoadIdentity();
-	w = glutGet(GLUT_WINDOW_WIDTH);
-	h = glutGet(GLUT_WINDOW_HEIGHT);
-	gluOrtho2D(0, w, h, 0);
-	glMatrixMode(GL_MODELVIEW);
-	glPushMatrix();
-	glLoadIdentity();
-
-	glColor3f(0.6f, 0.8f, 0.6f);
-	glRasterPos2i(x, y + 15);
-	len = (int) strlen(str);
-	for (i = 0; i < len; i++)
-		glutBitmapCharacter(GLUT_BITMAP_9_BY_15, str[i]);
-
-	glPopMatrix();
-	glMatrixMode(GL_PROJECTION);
-	glPopMatrix();
-	glMatrixMode(GL_MODELVIEW);
-}
-
-//打印文字到屏幕,类似printf,推荐
-void glprintf(int x, int y, const char* fmt, ...)
-{
-	static char buf[MAX_STR_LEN];
-	va_list p;
-	va_start(p, fmt);
-	memset(buf, 0, sizeof(buf));
-	vsprintf(buf, fmt, p);
-	draw_text(x, y, buf);
-}
-
-
-
-
 
 /*-------------------------------测试函数----------------------------*/
 
 void do_render()
 {
 	glColor3f(1.f, 1.f, 1.f);
-	PG_Update(s_world, 0.003);
+	PG_Update(s_world, 0.005);
 	PGT_RenderWorld(s_world);
-	//glprintf(0, 0, "Velocity of body1:(%.2f, %.2f)", body->vecLinearVelocity.real, 
+	//glprintf(0, 0, "Velocity of body: (%.2f, %.2f)", body->vecLinearVelocity.real, 
 	//	body->vecLinearVelocity.imag);
+	//glprintf(0, 20, "w of body: %d", body->fAngleVelocity);
 }
 
 
 }
 
 
-
-
-
 /*-------------------------------设置函数----------------------------*/
 
 
 	body = PG_BodyNew();
 	PG_Bind_RectShape(body, 20, 20, 0);
 	PG_Set_Vector2(body->vecPosition,0,0)
-	PG_Set_Vector2(body->vecLinearVelocity,40,0)
+	PG_Set_Vector2(body->vecLinearVelocity, 40, 0)
 	PG_AddBodyToWorld(s_world,body);
 	
 	
 	joint = PG_DistanceJointNew(body,NULL,0,100,a1,a2);
 	PG_AddJointToWorld(s_world,joint);
 }
-
 //test collision
 void TestBasic2Init()
 {
 	s_world->fStepTime = 0.03;
 
 	body = PG_BodyNew();
-	PG_Set_Vector2(body->vecPosition, 0, 0);
-	PG_Set_Vector2(body->vecLinearVelocity, 0, 0.f);
-	body->fRotation = 0;
-	body->fAngleVelocity = 0.f;
+	PG_Set_Vector2(body->vecPosition, -50, 0);
+	PG_Set_Vector2(body->vecLinearVelocity, 10.f, -10.f);
+	body->fRotation = M_PI/4;
+	body->fAngleVelocity = 2.f;
 	body->fRestitution = 1.f;
-	PG_Bind_RectShape(body, 20, 20, 0);
+	PG_Bind_RectShape(body, 30, 30, 0);
 	PG_AddBodyToWorld(s_world, body);
 	
 	body1 = PG_BodyNew();
 	body1->bStatic = 1;
 	body1->fRestitution = 1.f;//for test
 	body1->fMass = 1e24;
-	PG_Bind_RectShape(body1, 600, 20, 0);
+	PG_Bind_RectShape(body1, 500, 20, 0);
 	PG_AddBodyToWorld(s_world, body1);
 
 }
 	glutMainLoop();
 	return 0;
 }
-

File include/pgBodyObject.h

View file
 //TODO: is the local coordinate necessary? anyway let it alone right now.
 pgVector2 PG_GetGlobalPos(pgBodyObject* body, pgVector2* local_p);
 
-//return the global velocity of a point p (on the rigid body)
-//(notice: here p is defined in the global coordinate)
-pgVector2 PG_AngleToLinear1(pgBodyObject* body, pgVector2* global_p);
-pgVector2 PG_AngleToLinear(pgVector2* r, double w);
-
 //translate vector from coordinate B to coordinate A
 pgVector2 PG_GetRelativePos(pgBodyObject* bodyA, pgBodyObject* bodyB, pgVector2* p_in_B);
 

File src/pgBodyObject.c

View file
 	return p_in_A;
 }
 
-pgVector2 PG_AngleToLinear(pgVector2* r, double w)
-{
-	//pgVector2 v;
-	//double r_len, v_len;
-
-	//r_len = c_get_length(*r);
-	//if(is_zero(r_len))
-	//{
-	//	v.imag = v.real = 0;
-	//}
-	//else
-	//{
-	//	r->real /= r_len;
-	//	r->imag /= r_len;
-	//	v_len = fabs(r_len*w);
-	//	r->real *= v_len;
-	//	r->imag *= v_len;
-	//	if(w > 0) //counter-clock wise
-	//	{	
-	//		v.real = -r->imag;
-	//		v.imag = r->real;
-	//	}
-	//	else //clock wise
-	//	{
-	//		v.real = r->imag;
-	//		v.imag = -r->real;
-	//	}
-	//}
-
-	//return v;
-
-	pgVector2 ans;
-	ans = c_fcross(w, *r);
-	return ans;
-}
-
-pgVector2 PG_AngleToLinear1(pgBodyObject* body, pgVector2* global_p)
-{	
-	//get rotate radius vector r
-	pgVector2 r = c_diff(*global_p, body->vecPosition);
-	return PG_AngleToLinear(&r, body->fRotation);
-}
-
 //============================================================
 //getter and setter functions
 

File src/pgCollision.c

View file
 #include "pgAABBBox.h"
 #include "pgShapeObject.h"
 #include "pgBodyObject.h"
+#include "glTestSuite.h"
 #include <assert.h>
+#include <GL/glut.h>
 
 extern PyTypeObject pgContactType;
 
 	//k
 	refR = c_diff(contact->pos, refBody->vecPosition);
 	incidR = c_diff(contact->pos, incidBody->vecPosition);
-	
-	//tmp1 = refR.real*contact->normal.imag - refR.imag*contact->normal.real;
-	//tmp2 = incidR.real*contact->normal.imag - incidR.imag*contact->normal.real;
-
-	//k = 1/refBody->fMass + 1/incidBody->fMass + tmp1*tmp1/refBody->shape->rInertia
-	//	+ tmp2*tmp2/incidBody->shape->rInertia;
 
 	tmp1 = c_dot(c_fcross(c_cross(refR, contact->normal), refR), contact->normal)
 		 /refBody->shape->rInertia;
 	k = 1/refBody->fMass + 1/incidBody->fMass + tmp1 + tmp2;
 	
 	//dV = v2 + w2xr2 - (v1 + w1xr1)
-	incidV = c_sum(incidBody->vecLinearVelocity, PG_AngleToLinear1(incidBody, &(contact->pos)));
-	refV = c_sum(refBody->vecLinearVelocity, PG_AngleToLinear1(refBody, &(contact->pos)));
+	incidV = c_sum(incidBody->vecLinearVelocity, c_fcross(incidBody->fAngleVelocity,
+		           incidR));
+	refV = c_sum(refBody->vecLinearVelocity, c_fcross(refBody->fAngleVelocity,
+		         refR));
+
 	contact->dv = c_diff(incidV, refV);
 	neg_dV = c_diff(refV, incidV);
 	
 	refR = c_diff(contact->pos, refBody->vecPosition);
 	incidR = c_diff(contact->pos, incidBody->vecPosition);
 
-	if(contact->dv.imag > 0 && contact->dv.real > 0) return;
+	if(c_dot(contact->dv, contact->normal) > 0) return;
+	glPointSize(6.f);
+	glColor3f(1.f, 0.f, 0.f);
+	glBegin(GL_POINTS);
+	glVertex2d(contact->pos.real, contact->pos.imag);
+	glEnd();
+	glPointSize(1.f);
 
 	if(!refBody->bStatic)
 	{
 	{
 		incidBody->vecLinearVelocity = c_sum(incidBody->vecLinearVelocity, 
 			c_div_complex_with_real(moment, incidBody->fMass));
-		incidBody->fAngleVelocity += c_cross(refR, moment)/incidBody->shape->rInertia;
+		incidBody->fAngleVelocity += c_cross(incidR, moment)/incidBody->shape->rInertia;
 	}
 }