A small coding question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

A small coding question

Post 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);
Image
A Person
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 10
Joined: Mon Jul 12, 2010 3:12 am
Current Project: iPhone 2D Game
Favorite Gaming Platforms: Wii, NDS, iPhone, 360
Programming Language of Choice: Objective-C
Location: Calgary, AB

Re: A small coding question

Post 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;
}
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: A small coding question

Post 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?
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: A small coding question

Post 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 :\).
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: A small coding question

Post 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
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: A small coding question

Post 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).
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: A small coding question

Post by X Abstract X »

I look forward to hearing back with your results.
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: A small coding question

Post 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).
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: A small coding question

Post 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.
Last edited by X Abstract X on Tue Jul 13, 2010 5:47 am, edited 1 time in total.
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: A small coding question

Post 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.
Image
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: A small coding question

Post 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.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: A small coding question

Post 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.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: A small coding question

Post by Scoody »

The angle will not always be 90 degrees at the player to make use of pythagoras ...
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: A small coding question

Post 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.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
Kyosaur
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Tue Jul 13, 2010 2:00 am
Favorite Gaming Platforms: PS2,PS3,NDS
Programming Language of Choice: C++

Re: A small coding question

Post 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 :|).
Image
Post Reply