• Advertisement
Sign in to follow this  

Applying Mathematical Functions and creating game Formulas

This topic is 567 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi there, i'm beginner in creating/developing games. so i figured out that i should build a simple game, maybe a clone to hone my skills in programming and game design. the game of choice was a simple PONG game.

 

the game simply consists of

1-Paddle

2-ball      

 

my problem is that i can't figure out how to make a formula that makes the ball rebound with speed based on

1-the moving speed of the paddle

2-the direction where the paddle was moving

3-the speed of the ball when it hit the paddle

 

However my question isn't how to solve this specific problem, but i want a general way/method about how to Create mathematical formulas whenever i need one. i don't know what is that even called so i couldn't find any answer just using google. 

so giving me any link, material, tutorial, exercises, technique or Even the correct keywords to make me able to search this topic will be highly appreciated

 

note: after searching i found one answer which i think kinda fits my question which was:

which i wasn't able to apply to my game, cause i just don't know how to do that. for instance is the linear function mentioned here is this y=mx+c if so how to apply this to my game, can i make it take more than two parameters? if not what does linear functions mean and how to create one?

 

this is the problem i stuck with and i don't know where to begin to solve this problem is it by doing more math exercises or learning some more abstract math.... i hope that i made my self clear.

 

of course you can ask me for any more info.

Share this post


Link to post
Share on other sites
Advertisement
This is a mixture of plain geometry mathematics and a bit of game-design hacking. It's not possible to be able to craft a mathematical formula for absolutely everything, but if you study school-level mathematics to a decent level then you'll be able to spot which approaches you need in each situation.

How to calculate the standard reflection angle is very simple - look up the "law of reflection" or the "angle of reflection". With Pong, it's even simpler, because if the bat is aligned to an axis, you only have to reflect one axis for the ball - which usually means changing x to -y or y to -y.

Adding on a factor to include the movement of the paddle is also theoretically simple - if you have the paddle movement as a vector, and you have the ball direction as a vector, you can add them together. You probably want to scale down the paddle movement vector a bit before adding it to the reflected ball vector.

Share this post


Link to post
Share on other sites

ball movement in pong is basic rectilinear physics. all the formulas have been known for hundereds(?) of years.

 

ball movement based on paddle movement is actually a game hack, not really realistic physics - unless you get into inelastic collisions where the ball and paddle deform slightly on contact, creating more contact time for the paddle to impart a shear force on the ball. but such shear forces would tend to induce spin, not change trajectory. and i've never heard of a pong clone that modeled ball spin.

 

 

 

but i want a general way/method about how to Create mathematical formulas whenever i need one.

 

look it up, there are already formulas for just about everything - at least everything in the real world.   if no formula exists, then the discipline you'll be wanting is "modeling and simulation", or "mathematical modeling". those are the disciplines of "making up new formulas".

 

 

 

when making games, if you need a formula for something that is in the real world, 90% or more of the time its going to be a physics formula. 

 

and if its graphics related, odds are the formula will come from analytic geometry, or linear algebra in the case of 3D.   

 

so when it comes to formulas for games, physics, analytic geometry, and linear algebra are going to be your big three go to disciplines for turn key formulas.

Edited by Norman Barrows

Share this post


Link to post
Share on other sites

Writing a formula is a bit like writing a sentence... it can be long and complex or short and simple, there are multiple ways to express the same thing, you can have two nearly identical expressions with two vastly different results. I don't think there is any way to algorithmically generate expressions that give a specific output, you have to build each formula on a case-by-case basis.

 

Find a formula with the basic shape you want. Linear, oscillating, tappered, bell curved etc... doesn't matter.

take

Y=f(x) as your basis function

Y'=f'(x) as your modified function

 

To raise or lower a constant distance  Y=f(x)+c

To shift right or left a constant distance Y=f(x-c)

to stretch/shrink along the x axis Y=f(x*c)

to stretch/shrink along the y axis Y=f(x)*c

to travel the oppiste direction on the x axis Y=f(-x)

to travel the oppisite direction on the y axis Y=-f(x)

 

a generic formula which can apply or not apply some transformations

Y= (Y_Reflect/abs(Y_Reflect))*(Y_StrechCoefficent)*f((X_reflect/abs(X_reflect))*x-x_shift)+y_shift

 

When Y_Reflect<0 reflect vertically, when Y_reflect>0 don't modify output, when Y_reflect=0... function falls apart... constant output of 0

When X_Reflect<0 reflect vertically, when X_reflect>0 don't modify output, when X_reflect=0... function falls apart... constant output of f(0)

 
 
 
 

Of course this only works if you already have a function in mind that you want to modify in some way

Share this post


Link to post
Share on other sites

It's not possible to be able to craft a mathematical formula for absolutely everything

