Page 1 of 2

A small coding question

Posted: Tue Jul 13, 2010 3:03 am
by Kyosaur
Hi, im new to your forum (stumbled across your youtube vids and became hooked lol) :D.

My question is more math orientated than programing, regardless its something i drastically need help with.

Im making a soccer script, and i'm having a small issue that i just cant work out. Its sort of hard to explain so i made a picture to help me out:

Image

Basically, when a player kicks the ball (the distance is the radius of the circle), i check to see if where the ball would have landed is inside of my area. If it is, i simply move the ball there; however if its not, i WANT to move it only to the circumference of the circle (this is to simulate collision detection) and bounce it back in to our soccer field (bouncing is done, dont worry about that lol).

Is there anyway to do this with: Player coordinates; Player's facing angle; Area center coordinates; Area radius; and the coord's where the ball WOULD'VE landed ? Keep in mind this isnt for C(++), its for a single threaded programing language (Doing the "collision detection" like this is to eliminate the need for a timer).

Trigonometry functions available:

Code: Select all


/*
Angle modes:

radians,
degrees (proffered),
grades
 
*/

native Float:floatsin(Float:value, anglemode:mode=radian);
native Float:floatcos(Float:value, anglemode:mode=radian);
native Float:floattan(Float:value, anglemode:mode=radian);

Re: A small coding question

Posted: Tue Jul 13, 2010 3:40 am
by A Person
I'm not quite sure exactly what your doing here but what i would do would be to have a point in circle function where you pass in the soccer position and the circle origin and radius or something. Then if the point is not in the circle undo the last movement of the ball. I'm not going to explain point in circle function just google it.

Sorry about the objective-c

Code: Select all

if(![collisionDetection pointIsInCircle:soccerPosition circleOrigin:circlePosition withRadius:circleRadius]) {
soccerPosition.x -= ballVelocity.x;
soccerPosition.y -= ballVelocity.y;

ballVelocity.x = 0;
ballVelocity.y = 0;
}

Re: A small coding question

Posted: Tue Jul 13, 2010 3:46 am
by X Abstract X
So this isn't a realtime simulation where the ball has a speed and it's position is adjusted over time? You just want to do a calculation and move the ball to it's new position instantaneously?

Re: A small coding question

Posted: Tue Jul 13, 2010 4:05 am
by Kyosaur
A Person wrote:I'm not quite sure exactly what your doing here but what i would do would be to have a point in circle function where you pass in the soccer position and the circle origin and radius or something. Then if the point is not in the circle undo the last movement of the ball. I'm not going to explain point in circle function just google it.

Sorry about the objective-c

Code: Select all

