Jump to content

[Solved] Shooting a Gun - Hitting the Target


Shard
 Share

Recommended Posts

When I shoot the gun, I need to check if I hit a Character type object. Unfortunately, I can't compare the returned entity to check if I hit a Character object or a Vehicle object because it is a byte*. How do you guys determine what you have hit?

 

 

Example;

 

//Character

class Character
{

TMesh mesh = LoadMesh("abstract::player.gmf");
SetEntityUserData(this);

};

[/code


]//Shoot Function
[code]void Character::Shoot()
{
if(currentWeapon->canShoot)
{
	currentWeapon->canShoot = false;
	TPick target;

	if(user->isPlayer) EntityPick(&target,frameWork->GetMain().GetCamera(),range);
	else EntityPick(&target,user->head,range);

	if(target.entity != NULL)
	{
		Character *hitTarget = (Character*) GetEntityUserData(target.entity); //<------- How do I check if I'm hitting a character object or just an object casted to a character type?
	}
}
}

 

 

 

A friend of mine suggested that I compare the sizeof(Character) to the sizeof(hitTarget). I thought this would work at first, but then I realized that since I'm casting to a character type from byte*, the size of hitTarget would always be the same as a Character type.

 

What do I do here?

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

Link to comment
Share on other sites

Just of the top of my head you could use SetEntityUserData to store a struct which contains the class pointer as one element and the class type as the 2nd.

 

for example

 

struct objectDetails
{
   byte* objectPointer;
   int   objectType;
}

 

where for instance objectTypes are 0 = character, 1 = vehicle so on and so forth

 

Then conditionally cast the byte* objectPointer to the class based on the objectType.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

That's a good point Flexman.

 

Assuming it's not a multiuser game though:

 

Another way that has occured to me is to have base class that all of your dynamic game objects classes are subsequently derived from (this is what I do in my game engine framework). The base class contains common data items such as the EntityID of the objects, the position, rotation, objectType etc. As all of your higher game objects classes (Vehicles, NPCs and so on) are derived from this base class then their pointers can be assigned to pointers of the base class. So you effectively have a universal way of casting a base class pointer to any of the derived object classes. The problem with this is that it only allows access to the methods and variables inherited from the base class but if you made one of those a classType variable in which you stored the derived class type at the time its created then you could access this and again conditionally cast the same base pointer to the devived class type getting full access.

 

Conversely, you could assign a different EntityType for each type of your dynamic game object types and distinguish between them using GetEntityType(PickedEntity) in order to apply the correct cast.

 

Just some food for thought.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

Cannot simply use different collision types for the objects and so filter directly them with EntityPick (or also checking their entity type after picking)?

Didn't I just say that lol. No harm in repeating it I guess ;)

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

 

Conversely, you could assign a different EntityType for each type of your dynamic game object types and distinguish between them using GetEntityType(PickedEntity) in order to apply the correct cast.

 

 

 

Cannot simply use different collision types for the objects and so filter directly them with EntityPick (or also checking their entity type after picking)?

 

Mind = Blown. Didn't even think of this.

simpleSigPNG.png

 

Programmer/Engineer/Student

www.reikumar.com

 

2.6 GHz Intel Core Duo - nVidia GeForce 8600 GT - Windows 7 64-bit - 4 Gigs RAM

C++ - Visual Studio Express - Dark GDK - Leadwerks SDK

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