March 28, 2024, 01:44:15 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


question... 3d sprite and collision

Started by J B Wood (Zumwalt), November 22, 2006, 11:45:23 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

J B Wood (Zumwalt)

C2DSprite has collision testing, does C3DSprite have the same collision testing?
I want to check if my missiles hit anything and didn't know if I had to do dummy testing or if there was already a 3dsprite collision method.

Thanks

Ionic Wind Support Team

Nope.   You can use rectangle intersection tests, they are the simplest and for something like invaders would do fine.   The Windows API even has a function that does it for you:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/rectangl_1mpg.asp

The 3D sprites were designed initialliy for HUD and overlays.  I may add collision testing to them at a later date but you have to remember that they do have the dependancy of the 3D engine DLL.   The 2D library was designed specifically for making 2D games which is why it has two collision testing methods.

Ionic Wind Support Team

J B Wood (Zumwalt)

November 22, 2006, 12:29:41 PM #2 Last Edit: November 22, 2006, 12:45:26 PM by Jonathan (zumwalt) Wood
Thanks, this is more than a 5 minute fix so will have to wait until I get home.
I'll have to write a routine to do it.


declare import, IntersectRect(LPRECT lprcDst, RECT *lprcSrc1, RECT *lprcSrc2),BOOL;

J B Wood (Zumwalt)

I had a nanosecond, here was the result, and this works...


//The RECT structure defines the coordinates of the upper-left and lower-right corners of a rectangle.
//
//typedef struct _RECT {
//  LONG left;
//  LONG top;
//  LONG right;
//  LONG bottom;
//} RECT, *PRECT;
//Members
//left
//Specifies the x-coordinate of the upper-left corner of the rectangle.
//top
//Specifies the y-coordinate of the upper-left corner of the rectangle.
//right
//Specifies the x-coordinate of the lower-right corner of the rectangle.
//bottom
//Specifies the y-coordinate of the lower-right corner of the rectangle.
declare import, IntersectRect(LPRECT lprcDst, RECT *lprcSrc1, RECT *lprcSrc2),BOOL;

sub testCollide(),int
{
RECT destRect;

RECT *src1 = NEW(RECT,1);
src1->left=0;
src1->top=0;
src1->right=2;
src1->bottom=2;

RECT *src2 = NEW(RECT,1);
src2->left=2;
src2->top=2;
src2->right=4;
src2->bottom=4;

BOOL res;
res=IntersectRect(destRect,src1,src2);
MessageBox(NULL,STR$(destRect.left),"Collision Check");
}


Change the source and destination around for there edges. The above produces 0 on the message box because the 2 boxes do not intersect, the below produces a 1.


declare import, IntersectRect(LPRECT lprcDst, RECT *lprcSrc1, RECT *lprcSrc2),BOOL;

sub testCollide(),int
{
RECT destRect;

RECT *src1 = NEW(RECT,1);
src1->left=0;
src1->top=0;
src1->right=2;
src1->bottom=2;

RECT *src2 = NEW(RECT,1);
src2->left=1;
src2->top=1;
src2->right=3;
src2->bottom=3;

BOOL res;
res=IntersectRect(destRect,src1,src2);
MessageBox(NULL,STR$(destRect.left),"Collision Check");
}


Don't forget your sub main :)

Ionic Wind Support Team

Dont forget anything you NEW you also have to DELETE.

When you see RECT *lpRect somewhere it doesn't have to be an allocated object, it is the address of a rect structure. 


declare import, IntersectRect(RECT *lprcDst, RECT *lprcSrc1, RECT *lprcSrc2),BOOL;

sub testCollide(),int
{
RECT destRect,src1,src2;

  src1.left=0;
  src1.top=0;
  src1.right=2;
  src1.bottom=2;

  src2.left=1;
  src2.top=1;
  src2.right=3;
  src2.bottom=3;

BOOL res;
res=IntersectRect(&destRect,&src1,&src2);
MessageBox(NULL,STR$(destRect.left),"Collision Check");
}


& means 'address of'.  Technically you don't need it in Aurora, as the compiler knows to take the address of a structure when passing it as a parameter.

Paul.
Ionic Wind Support Team

J B Wood (Zumwalt)

Thanks Paul, going to troll over the code some more, I have added imgheight and imgwidth to all my structures that have a c3dsprite object.
I am assuming that the x,y values are the upper left of the image anyway.
This is going to simplify my life for the game.