if(![collisionDetection pointIsInCircle:soccerPosition circleOrigin:circlePosition withRadius:circleRadius]) {
soccerPosition.x -= ballVelocity.x;
soccerPosition.y -= ballVelocity.y;

ballVelocity.x = 0;
ballVelocity.y = 0;
}
Thank you for replying, but this doesnt help me at all :(. I'm just looking for a pure mathematical approach, as this will not work with my programing language (If anyone is curious its called PAWN, and is for a mulitplayer modification of san andreas / half life).
X Abstract X wrote:So this isn't a realtime simulation where the ball has a speed and it's position is adjusted over time? You just want to do a calculation and move the ball to it's new position instantaneously?
You got it :P. The coordinates have to be calculated before i can move the ball (To move the ball, i use a native function, which doesnt get me access to the X/Y velocities. So using the previous poster's method isnt possible :\).

Re: A small coding question

Posted: Tue Jul 13, 2010 4:27 am
by X Abstract X
I think I'm really close to solving this, as long as I understand correctly. Ok, here we go hope this can help but maybe not, it's 6:00am and I haven't slept yet. Warning: messy solution follows.

First, we need to see if the ball would land out of the circle.

Code: Select all

ballPotentialX = playerX + cos(playerToBallAngle) * circleRadius
ballPotentialY = playerY + sin(playerToBallAngle) * circleRadius

distanceFromCenterOfCircleToBall = sqrt{[absolute(ballPotentialX - circleCenterX)]^2 + [absolute(ballPotentialY - circleCenterY)]^2}

if (distanceFromCenterOfCircleToBall > circleRadius)
    placeBallOnCircumference();
Example picture of how to calculate the position of the ball on the circumference of the circle
Image

Direct link because the picture gets cut off in my browser: http://img707.imageshack.us/img707/4897/circlek.png

Re: A small coding question

Posted: Tue Jul 13, 2010 4:56 am
by Kyosaur
X Abstract X wrote:I think I'm really close to solving this, as long as I understand correctly. Ok, here we go hope this can help but maybe not, it's 6:00am and I haven't slept yet. Warning: messy solution follows.

First, we need to see if the ball would land out of the circle.

Code: Select all

ballPotentialX = playerX + cos(playerToBallAngle) * circleRadius
ballPotentialY = playerY + sin(playerToBallAngle) * circleRadius

distanceFromCenterOfCircleToBall = sqrt{[absolute(ballPotentialX - circleCenterX)]^2 + [absolute(ballPotentialY - circleCenterY)]^2}

if (distanceFromCenterOfCircleToBall > circleRadius)
    placeBallOnCircumference();
Example picture of how to calculate the position of the ball on the circumference of the circle
Image

Direct link because the picture gets cut off in my browser: http://img707.imageshack.us/img707/4897/circlek.png
Wow ty, that looks really promising :D. Let me wrap my head around it and convert it to PAWN, once its done i'll reply with the testing results :D (may take a little little while, as its 3 am / im kind of losing the ability to think).

Re: A small coding question

Posted: Tue Jul 13, 2010 5:03 am
by X Abstract X
I look forward to hearing back with your results.

Re: A small coding question

Posted: Tue Jul 13, 2010 5:23 am
by Kyosaur
X Abstract X wrote:I look forward to hearing back with your results.
I started the conversion to PAWN, when i relized that i was using X,Y,Z as the players coordinates, and that they are NOT coordinates lol / are angles. I dont have any means of getting these angles unfortunately, any way to work around this ? (everything in the key from my pic, i have access to).

It seems i REALLY messed up going with a circular area. I did it because i can fake the bouncing physics with the circular area simply by moving the ball from the circumference back to the center; which looks surprising realistic (speed always was a factor tbh; the distance check is a lot faster for circular than rectangular, and its easier to manage).

Re: A small coding question

Posted: Tue Jul 13, 2010 5:32 am
by X Abstract X
Hey that's too bad, ye I should have used greek letters to distinguish but I couldn't remember the alt-codes for any lol. Anyway, I'm completely confused by your situation. How can you have a player shoot a soccer ball in your game if you don't know the angle at which the ball gets kicked? I'm really confused about what your doing overall because you seem to have a bunch of restrictions and strange requirements.

angle y can be calculated as long as you know the player's position and circle center position.
angle z, I specified how to calculate in the diagram.
angle x can be calculated assuming you have the angle that the ball is kicked

I'm too tired now but if you need further help, I could attempt to write a full-in-code solution tomorow.

Re: A small coding question

Posted: Tue Jul 13, 2010 5:47 am
by Kyosaur
X Abstract X wrote:Hey that's too bad, ye I should have used greek letters to distinguish but I couldn't remember the alt-codes for any lol. Anyway, I'm completely confused by your situation. How can you have a player shoot a soccer ball in your game if you don't know the angle at which the ball gets kicked? I'm really confused about what your doing overall.
I have that ONE angle (its the players angle), here's the code i use "shooting":

Code: Select all

X += (SHOOT_DIST * floatsin(-A,degrees));
Y += (SHOOT_DIST * floatcos(-A,degrees));
X and Y are the players current position (there's a Z as well, however, the ball's z never changes, so not used). The SHOOT_DIST is a define (its the same as the area radius), and the A is the players angle.


EDIT:

Im pretty tired too, and have been struggling to form sentences that make sense lol. I'll check out your picture tomorrow and see if it makes more sense to me (been struggling with it, might be due to the sleep deprivation). Thanks for your help by the way :D.

Re: A small coding question

Posted: Tue Jul 13, 2010 6:38 am
by X Abstract X
It's almost 8:00am now, my head is spinning in all these sketches I'm making, I'm probably overcomplicating this. I'm going to bed, I'll have another look tomorow. I'm pretty sure I have this all worked out if I can figure out how to calculate one angle that I'm missing.

Re: A small coding question

Posted: Tue Jul 13, 2010 1:04 pm
by xiphirx
What you're trying to achieve is quite simple...

For your first conditional, calculate the distance of the ball from the center of the circle, if it is less than or equal to the radius, then it is inside.
For your second conditional, calculate the distance of the ball from the center of the circle, if it is greater than the radius, then
Image

Find X in that image, and set the balls position to the players position plus that amount.

Re: A small coding question

Posted: Tue Jul 13, 2010 2:22 pm
by Scoody
The angle will not always be 90 degrees at the player to make use of pythagoras ...

Re: A small coding question

Posted: Tue Jul 13, 2010 4:02 pm
by xiphirx
Scoody wrote:The angle will not always be 90 degrees at the player to make use of pythagoras ...
oh shit lmao, I'm a dumbass once again D:

EDIT: You may want to look into law of sines and cosines.

Re: A small coding question

Posted: Wed Jul 14, 2010 12:12 am
by Kyosaur
X Abstract X wrote:It's almost 8:00am now, my head is spinning in all these sketches I'm making, I'm probably overcomplicating this. I'm going to bed, I'll have another look tomorow. I'm pretty sure I have this all worked out if I can figure out how to calculate one angle that I'm missing.
Ok, keep me posted :D.

If you think it would be easier, i can change the area to a rectangular one (although if i do that, i lose my "bounce" affect, and will have to learn how to do basic physics :|).