Objects recognizing when another object is nearby

0 favourites
  • 11 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • I'm having trouble figuring out how to get an object to detect another object of the same type at an offset. Here's a test project I've set up to show my problem, hopefully I'm making some dumb and obvious mistake here.

    Event page:

    i.imgur.com/sRKfZJR.png

    Layout:

    i.imgur.com/LzvY9i9.png

    So my goal here is to get each instance of the car sprite object to notice when something is in front of it so that it can react accordingly. I'm going to have a lot of these sprites in my game at once, so it'd be nice if I didn't have to compute distance between all of them each tick. I thought the overlaps on offset was perfect, but I guess not?

    Another point, in my real project file, the sprites seem to react to a second object (which is why I included the 'cone' sprite) but here it doesn't seem to register it? Though if I enable the action on the event page that moves the cone, it gets detected just fine.

    I've been wrestling with this for a while and I'm just not sure if I'm doing it the wrong way or what. Can someone please give me some suggestions?

    Here's a link to the .capx that the pictures came from: dropbox.com/s/lrxpi6ym5niysf0/CarSelfDetection.capx

  • Well, I can show you how I deal with this. It detects objects in a square shape rather than a circle, which is what you seem to be doing (i think)

    <img src="http://puu.sh/3ALqr.png" border="0" />

    Basically, if the fish is within 5 pixels (10 pixels squared for the entire area) then the event will trigger.

  • Right, but your DestinationX/Y are stationary. I'm looking for any car sprite that is near any other car sprite in one specific direction (Sprite.Angle in this case).

    Unless I'm mistaken your solution won't solve my problem.

  • I had this problem some time ago.

    Here are 3 examples I've found on the forums. Should help.

    app.box.com/s/yf102y7q0l97qvczsilz

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Right, but your DestinationX/Y are stationary. I'm looking for any car sprite that is near any other car sprite in one specific direction (Sprite.Angle in this case).

    Unless I'm mistaken your solution won't solve my problem.

    No, I use this same thing with Player.X and Y. It doesn't have to be stationary at all.

    One other way you can do it is to pin a invisible sprite to the car and when that sprite collides with an object, register the detection (think of it as "line of sight" detection)

  • I had this problem some time ago.

    Here are 3 examples I've found on the forums. Should help.

    https://app.box.com/s/yf102y7q0l97qvczsilz

    Thanks, I'm at work right now, but I'll check these when I get home.

    No, I use this same thing with Player.X and Y. It doesn't have to be stationary at all.

    One other way you can do it is to pin a invisible sprite to the car and when that sprite collides with an object, register the detection (think of it as "line of sight" detection)

    I think that perhaps I have explained my problem improperly. I have a bunch of instances of the same object (in this case a car) on my layout and I want to know when one of them is in front of the other. It is not the player and there's say, 100 of them around at all times. To the best of my knowledge, I can't pin an invisible sprite to each of them (using event code) to use that to check for collisions. If I can, that would be great though. How do I pin 100 invisible sprites to 100 cars though?

  • megatronx,

    I checked out those .capx files you linked me and while they were informative, they haven't led me to a solution for my problem. Although they did make me learn about Families, so that was good. Thanks.

    Upon reflection, I was thinking that maybe my initial test project wasn't representative enough of what I want to accomplish, so I've replaced it with a new one which is much more in line with what I'm trying to do.

    Download it here: dropbox.com/s/lrxpi6ym5niysf0/CarSelfDetection.capx

    If you can't download it, here's some screenshots.

    Layout (The car facing left is the one which accelerates)

    <img src="http://i.imgur.com/6Ke1LTF.png" border="0" />

    Event Sheet

    <img src="http://i.imgur.com/GyWJKFS.png" border="0" />

    Basically, there's a bunch of cars and one of them starts to accelerate (instance variable crashed == false). Every tick, for each car in the layout, I check the area in front of the car and if it is not overlapping another car at offset then I accelerate. If there is a car detected then I stop. Unfortunately, as you can see if you run the project, the car does not stop.

    How do I make the car stop?

  • This is best done with an invisible detector (left not-so-invisible so you can see it for this example).

    CarSelfDetection_invisibledetector

    Edit: Oops, didn't read the full thread. Here's one with multiple cars moving.

    CarSelfDetection_invisibledetector_multi

  • This is best done with an invisible detector (left not-so-invisible so you can see it for this example).

    http://blackhornettechnologies.com/Construct2Stuff/CarSelfDetection_invisibledetector.capx

    Oh snap! Thanks Blackhornet! This is exactly what I needed.

    Looking over the project you uploaded, I think my problems were that I wasn't understanding entirely how the "Pick" conditions worked and I didn't even think about using UIDs like that. I'll go read up on these features more now.

    Thank you very much, again.

  • Actually you can replace the 'Create/Set position' with a Spawn, to save a step. Also the ColDetUID instance-variable on the Car isn't used and can be removed.

  • 'm going to have a lot of these sprites in my game at once, so it'd be nice if I didn't have to compute distance between all of them each tick. I thought the overlaps on offset was perfect, but I guess not?

    I'm honestly not sure that overlap is less expensive than distance checking.

    To check the distance between two points, the formula is (and it's what c2 use):

    distance(xa,ya,xb,yb) = sqrt((xa-xb)^2 + (ya-yb)^2)

    basically you have 4 multiplications, 1 addition and one square root operation.

    The square root operation is known to be a bit slow

    Now for an overlap check, depending on the complexity of the shapes and the fact that they are rotated or not, it can be easy, or less easy.

    But the basic "quad is overlapping quad" function more or less check if one is contained inside the other, and then if not, it checks if there's an interesection between each segments.

    The operations used are fairly simple but it something of the order of

    "number of vertices of object A TIMES number of vertices of object B"

    So it can get pretty iffy.

    Now, the only thing that is a bit unknown here is the speed of a square root operation. But you know, you don't even have to use a square root, you can yourself check if:

    (xa-xb)^2 + (ya-yb)^2 <  someDistance^2

    Then you have your cheap distance check.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)