I now have a main menu in place, a pause and resume, a pause to main menu, and a quit for the game.
Trying to get the new game piece done for the game_state object.

Whats cool about the pause is that you don't loose your place in the game, when you resume, it just continues where it left off.

J B Wood (Zumwalt)

I am having a lapse of conscious here, in Aurora, when we pass a pointer into a method, if we make changes to the object that pointer is linked to, are those changes made to the original object or only in the scope of the method?

This is where my by value vs by reference is getting confused with Aurora.
Example code:


sub AlienVsBunker(CPointerList AlienList,CPointerList BunkerList)
{
// test collision between the aliens and the bunkers
// return the AlienList and Bunker list back out of the routine
}


Now in this block, if I make a change to anything in the AlienList that I pass in, and a change to anything in the BunkerList that is passed in, do those changes stay?

Do I have them declared right in my sub?

Ionic Wind Support Team

Classes follow the same rules as structures.  They are passed by reference unless specifically declared BYVAL.  Which means you are just passing the address of the object and working with the object that was passed, not a copy of it.

So there is no need to return it back. 

this:
sub AlienVsBunker(CPointerList AlienList,CPointerList BunkerList)
{
// test collision between the aliens and the bunkers
// return the AlienList and Bunker list back out of the routine
}

Is the same as

sub AlienVsBunker(CPointerList *AlienList,CPointerList *BunkerList)
{
// test collision between the aliens and the bunkers
// return the AlienList and Bunker list back out of the routine
}

Paul.
Ionic Wind Support Team

J B Wood (Zumwalt)

Thanks Paul, this is exactly what I was hoping to hear.

J B Wood (Zumwalt)

What if I want to pass a structure in as a parameter, but I don't know which structure I want to use?
Would this work?

sub CollideCheck(CPointerList objList1, pointer inStructure1, CPointerList objList2, pointer inStructure2)
{
}


Where the inStrucutre1 or inStructure2 could be a player structure, an alien structure, a bullet structure, or a bunker structure.

Ionic Wind Support Team

use a union and a 'type' element as the first element of each structure.  Or even just diseparate structures with a 'type' element.


enum struct_types { ALIEN_S,BULLET_S,BUNKER_S}

struct alien_structure
{
    struct_types type;
    int x, y;
    ...
}

struct bullet_structure
{
    struct_types type;
    float count;
    ...
}

bullet_structure *pBullet = NEW(bullet_structure, 1);
pBullet->type = BULLET_S;

sub CollideCheck(CPointerList objList1, pointer inStructure1, CPointerList objList2, pointer inStructure2)
{
     if(*(int)inStructure2 == BULLET_S)
     {
     }
}


Its an old coding trick that relys on the fact that the first element of a structure has the same address as the structure itself.  So typecasting it to an INT accesses the 'type' element without knowing what kind of structure was passed.

Many ways to solve the problem, thats just the first one to pop into my head.

Paul.
Ionic Wind Support Team

J B Wood (Zumwalt)

Thanks again Paul, it gives me something to work with.

J B Wood (Zumwalt)

November 23, 2006, 06:19:56 PM #12 Last Edit: November 23, 2006, 06:22:00 PM by Jonathan (zumwalt) Wood
New problem:


sub ActivateShip(CPointerList *ShipList)
{
pointer pos;
sPlayer *pPlayer;
int tmpActivated=0;
pos = ShipList->GetFirst();
while(pos != NULL)
{
pPlayer = ShipList->GetData(pos);
if(pPlayer->currentHP == pPlayer->maxHP)
{
if(tmpActivated=0)
{
pPlayer->Activated=1;
tmpActivated=1;
pPlayer->x=0;
pPlayer->y=screen_height-120;
pPlayer->Ship.SetPosition(pPlayer->x,pPlayer->y);
}
}
pos = ShipList->GetNext(pos);
}
}


When I pass it my shiplist, the ship does get moved into play but when I escape out of the game by hitting ESC, I gett an error:

AppName: animation2d.exe    AppVer: 0.0.0.0    ModName: animation2d.exe
ModVer: 0.0.0.0    Offset: 00234a86

Doesn't make sense to me ... continuing to try to debug it..


EDIT:

LOL nevermind, typical of me, I created a new pointerlist at the top, never used it, and deleted it when I quit the program without ever using it, after removing it all together, everything is ok.