Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Colision handleing between Class Objects

Author
Message
Moops
14
Years of Service
User Offline
Joined: 19th Apr 2010
Location:
Posted: 17th May 2010 22:15
Hiya guys, with a few more C++ hours under my belt, ive ventured out into OOP element of c++.

I'll try to explain my problem and hope you can help.

I have 2 classes created:
Enemy
Bullet

Both classes handle themselves from creation to deletion, including collision detection. However the problem im having is that when 2 objects collide because each class handles it's own collision routine only the first object in the loop is removed and the second object carries on as if it hasn't been hit. However sometimes it is removed, it's intermittent (hit 'm miss), pardon the punt.

Ive attached the small project and you can see how this plays.

Cheers

Moops

Attachments

Login to view attachments
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 17th May 2010 22:53
I haven't gotten to look at the code yet, but I assume that you know boh objects involved with the collision? if so, have the first one tell the second one about it. Ie: obj2->HandleCollision(this);

I'll take a closer look later but this approach may work...

JTK
Moops
14
Years of Service
User Offline
Joined: 19th Apr 2010
Location:
Posted: 17th May 2010 23:59
Hey JTK,

you are correct, collision is known about both objects, that is until one is deleted. Generally the bullet is deleted first and I could tell the enemy class that it is no longer alive and kill it, but this is were my knowledge lacks im afraid, how to enable the bullet class to access a function in the enemy class.

I probably need to create pointers between the 2 classes somehow, or create another higher level class which enemy and bullet inherit from.

apologies for the lack of explanation, im only 5weeks into C++ and trying to explain something I don't quite understand can be extremely frustrating

Cheers guys

Moops
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 18th May 2010 01:44
This is my third attempt at answering lol, I kept going into maps of pointers and stuff but I don't want to bog you down.
I suppose the general point was that you can create a collision handler class, this would be a more oop approach. How you give that class access to your bullets and enemys is up to you, I wont go into that yet.
With classes its all about planning the design so you know which jobs belong to which classes, how they access/message each other and how they may be related.

Sorry for being a little vague.

JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 18th May 2010 02:14
I'm not saying this is the right way to do it, but using your own code, I've made some modifications... Have a look at the changes and see if you can figure out what I'm doing...

What you've got isn't a bad start for a first attempt, but as matty was saying, planning the design is very important!

Ask away if you have any questions!

JTK

Attachments

Login to view attachments
Moops
14
Years of Service
User Offline
Joined: 19th Apr 2010
Location:
Posted: 18th May 2010 13:17
Many thanks JTK, the alterations to the code work well.

I appreciate the comments Matty regarding planning and I guess as my experience grows with C++ this experience will enable me to plan. Right now it's all trial 'n error, unfortunately for you guys you get the brunt of it will silly basic questions.

I'll scrap the learning code and try and create something with a little more thought. I've attached a simple Jpeg image of a class layout, would you guys mind commenting on it, am I in going in the right direction given my end goal is a top down shooter.

Is this a silly thing to start off with, am I barking mad?

Cheers

moops

Attachments

Login to view attachments
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 18th May 2010 15:00


I knocked this up really quick with violet UML editor, you may want to look into getting it or something similar, its free.

All the arrows here represent inheritance, I have not put arrows in for any other kind of relationship as you will see, the Engine and collision handler do not inherit from anything here although they will be linked to the Objects in some way.

Its the relationships between classes/objects that is most important, inheritance is only one type of relationshiip. An Object in my diagram represents anything that has a position-rotation in world space among other similarities so they will share alot of functionality.

This is a quick diagram without much thought, do not take it to be a good thoughtful design for a game engine

Moops
14
Years of Service
User Offline
Joined: 19th Apr 2010
Location:
Posted: 18th May 2010 16:26
Thanks matty, nice free little app.

Login to post a reply

Server time is: 2024-07-07 00:04:28
Your offset time is: 2024-07-07 00:04:28