collision point

Discussion and feedback on Construct 2

Post » Sun Jun 21, 2015 3:17 am

someone must have asked this question but I can't find it..

I have a sprite shooting a laser.. when the laser hits another sprite I spawn a "hit sprite" which fades out.. looks like a hit effect. I make the end of the laser bolt be imagepoint(1).. so On Collision I set the new hit effect to be at that image point.. the only problem is when something collides with the middle of the laser, it spawns the hit effect no where near where it actually hit! I would have the sprite that gets hit just spawn the hit effect.. but it's pretty large so the same problem will occur.

I just need to know the coordinates of the collision.. it seems totally possible right?? Or do I have to do some sort of Pick overlap point??
B
98
S
32
G
16
Posts: 1,204
Reputation: 16,715

Post » Sun Jun 21, 2015 5:58 pm

One approach is to move the laser backwards until it's not overlapping the other object, then the end of the laser would be the collision point.
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Sun Jun 21, 2015 6:52 pm

Search for the rex_light behavior. I believe that it does what R0J0 said automatically. You could use it on the laser sprite it self, or set it on an invisible "detector" sprite just to get the hit.x and hit.y positions.
composer - multimedia artist
www.eli0s.com/en/
B
69
S
27
G
6
Posts: 1,146
Reputation: 10,379

Post » Sun Jun 21, 2015 11:07 pm

@R0J0hound the problem is.. the shooter is moving, therefore the laser is pinned to the shooter (where it grows from). And it grows really quick. The object thats being shot at is also moving. The laser after fully grown just fades out. I do a bunch of them, slightly overlapping one another.. like a sort of auto-laser.

so the potential objects that the laser could hit, or rather objects that could collide with the laser while it's fading out could easily hit the laser somewhere in the middle as they are going by the extended laser beam. This laser never shrinks (I do have others that do).

So there's no way to detect collision x/y?? I find that hard to believe.
B
98
S
32
G
16
Posts: 1,204
Reputation: 16,715

Post » Sun Jun 21, 2015 11:59 pm

The fact the laser is growing fast or that the objects are moving is not really a problem. The same idea can be used. If the laser at any point overlaps the object, then in with a loop make the laser a little shorter until it's not overlapping the object. At that point the end of the laser is the collision point.

Another way would be to do a raycast with math, which basically amounts to calculating the intersection points of the laser line and the edges of the object and keeping the closest intersection.

You could also utilize the chipmunk behavior as it has some stuff built-in to get the collision point. Vanilla C2 collision detection only finds if two objects intersect, at no point is a collision point calculated so you'll have to use one of the above approaches.
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Mon Jun 22, 2015 1:15 am

R0J0hound wrote:If the laser at any point overlaps the object, then with a loop make the laser a little shorter until it's not overlapping the object. At that point the end of the laser is the collision point.


ah okay..I think I see what you are saying-- have the object cut off the laser (like a hand in front of a flashlight) and grow it until they hit the object.. I will try it..

R0J0hound wrote:You could also utilize the chipmunk behavior as it has some stuff built-in to get the collision point. Vanilla C2 collision detection only finds if two objects intersect, at no point is a collision point calculated so you'll have to use one of the above approaches.

Well that's too bad because it seems like the OnCollision Trigger "knows" there was a collision and must know where it is on the screen. Too bad it doesn't return or populate a system variable like: Sprite.OnActiveCollisionX and Sprite.OnActiveCollisionY
B
98
S
32
G
16
Posts: 1,204
Reputation: 16,715

Post » Mon Jun 22, 2015 1:56 am

@R0j0hound how would you do a math raycast in C2? Collision polygon vertices are not accessible to the runtime, or can plugins accomplish that?

@jobel , one option is to use an iterative raycast method. I believe the best way to do this way is with a binary search, there's example code here:
https://www.scirra.com/tutorials/902/li ... raycasting
B
92
S
31
G
24
Posts: 3,191
Reputation: 32,689

Post » Mon Jun 22, 2015 2:26 am

Well that's too bad because it seems like the OnCollision Trigger "knows" there was a collision and must know where it is on the screen. Too bad it doesn't return or populate a system variable like: Sprite.OnActiveCollisionX and Sprite.OnActiveCollisionY

It doesn't know where the collision occurred, it would have to be calculated in js in the same way as listed here.

@sqiddster
A plugin could access the collision polygon. The physics, polygon and chipmunk already do internally. In just events you could place imagepoints in the places of the collision polygon points for a brute force way. Then you can do line intersections with:
http://paulbourke.net/geometry/pointlineplane/

I can find a capx where i've implemented it if needed. A plugin could be made to hide the complexity, but I don't enjoy the process of updating and fixing plugins currently.
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Mon Jun 22, 2015 3:38 am

thanks @sqiddster that looks like a cool capx.. thanks for sharing that. In my case (a space game) all the "solids" are interactive things, so it doesn't save me any performance. I also need to use on Collision opposed to Overlapping because I don't want it to continuously trigger. plus my laser is more like an automatic gun.. it fires long pulses (but beams) and spawns lasers on top of lasers, each laser is somewhat like a 'bullet' and lasts about 0.25 seconds.

my much simpler solution is: OnCollision with the laser, redraw the laser to the x/y of the colliding object and have the object spawn the "hit effect" (and you have to pin it incase the object is moving fast!). It ends up putting the laser "into" the object more, instead of stopping at the edge. I have an overhead view, so it works. If I end up using this weapon against large sprites (512x512) that's probably not going to work. So I may use that raycaster logic yet!
B
98
S
32
G
16
Posts: 1,204
Reputation: 16,715

Post » Mon Jun 22, 2015 3:53 am

R0J0hound wrote:It doesn't know where the collision occurred, it would have to be calculated in js in the same way as listed here.
bummer..thanks for letting me know..
B
98
S
32
G
16
Posts: 1,204
Reputation: 16,715

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 9 guests