gamecreator Posted December 15, 2011 Share Posted December 15, 2011 Hi. I must be doing something very strange. I have the following code: #include "engine.h" TEntity cube, cubephy, ground, groundphy; bool collisiondetected=false; void _stdcall EntityCollisionCallback(TEntity entity0, TEntity entity1) { collisiondetected=true; } int colmode=2; int blah=11; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { Initialize(); RegisterAbstractPath("D:/Programming/Leadwerks Engine SDK"); Graphics(640,480); DebugPhysics(1); TFramework fw = CreateFramework(); SetGlobalObject("fw",fw); BP lua=GetLuaState(); lua_pushobject(lua,fw); lua_setglobal(lua,"fw"); lua_pop(lua,1); TCamera camera=GetLayerCamera(GetFrameworkLayer(0)); PositionEntity(camera,Vec3(0,-2,-5)); RotateEntity(camera,Vec3(15,0,0)); TLight light=CreateDirectionalLight(); RotateEntity(light,Vec3(45,45,45)); cube=CreateCube(0); cubephy=CreateBodyBox(0.5,0.5,0.5,0); EntityParent(cube,cubephy,0); ScaleEntity(cube,Vec3(0.5,0.5,0.5)); MoveEntity(cubephy,Vec3(0,10,0)); SetBodyMass(cubephy,1); EntityType(cubephy,1,1); ground=CreateCube(0); groundphy=CreateBodyBox(5,0.5,5,0); EntityParent(ground,groundphy,0); ScaleEntity(ground,Vec3(5,0.5,5)); MoveEntity(groundphy,Vec3(0,-5,0)); SetBodyMass(groundphy,0); EntityType(groundphy,1,1); SetEntityCallback(cubephy,(byte*)EntityCollisionCallback,ENTITYCALLBACK_COLLISION); Collisions(1,1,2); while(!KeyHit(KEY_ESCAPE)) { if(KeyDown(KEY_2)) { blah=2; RotateEntity(cubephy,Vec3(0,100)); SetBodyOmega(cubephy,Vec3(0,0,1)); PositionEntity(cubephy,Vec3(0,5,0)); SetBodyVelocity(cubephy,Vec3(0,0,0)); Collisions(1,1,2); colmode=2; collisiondetected=false; } else if(KeyDown(KEY_1)) { RotateEntity(cubephy,Vec3(0,100)); SetBodyOmega(cubephy,Vec3(0,0,1)); PositionEntity(cubephy,Vec3(0,5,0)); SetBodyVelocity(cubephy,Vec3(0,0,0)); Collisions(1,1,1); colmode=1; collisiondetected=false; } colmode=13; UpdateFramework(); RenderFramework(); DrawText(0,40,"Press 1 for Collisions(1), 2 for Collisions(2)"); DrawText(0,80,"Collision mode: %d",colmode); if(collisiondetected) DrawText(0,100,"Collision detected: yes"); else DrawText(0,100,"Collision detected: no"); DrawText(0,120,"Blah: %d",blah); Flip(0); } return 1; } It drops a cube onto a ground or through it, depending on the collision method. What's strange is the variables colmode and blah. colmode stays 13 until the cube hits the ground. It then changes to what seems to be a random number (20776980). Then, when I hit 2, blah changes to that same number as well. But if I run the program and hit 2 before the cube hits, blah changes to 2. Then if I hit it again after the collision is detected, it changes to that odd number. Anyone have any idea what's going on here? I couldn't even guess beyond it having something to do with that callback. Quote Link to comment Share on other sites More sharing options...
gamecreator Posted December 15, 2011 Author Share Posted December 15, 2011 I believe I solved my problem. You apparently need to have the entire function declared, even if you're not using the other arguments, so it should have been this: void _stdcall EntityCollisionCallback(TEntity entity0, TEntity entity1, byte* position, byte* normal, byte* force, flt speed) Makes sense since I imagine it still fills the position and other values and since I didn't have those declared, it used the next available memory, which were my variables. At least that's my guess, not knowing much about it. Quote Link to comment Share on other sites More sharing options...
Rick Posted December 15, 2011 Share Posted December 15, 2011 Such is the danger when passing a function pointer as (byte*) Quote Link to comment Share on other sites More sharing options...
gamecreator Posted December 15, 2011 Author Share Posted December 15, 2011 Ha, no joke. I've kept in mind not to access arrays outside of their bounds, for example, but I never thought of this before. What's funny is that macklebee posted code which made it look like the callback also works with Vec3 but I couldn't get it to work. Not sure if he was just posting that from the top of his head. And there's also Josh's post... Maybe worth a reminder as a feature request but I can live without it too. Quote Link to comment Share on other sites More sharing options...
Rick Posted December 15, 2011 Share Posted December 15, 2011 lol 2 years ago. Quote Link to comment Share on other sites More sharing options...
macklebee Posted December 15, 2011 Share Posted December 15, 2011 What's funny is that macklebee posted code which made it look like the callback also works with Vec3 but I couldn't get it to work. Not sure if he was just posting that from the top of his head. And there's also Josh's post... Maybe worth a reminder as a feature request but I can live without it too. from the version.txt file: 2.30-Added Lua implementation. -New editor. -Roads. -Vegetation billboards. -Switched to single-state Lua system. -Added GetLuaState(). -Added saturation, brightness, contrast controls in environment_atmosphere class. -Fixed bug where LoadScene() skipped terrain holes. -Added MouseZ() to lua command set. -Fixed terrain sector AABB calculation error. -Fixed bug where controller could walk on hidden terrain tiles. -BlitzMax collision callbacks now use vec3 objects instead of byte ptr. -Framework commands added to main engine. can't speak for what was done for c/c++ though... Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
gamecreator Posted December 16, 2011 Author Share Posted December 16, 2011 Ah, thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.