Exactly sir, that's why i'm asking about the science of creating mathematical formulas. just to use when i'm in a situation like this.

am using vectors and the reflection is done like if it hits the right/middle/left side of the paddle, the reflection is (1,-1) - (0,1) - (-1,1) respectively instead of the "law of reflection" .  (the paddle is in the horizontal axis)

that was relatively easy because there is someone already did this before me, but iam stuck whenever i try to create something  new. as that requires more knowledge than just being able to search for it directly.

 

in my case, it is making the ball reflect with some kind of speed that scales up or down depending on other factors instead of a fixed number (1) each hit.

 
but of Course thank you mr @[member='Kylotan'], for your Reply.

 

 

ball movement based on paddle movement is actually a game hack, not really realistic physics

that's true, that's why i'm  stuck.

 

then the discipline you'll be wanting is "modeling and simulation", or "mathematical modeling". those are the disciplines of "making up new formulas".

if that so, then its the answer am seeking.

 

thanks @[member='Norman Barrows']

 

 

@[member='Paragon123'] hmm, that's exactly the challenging part that am asking about how to compose this f(x) how to write and define it, what's its restrictions... etc etc. i think that demo calculator will be of great help.

 

thanks to all of you, now  i have some more places and areas to explore and a new tool to use when making games -or any other program actually.

Share this post


Link to post
Share on other sites

Learning how to apply your understanding of mathematical properties comes with practice solving problems using math.  That is not to say, repeatedly solving math formulas or substituting Real numbers in place of variables, but rather defining relationships using algebra for instance, or calculus, trigonometry, etc. and applying them to solving some other type of problem (e.g "Where should I draw this representation of a ball on a computer screen?").

 

So considering the words you used in your post:

value to grow

value to oscillate

 

 

You're seeking to use math to represent something changing.  In your Pong example, most likely to change the position of a ball on a screen.  Do you need a formula used to teach algebra to accomplish this task?  Maybe, maybe not.

 

The Slope Y-intercept formula y=mx+b is an abstract representation of a line on a coordinate plane.  In its most basic form, it takes three parameters the slope (m), the y-intercept (b) and any and all values for 'x' (x) and the result is the point (x,f(x)).  Let's consider what else it can represent.  It can represent Force as in F=MA, where F (force) is the output y value, A (acceleration) is the slope and M (mass) is the value along the x-axis.  It can represent the rate of change.  All very interesting, but do you need it to change the position of a ball on a screen?

 

The apparent linear motion of a ball on a screen is visible, however, the program is supplying both sides of the equation.  The simplest way to change the position of the ball is to increment or decrement the x and y directly by some number, speed for example.

var ball = {
    x : 1,
    y : 1,
    dx : 1,
    dy : 1,
    move : function () {
        this.x += this.dx;
        this.y += this.dy;
    }
}

ball.move();  // ball.x = 2 ball.y = 2
        

The ball moved from (1,1) to (2,2).  

The formula for that line from (1,1) to (2,2) is still y=mx+b.

2 = m(1) + b

 

Is the slope relevant, what about the y-intercept?  Not necessarily, but consider Paragon123's post.

Y = f(x)

f(x) = mx + b

 

You can 'stretch' along they y-axis by changing ''m", you can 'shift' along the y-axis by changing "b".  The net result of input f(1) for 'x' is y != 2 which can still be accomplished more simply by changing "dy" as appropriate.

 

Suppose I want the ball to move according to the speed of the ball, the board tilt and the speed of the paddle:

var tilt = {
    dy : 4
}

var paddle = {
    dx : 1
}

var ball = {
    x : 1,
    y : 1,
    dx : 1,
    dy : 1,
    move : function () {
        this.x += (this.dx + paddle.dx);
        this.y += (this.dy + tilt.dy);
    }
}

ball.move();  // ball.x = 3 ball.y = 6

The ball moved from (1,1) to (3,6), still y = mx + b, but we already know y and x and we do not need "m" or "b" to draw the ball on the screen.

6 = m(3) + b

 

Setting ball.x and ball.y by assignment is a 'formula' created to change the position of the ball by adding the motion of the ball (ball.dx, ball.dy), the table (tilt.dy), and the paddle (paddle.dx).  However, as written, the ball will always move according to paddle.dx.  Is that a feature or a bug?  You decide, then change the formula accordingly.

 

Hopefully I've demonstrated you do not necessarily need to apply any particular formula to a program, formulas can be created using a step-by-step approach to the non-math problem of "moving a ball", and that one approach is experimentation.

Share this post


Link to post
Share on other sites

I would go with vectors all the way. I mean: position + velocity + acceleration. That solves all your issues. All you have to do then is use the paddle velocity as ball's acceleration on every collision. Take a look at this beautiful series on Coding Math by Keith Peters. I still watch it sometimes to remind myself to keep things simple.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement