Determing x,y position of nearest collision object

0 favourites
  • 8 posts
From the Asset Store
Hand-painted tiles, objects, animated objects, and background to build a colorful Mayan civilization environment.
  • Hello lovely people of the Construct 2 forum!

    I'm fairly new around these parts, and have just started developing my dream game which I am confident (and I hope not unduly) will be amazingly fun to play when I finish it. Think Sonic the Hedgehog meets Fez.

    Anyway, other than some basic experience with GameMaker when I was a young teenager programming is utterly alien to me, although having just completed a masters in philosophy I'd hope to think that solving logical problems is not. With that in mind I hope somebody out there can help me with the following problem:

    Basically I have a ball rolling on top of various sloped surfaces, all of which are physics objects. If the ball is BALL1 and the slope sprite which BALL1 is rolling on at this particular moment in time is SLOPE1, I need to figure out a way of determining the x,y co-ordinates of the bit of SLOPE1 which is closest to BALL1. i.e How do I figure out the position of the bit of the slope which the ball is currently touching? I'd love to go into more detail about why I need to know this but as somebody who is completely new to game development I'm still not familiar with how much of my 'concept' I should be making public and how much I should be keeping under wraps. The reason I need to know this is for a fairly integral gameplay mechanic after.

    Anyway, sorry I haven't given you much to go on in terms of letting you in on the actual end-result problem I need solving, but I'm confident that if anybody can help me with this specific mathematical/logical problem I'll be able to get the game to do what I need it to. I would treat whoever showed me the light with regard to this problem as I would somebody who had pledged me �1000 on Kickstarter. Really, without being able to figure this out I can't progress with development on my game. I will be eternally grateful to whoever can give me some guidance here!

    Thank you so much!

    PantuTheDog

  • Hi, I'm not quite sure what you're trying to do, but is it necessary to find out where the ball is on the actual slope rather than the screen itself? If you just track the x and y coordinates of the ball on the screen then you should be able to use that info to do various things - for example you could pick a slope nearest to the ball and then spawn an object at the ball's current x & y coordinates.Example I may be missing the mark here, but it's hard to tell without knowing more about what you're trying to achieve.

  • Sorry, maybe I didn't make myself quite clear. It is necessary for me to know the x,y position of the point at which the Ball is touching whichever surface it is touching at that point in time. Knowing the x,y position of the ball isn't enough, because the x,y position of the ball will only tell me where the centre of the ball is; not the x,y position of the edge of the ball which is touching the nearest surface. For instance, if the Ball's co-ordinates are thus: Ballx = 100, Bally = 100, and the ball has a diameter of 20, and there is a flat surface directly beneath the ball on which it is rolling, then the point at which the physics collision is occuring (the point I need to know) would be something like x=100, y=110. However if the surface the ball is rolling on is NOT directly beneath the ball or is curved then figuring out the co-ordinates of this collision point is much more tricky for me.

    Is there no way Construct 2 can automatically return the co-ordinates of a physics collision between two objects; i.e the point at which they are touching?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In your example the position being detected is always the position BELOW the centre of the ball, which is fine when the surface on which the ball is rolling is perfectly flat, but as the angle of the surface changes such that the ball is now rolling up or down a hill (or even around a loop) I need to be able to detect the point on the edge of the ball which is touching the surface. Hope this makes it more clear!

  • Ahh yes, I see what you mean now. I believe construct classic had the condition "object overlaps point" which appears to be missing from construct 2 at the moment. Hopefully soon we'll have a more robust collision system.

  • I don't know how you've set up your slopes.

    If they are square objects you could probably calculate it with some math.

    on collision spawn a sprite at ball origin

    move at angle :angle(ball.x,ball.y,slope.x,slope.y)

    distance : distance(ball.x,ball.y,slope.x,slope.y)-ball.radius

    Could be I'm completely mistaken (happens a lot).

  • instead of using pixel perfect detection, you could pin some detector sprites on the circle, when the collision happens you have your xy value, more detectors more accurate

  • Thanks everyone. No-one's offered a perfect way to do what I need done yet, but Vtrik's suggestion should get what I need done for now, at least in a rudimentary way. It'll be fine for getting the ball to jump off surfaces in the right direction, but later in development I'll need a more precise way of calculating which side of the ball is touching surfaces so I can get it to gravitate towards the part of whichever surface it is touching, kind of like a ball of velcro rolling along velcro slopes.

    Still, a proper implementation in the next update of Construct 2 of the system being able to return the x,y co-ordinates for the point of collision would be very much appreciated. I'm sure it would come in handy for plenty of developers in a variety of conceivable situations.

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