3 registered members (TipmyPip, AndrewAMD, dBc),
18,430
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Collision
#298414
11/14/09 21:14
11/14/09 21:14
|
Joined: Mar 2009
Posts: 186
Valdsator
OP
Member
|
OP
Member
Joined: Mar 2009
Posts: 186
|
I'm trying to get an entity to explode when the player spaceship touches it. Problem is, I have no idea how I would do a if(hit) { do stuff } I looked at the f1 help, and I know it has something to do with c_trace, but I'm still confused. Could someone just write an example that basically shows, if this entity is hit by this entity, do something. I'll make sure not to copy and paste.  Or maybe there's some tutorial lying around that I didn't see. Thanks in advance.
|
|
|
Re: Collision
[Re: Valdsator]
#299858
11/27/09 17:58
11/27/09 17:58
|
Joined: Nov 2009
Posts: 34
Sepiantum
Newbie
|
Newbie
Joined: Nov 2009
Posts: 34
|
This is probably the crudest collision engine that I ever wrote, but it should work. I would advise anybody experienced with coding to check over my work.
//have SHIPWIDTH, SHIPHEIGHT, ENEMYWIDTH, and ENEMYHEIGHT defined somewhere
int index;
for(index = 0;index < NUM_ENEMIES;index++){
//check if ship is able to be checked for collsion along the y axis
if(ship.y + SHIPHEIGHT/2 < enemy[index].y - ENEMYHEIGHT/2 &&
ship.y - SHIPHEIGHT/2 > enemy[index].y + ENEMYHEIGHT/2){
if(ship.x + SHIPWIDTH/2 > enemy[index].x - ENEMYWIDTH/2){
ship.x = enemy[index].x - ENEMYWIDTH/2 - SHIPWIDTH/2;
}
else if(ship.x - SHIPWIDTH/2 < enemy[index].x + ENEMYWIDTH/2){
ship.x = enemy[index].x + ENEMYWIDTH/2 + SHIPWIDTH/2;
}
}
//check if ship is able to be checked for collision along the x axis
if(ship.x + SHIPWIDTH/2 > enemy[index].x - ENEMYWIDTH/2 &&
ship.x - SHIPWIDTH/2 < enemy[index].x + ENEMYWIDTH/2){
if(ship.y - SHIPHEIGHT/2 < enemy[index].y + ENEMYWIDTH/2){
ship.y = enemy[index].y + ENEMYHEIGHT/2 + SHIPHEIGHT/2;
}
else if(ship.y + SHIPHEIGHT/2 > enemy[index].y - ENEMYHEIGHT/2){
ship.y = enemy[index].y - ENEMYHEIGHT/2 - SHIPHEIGHT/2;
}
}
}
What this basically does is stop your ship from going through your enemy. Well, that is what I think it does. Play around with the code.
Last edited by Sepiantum; 11/27/09 17:58.
|
|
|
Re: Collision
[Re: alibaba]
#300755
12/04/09 21:39
12/04/09 21:39
|
Joined: Mar 2009
Posts: 186
Valdsator
OP
Member
|
OP
Member
Joined: Mar 2009
Posts: 186
|
Edit:Think I've figured it out...
Last edited by Valdsator; 12/04/09 21:46.
|
|
|
Re: Collision
[Re: alibaba]
#300758
12/04/09 21:56
12/04/09 21:56
|
Joined: Mar 2009
Posts: 186
Valdsator
OP
Member
|
OP
Member
Joined: Mar 2009
Posts: 186
|
Actually, it was just an error in my naming of the entities.  But I've got it now, and I discovered that I need to use c_move so the entity can actually react. Why it was turning back to 0 tilt was actually because the naming of the entity was before it told it to go to tilt 90, so once it crashed, it didn't continue with the action. Thanks, now I can finally continue my game! Edit:One more question, how would I make it that only specific entities may activate the function? Because I don't want my ship to be getting hit by it's own bullets and such.
Last edited by Valdsator; 12/04/09 21:57.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|