Jump to content

Callback/Variable Interaction?


gamecreator
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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... :blink: Maybe worth a reminder as a feature request but I can live without it too.

Link to comment
Share on other sites

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... :blink: 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.

 

:o

 

can't speak for what was done for c/c++ though...

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...