From 1021d32f7ccb9f3f10253934fd4e5202b7382b2d Mon Sep 17 00:00:00 2001 From: neoraider Date: Fri, 20 Oct 2006 12:57:05 +0000 Subject: Physik-Engine angefangen. Lightmaps implementiert. --- Makefile.am | 4 +- Makefile.in | 10 ++-- collision.c | 8 +-- config.h.in | 4 +- configure | 151 +++++++++++++++++++++++++++--------------------------- configure.in | 5 +- math.c | 6 +-- neofx/collision.h | 2 +- neofx/math.h | 2 +- neofx/physics.h | 9 ++++ neofx/types.h | 14 +++-- physics.c | 26 ++++++++++ stamp-h.in | 0 13 files changed, 141 insertions(+), 100 deletions(-) create mode 100644 neofx/physics.h create mode 100644 physics.c delete mode 100644 stamp-h.in diff --git a/Makefile.am b/Makefile.am index af678bc..1b03ef7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,3 +1,3 @@ lib_LTLIBRARIES = libneofx.la -libneofx_la_SOURCES = math.c collision.c -nobase_include_HEADERS = neofx/math.h neofx/collision.h neofx/types.h +libneofx_la_SOURCES = math.c collision.c physics.c +nobase_include_HEADERS = neofx/math.h neofx/collision.h neofx/types.h neofx/physics.h diff --git a/Makefile.in b/Makefile.in index a75c23c..d1032ea 100644 --- a/Makefile.in +++ b/Makefile.in @@ -133,8 +133,8 @@ sharedstatedir = @sharedstatedir@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ lib_LTLIBRARIES = libneofx.la -libneofx_la_SOURCES = math.c collision.c -nobase_include_HEADERS = neofx/math.h neofx/collision.h neofx/types.h +libneofx_la_SOURCES = math.c collision.c physics.c +nobase_include_HEADERS = neofx/math.h neofx/collision.h neofx/types.h neofx/physics.h subdir = . ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 mkinstalldirs = $(SHELL) $(top_srcdir)/mkinstalldirs @@ -144,13 +144,14 @@ LTLIBRARIES = $(lib_LTLIBRARIES) libneofx_la_LDFLAGS = libneofx_la_LIBADD = -am_libneofx_la_OBJECTS = math.lo collision.lo +am_libneofx_la_OBJECTS = math.lo collision.lo physics.lo libneofx_la_OBJECTS = $(am_libneofx_la_OBJECTS) DEFAULT_INCLUDES = -I. -I$(srcdir) -I. depcomp = $(SHELL) $(top_srcdir)/depcomp am__depfiles_maybe = depfiles -@AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/collision.Plo ./$(DEPDIR)/math.Plo +@AMDEP_TRUE@DEP_FILES = ./$(DEPDIR)/collision.Plo ./$(DEPDIR)/math.Plo \ +@AMDEP_TRUE@ ./$(DEPDIR)/physics.Plo COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) \ @@ -245,6 +246,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/collision.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/math.Plo@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/physics.Plo@am__quote@ .c.o: @am__fastdepCC_TRUE@ if $(COMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" \ diff --git a/collision.c b/collision.c index 27e00b7..d093432 100644 --- a/collision.c +++ b/collision.c @@ -27,7 +27,7 @@ int CollisionPointTriangle(VERTEX p, TRIANGLE t) { int CollisionRayTriangle(VERTEX p, VECTOR vec, TRIANGLE t, float* f) { VECTOR pvec, tvec, qvec, edge1, edge2; - float det, u, v, invDet; + float det, u, v; edge1 = VectorSub(t.vertices[1], t.vertices[0]); edge2 = VectorSub(t.vertices[2], t.vertices[0]); @@ -46,11 +46,7 @@ int CollisionRayTriangle(VERTEX p, VECTOR vec, TRIANGLE t, float* f) { v = VectorDot(vec, qvec); if(v < 0.0 || u+v > det) return 0; - if(f) { - *f = VectorDot(edge2, qvec); - invDet = 1.0 / det; - *f *= invDet; - } + if(f) *f = VectorDot(edge2, qvec) / det; return 1; } diff --git a/config.h.in b/config.h.in index d89fbf0..9b647ec 100644 --- a/config.h.in +++ b/config.h.in @@ -20,8 +20,8 @@ and to 0 otherwise. */ #undef HAVE_REALLOC -/* Define to 1 if you have the `sqrt' function. */ -#undef HAVE_SQRT +/* Define to 1 if you have the `sqrtf' function. */ +#undef HAVE_SQRTF /* Define to 1 if you have the header file. */ #undef HAVE_STDDEF_H diff --git a/configure b/configure index 1407f63..599322b 100755 --- a/configure +++ b/configure @@ -19032,6 +19032,79 @@ LIBTOOL='$(SHELL) $(top_builddir)/libtool' # Checks for libraries. +echo "$as_me:$LINENO: checking for sqrt in -lm" >&5 +echo $ECHO_N "checking for sqrt in -lm... $ECHO_C" >&6 +if test "${ac_cv_lib_m_sqrt+set}" = set; then + echo $ECHO_N "(cached) $ECHO_C" >&6 +else + ac_check_lib_save_LIBS=$LIBS +LIBS="-lm $LIBS" +cat >conftest.$ac_ext <<_ACEOF +/* confdefs.h. */ +_ACEOF +cat confdefs.h >>conftest.$ac_ext +cat >>conftest.$ac_ext <<_ACEOF +/* end confdefs.h. */ + +/* Override any gcc2 internal prototype to avoid an error. */ +#ifdef __cplusplus +extern "C" +#endif +/* We use char because int might match the return type of a gcc2 + builtin and then its argument prototype would still apply. */ +char sqrt (); +int +main () +{ +sqrt (); + ; + return 0; +} +_ACEOF +rm -f conftest.$ac_objext conftest$ac_exeext +if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5 + (eval $ac_link) 2>conftest.er1 + ac_status=$? + grep -v '^ *+' conftest.er1 >conftest.err + rm -f conftest.er1 + cat conftest.err >&5 + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); } && + { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; } && + { ac_try='test -s conftest$ac_exeext' + { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 + (eval $ac_try) 2>&5 + ac_status=$? + echo "$as_me:$LINENO: \$? = $ac_status" >&5 + (exit $ac_status); }; }; then + ac_cv_lib_m_sqrt=yes +else + echo "$as_me: failed program was:" >&5 +sed 's/^/| /' conftest.$ac_ext >&5 + +ac_cv_lib_m_sqrt=no +fi +rm -f conftest.err conftest.$ac_objext \ + conftest$ac_exeext conftest.$ac_ext +LIBS=$ac_check_lib_save_LIBS +fi +echo "$as_me:$LINENO: result: $ac_cv_lib_m_sqrt" >&5 +echo "${ECHO_T}$ac_cv_lib_m_sqrt" >&6 +if test $ac_cv_lib_m_sqrt = yes; then + cat >>confdefs.h <<_ACEOF +#define HAVE_LIBM 1 +_ACEOF + + LIBS="-lm $LIBS" + +fi + + # Checks for header files. echo "$as_me:$LINENO: checking for ANSI C header files" >&5 echo $ECHO_N "checking for ANSI C header files... $ECHO_C" >&6 @@ -19878,80 +19951,8 @@ fi -echo "$as_me:$LINENO: checking for cos in -lm" >&5 -echo $ECHO_N "checking for cos in -lm... $ECHO_C" >&6 -if test "${ac_cv_lib_m_cos+set}" = set; then - echo $ECHO_N "(cached) $ECHO_C" >&6 -else - ac_check_lib_save_LIBS=$LIBS -LIBS="-lm $LIBS" -cat >conftest.$ac_ext <<_ACEOF -/* confdefs.h. */ -_ACEOF -cat confdefs.h >>conftest.$ac_ext -cat >>conftest.$ac_ext <<_ACEOF -/* end confdefs.h. */ - -/* Override any gcc2 internal prototype to avoid an error. */ -#ifdef __cplusplus -extern "C" -#endif -/* We use char because int might match the return type of a gcc2 - builtin and then its argument prototype would still apply. */ -char cos (); -int -main () -{ -cos (); - ; - return 0; -} -_ACEOF -rm -f conftest.$ac_objext conftest$ac_exeext -if { (eval echo "$as_me:$LINENO: \"$ac_link\"") >&5 - (eval $ac_link) 2>conftest.er1 - ac_status=$? - grep -v '^ *+' conftest.er1 >conftest.err - rm -f conftest.er1 - cat conftest.err >&5 - echo "$as_me:$LINENO: \$? = $ac_status" >&5 - (exit $ac_status); } && - { ac_try='test -z "$ac_c_werror_flag" || test ! -s conftest.err' - { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 - (eval $ac_try) 2>&5 - ac_status=$? - echo "$as_me:$LINENO: \$? = $ac_status" >&5 - (exit $ac_status); }; } && - { ac_try='test -s conftest$ac_exeext' - { (eval echo "$as_me:$LINENO: \"$ac_try\"") >&5 - (eval $ac_try) 2>&5 - ac_status=$? - echo "$as_me:$LINENO: \$? = $ac_status" >&5 - (exit $ac_status); }; }; then - ac_cv_lib_m_cos=yes -else - echo "$as_me: failed program was:" >&5 -sed 's/^/| /' conftest.$ac_ext >&5 - -ac_cv_lib_m_cos=no -fi -rm -f conftest.err conftest.$ac_objext \ - conftest$ac_exeext conftest.$ac_ext -LIBS=$ac_check_lib_save_LIBS -fi -echo "$as_me:$LINENO: result: $ac_cv_lib_m_cos" >&5 -echo "${ECHO_T}$ac_cv_lib_m_cos" >&6 -if test $ac_cv_lib_m_cos = yes; then - cat >>confdefs.h <<_ACEOF -#define HAVE_LIBM 1 -_ACEOF - - LIBS="-lm $LIBS" - -fi - -for ac_func in sqrt +for ac_func in sqrtf do as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` echo "$as_me:$LINENO: checking for $ac_func" >&5 @@ -20049,8 +20050,8 @@ if test `eval echo '${'$as_ac_var'}'` = yes; then _ACEOF else - { { echo "$as_me:$LINENO: error: required function sqrt not found" >&5 -echo "$as_me: error: required function sqrt not found" >&2;} + { { echo "$as_me:$LINENO: error: required function sqrtf not found" >&5 +echo "$as_me: error: required function sqrtf not found" >&2;} { (exit 1); exit 1; }; } fi done diff --git a/configure.in b/configure.in index 613b87c..c175a8e 100644 --- a/configure.in +++ b/configure.in @@ -14,6 +14,7 @@ AC_PROG_CC AC_PROG_LIBTOOL # Checks for libraries. +AC_CHECK_LIB(m, [sqrt]) # Checks for header files. AC_HEADER_STDC @@ -25,8 +26,8 @@ AC_HEADER_TIME # Checks for library functions. AC_FUNC_MALLOC AC_FUNC_REALLOC -AC_CHECK_LIB(m, [cos]) -AC_CHECK_FUNCS([sqrt], , AC_MSG_ERROR([required function sqrt not found])) + +AC_CHECK_FUNCS([sqrtf], , AC_MSG_ERROR([required function sqrtf not found])) AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/math.c b/math.c index 799de5b..51f5c13 100644 --- a/math.c +++ b/math.c @@ -21,7 +21,7 @@ VECTOR VectorSub(VECTOR v1, VECTOR v2) { vec.x = v1.x - v2.x; vec.y = v1.y - v2.y; - vec.z = v1.z - v2.z; + vec.z = v1.z - v2.z; return vec; } @@ -45,7 +45,7 @@ VECTOR VectorMul(VECTOR v, float f) { } VECTOR VectorNormalize(VECTOR v) { - float f = sqrt(v.x*v.x + v.y*v.y + v.z*v.z); + float f = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z); v.x /= f; v.y /= f; @@ -63,7 +63,7 @@ VECTOR VectorNeg(VECTOR v) { } float VectorLength(VECTOR v) { - return sqrt(v.x*v.x + v.y*v.y + v.z*v.z); + return sqrtf(v.x*v.x + v.y*v.y + v.z*v.z); } float VectorLengthSq(VECTOR v) { diff --git a/neofx/collision.h b/neofx/collision.h index ce5ff5f..5abe327 100644 --- a/neofx/collision.h +++ b/neofx/collision.h @@ -1,7 +1,7 @@ #ifndef NEOFX_COLLISION_H #define NEOFX_COLLISION_H -#include "types.h" +#include int CollisionPointTriangle(VERTEX, TRIANGLE); int CollisionRayTriangle(VERTEX, VECTOR, TRIANGLE, float*); diff --git a/neofx/math.h b/neofx/math.h index f0f6550..9d6c37c 100644 --- a/neofx/math.h +++ b/neofx/math.h @@ -1,7 +1,7 @@ #ifndef NEOFX_MATH_H #define NEOFX_MATH_H -#include "types.h" +#include float VectorDot(VECTOR, VECTOR); VECTOR VectorCross(VECTOR, VECTOR); diff --git a/neofx/physics.h b/neofx/physics.h new file mode 100644 index 0000000..4eee4c9 --- /dev/null +++ b/neofx/physics.h @@ -0,0 +1,9 @@ +#ifndef NEOFX_PHYSICS_H +#define NEOFX_PHYSICS_H + +#include + +void SetGravitation(VECTOR); +void ApplyPhysics(OBJECT, float); + +#endif diff --git a/neofx/types.h b/neofx/types.h index add2c88..8586545 100644 --- a/neofx/types.h +++ b/neofx/types.h @@ -1,15 +1,14 @@ #ifndef NEOFX_TYPES_H #define NEOFX_TYPES_H -#pragma pack(push, 2) typedef union _MATRIX { float m[4][4]; float f[16]; } MATRIX; -typedef struct _VECTOR_VERTEX { +typedef struct _VECTOR_VERTEX_ROTATION { float x, y, z; -} VECTOR, VERTEX; +} VECTOR, VERTEX, ROTATION; typedef struct _TEXCOORDS { float s, t; @@ -19,6 +18,13 @@ typedef struct _TRIANGLE { VERTEX vertices[3]; VECTOR normal; } TRIANGLE; -#pragma pack(pop) + +typedef struct _OBJECT { + VERTEX pos; + VECTOR move; + ROTATION rot; + float air_resistance; + float bounce; +} OBJECT; #endif diff --git a/physics.c b/physics.c new file mode 100644 index 0000000..cea066d --- /dev/null +++ b/physics.c @@ -0,0 +1,26 @@ +#include +#include +#include + +static VECTOR gravitation = {0, -9.8, 0}; + + +void SetGravitation(VECTOR v) { + gravitation = v; +} + +void ApplyPhysics(OBJECT o, float delta) { + VERTEX old_pos = o.pos; + VECTOR old_move = o.move; + float e; + + e = expf(-o.air_resistance*delta); + + o.move = VectorAdd(VectorMul(gravitation, 1/o.air_resistance), + VectorMul(VectorSub(old_move, VectorMul(gravitation, 1/o.air_resistance)), + e)); + + o.pos = VectorAdd(old_pos, VectorAdd(VectorMul(gravitation, delta/o.air_resistance), + VectorMul(VectorSub(old_move, VectorMul(gravitation, 1/o.air_resistance)), + (1-e)/o.air_resistance))); +} diff --git a/stamp-h.in b/stamp-h.in deleted file mode 100644 index e69de29..0000000 -- cgit v1.2.3