Gamestudio Links
Zorro Links
Newest Posts
Executing Trades on Next Bar Open
by vicknick. 06/13/24 08:51
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (Ayumi), 1,170 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19059 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Collision #298414
11/14/09 21:14
11/14/09 21:14
Joined: Mar 2009
Posts: 186
V
Valdsator Offline OP
Member
Valdsator  Offline OP
Member
V

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. tongue Or maybe there's some tutorial lying around that I didn't see.

Thanks in advance.

Re: Collision [Re: Valdsator] #298622
11/16/09 14:39
11/16/09 14:39
Joined: Aug 2007
Posts: 1,922
Schweiz
Widi Offline
Serious User
Widi  Offline
Serious User

Joined: Aug 2007
Posts: 1,922
Schweiz
Use events for that.

my.emask = ENABLE_ENTITY | ENABLE_BLOCK;
my.event = explode_function;

Re: Collision [Re: Widi] #299572
11/25/09 00:27
11/25/09 00:27
Joined: Mar 2009
Posts: 186
V
Valdsator Offline OP
Member
Valdsator  Offline OP
Member
V

Joined: Mar 2009
Posts: 186
Uhh, I don't really understand. Can someone please give me an example in code of something touching something else and a function being activated? Are there things you must set up before being able to do this sort of collision, or what?

Thanks.

Re: Collision [Re: Valdsator] #299858
11/27/09 17:58
11/27/09 17:58
Joined: Nov 2009
Posts: 34
S
Sepiantum Offline
Newbie
Sepiantum  Offline
Newbie
S

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.

Code:
//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: Sepiantum] #299861
11/27/09 18:04
11/27/09 18:04
Joined: May 2008
Posts: 2,113
NRW/Germany
alibaba Offline
Expert
alibaba  Offline
Expert

Joined: May 2008
Posts: 2,113
NRW/Germany
@Sepiantum
WOW! But thats not helpful for a beginner.

@valdsator
you must make something liek this:
if you use lite-c:
my.emask|=ENALE_IMPACT; //React if touched
my.event=nameoffunction; //Do this, if collided


Professional Edition
A8.47.1
--------------------
http://www.yueklet.de
Re: Collision [Re: alibaba] #299869
11/27/09 19:12
11/27/09 19:12
Joined: Nov 2009
Posts: 34
S
Sepiantum Offline
Newbie
Sepiantum  Offline
Newbie
S

Joined: Nov 2009
Posts: 34
LOL. I am a beginner. I just have good imagination. So visualizing stuff is easy for me.

Re: Collision [Re: Sepiantum] #299880
11/27/09 20:51
11/27/09 20:51
Joined: May 2008
Posts: 2,113
NRW/Germany
alibaba Offline
Expert
alibaba  Offline
Expert

Joined: May 2008
Posts: 2,113
NRW/Germany
Anyway really good code for a beginner wink


Professional Edition
A8.47.1
--------------------
http://www.yueklet.de
Re: Collision [Re: alibaba] #300755
12/04/09 21:39
12/04/09 21:39
Joined: Mar 2009
Posts: 186
V
Valdsator Offline OP
Member
Valdsator  Offline OP
Member
V

Joined: Mar 2009
Posts: 186
Edit:Think I've figured it out...

Last edited by Valdsator; 12/04/09 21:46.
Re: Collision [Re: Valdsator] #300757
12/04/09 21:46
12/04/09 21:46
Joined: May 2008
Posts: 2,113
NRW/Germany
alibaba Offline
Expert
alibaba  Offline
Expert

Joined: May 2008
Posts: 2,113
NRW/Germany
its right like you use it, but you forget the " | ". i must be like this:
my.emask |= ENABLE_IMPACT;
my.event = collide;


Professional Edition
A8.47.1
--------------------
http://www.yueklet.de
Re: Collision [Re: alibaba] #300758
12/04/09 21:56
12/04/09 21:56
Joined: Mar 2009
Posts: 186
V
Valdsator Offline OP
Member
Valdsator  Offline OP
Member
V

Joined: Mar 2009
Posts: 186
Actually, it was just an error in my naming of the entities. tongue 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.

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1