Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer to functions

Started by wizard341 May 9, 2003 at 1:42 PM 2 replies 350+ views
Original Post
wizard341
wizard341
Ive recently come across a slight stumbling block that has to deal with trying to point to a function. If i can get this to work it will save me about 10 pages of repetous code, which is always nicer to look at. My problem is this, for my collision detection method, i have a function that is called. It checks to see what direction is is facing, then stores the appropriate values in variables. For example, if you move up, then you only need to check your top left and right xy coordinates to see if it hits anything. Likewise, when it comes to dynamic collision you need only check against other players bottom left and right xy coordinates. So i can find my own (the character that is moving) xy coordinates easily, but i want to be able to check against the other players opposite xy coordinates. So if im moving up, i store my top xy coordinates, and i would also like to store a pointer to all the get methods i need to grab other players bottom xy coordinates, but im having problems. Here is a listing of my code.
    

	float (*pCheckAgainst1x)();
	float (*pCheckAgainst1y)();
	float (*pCheckAgainst2x)();
	float (*pCheckAgainst2y)();
	//boundries of our character

	float boundry1x;
	float boundry1y;
	float boundry2x;
	float boundry2y;

	//boundries of other characters to check against our character

	if(yPos - myYpos > 0)
	{
		directionFacing = 'u';
		boundry1x = upLeftX;
		boundry1y = upLeftY;
		boundry2x = upRightX;
		boundry2y = upRightY;

		pCheckAgainst1x = CPerson::getlowLeftX;
		pCheckAgainst1y = CPerson::getlowLeftY;
		pCheckAgainst2x = CPerson::getlowRightX;
		pCheckAgainst2y = CPerson::getlowRightY;

	}
    
And here is what im doing with the code (this part hasnt been changed for the pointer to function access yet, but you can get a general feel for what im doing Here is the function header so you can see the start of the function: bool CPerson::checkCollision(TileSpacePartition* TSP,CPERSONVECTOR& CV,float myXpos, float myYpos) TSP is just the playing board the game is played on, and CV is the data structure where the characters are stored
       

//first check for static collisions

if(TSP->GetSolid(boundry1x,boundry1y) == false && TSP->GetSolid(boundry2x,boundry2y) == false)
{
if(TSP->GetKillable(boundry1x,boundry1y) == true || TSP->GetKillable(boundry2x,boundry2y) == true)
{
//character died, tell the function we cant move and that were dead also

this[0].dead = true;
return false;
}
}
//we hit a solid object, tell the function that we cant move

else
{
return false;
}

//next check to see if we hit any other creatures

for(int x = 0; x < creatures; x++)
{
//first make sure were not comparing our own coordinates 

if((CV[x].upLeftX != this[0].upLeftX && CV[x].upRightX != this[0].upRightY))
{
if((boundry1y - CV[x].checkAgainst1y < 3 && boundry2y - CV[x].checkAgainst2y > -3) && (boundry1x - CV[x].checkAgainst1x < 16 && boundry2x - CV[x].checkAgainst > -16))
{
//we hit someone

return false;
}
}
}
//we didnt hit anything, tell the function its ok to move

return true;
   
Vc++ is giving me this error for trying to initially store the pointer to functions. d:\program files\microsoft visual studio\myprojects\bombermangamevc6\cperson.cpp(653) : error C2440: '=' : cannot convert from 'float (__thiscall CPerson::*)(void)' to 'float (__cdecl *)(void)' Any ideas on how to fix it? Thanks! [edited by - wizard341 on May 9, 2003 2:45:44 PM]
daerid
daerid
quote:
Original post by Anonymous Poster
You can''t create a function pointer to a normal C++ member function.


Not true at all.


  
#include <iostream>

class Object
{
public:
void Method() {
std::cout << "Method() " << std::endl;
}
void Function() {
std::cout << "Function() " << std::endl;
}
};

int main() {
// declare pointer

void (Object::*PtrToMember)();
Object o;

// assign the member pointer

PtrToMember = Object::Method;
(o.*PtrToMember)();

PtrToMember = Object::Function;
(o.*PtrToMember)();
}
daerid@gmail.com
wizard341
wizard341
Works perfectly. Thanks guys.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.