Math in GameDev

0 favourites
  • 13 posts
From the Asset Store
Aliens are invading our planet! Solve the math question, and attack the alien with your laser. Covers addition, subtract
  • I was curious what uses there are for different math functions.

    Like take trigonometry, sin() tan() cos(), all very good for getting angles between 2 different positions, and moving objects in a smooth sinewave motion.

    What uses in gamedev are there for log(), or using powers or square roots, etc.? I'm curious to learn!

    Bonus points if it's useful for something unachievable with what C3 already provides (e.g. Sin/cos to move a sprite at an angle is already possible with the "Move At Angle" action, so not best example).

  • Probably too many to list. Math functions and operators are just so you can use any formula you can find. Depending on what you do they all can be used for game dev.

    But as a small example: log is used to convert the sound volume decibels to percent and vise versa. Another use for log is when converting a large number into 10b for example.

    The exact formulas can should be able to be found by a forum search.

  • Try using online graphing calculator and put some formula and observe the value and also the visual it produce. You probably want to implement them into motion or other object property. Learning arithmetic would be useful too to understand how certain numbers sequenced/ordered since I relied on them to build puzzle logic.

  • Thanks for ideas. I guess I was curious at the more fundamental "what could it be used for" then work backwards from there. Like E.G. Exp(), we can punch some numbers in and use this and observe it is a value that gets... exponentially higher depending on the value you input, but then what I am curious about, is where in 2D game mechanics might this be used?

    I asked chatgpt (rather not, it's cooler to get ideas from people) and it gave lots of examples but I don't trust if it's true. Some were true, like it said sin/cos can be used for a pendulum motion. But then it said log() and exp() could be good for either a decay system, or an RPG levelling system to set the next goal for experience points to reach. Even if this was true, seems like you could just achieve the same effect with multiplication, is there a situation where its better to use exp/log over multiplaction perhaps? Is there a situation where exp/log must be used to achieve an effect/motion/etc.?

    Why would you use ln(), why would you use unlerp() (I've seen unlerp somewhere once, but can't recall what game mechanic it was for).

    I suppose I ask this because my mind sometimes grasps things better when I imagine a result, then work backwards deconstructing it from there.

  • There isn’t really any specific 2d game related uses of those math functions. It’s easy to shotgun possible uses but it won’t cover all cases.

    For leveling up you could go with whatever strikes your fancy. The player won’t care if you use exp(), some simple multiplication level to level or some other curve.

    Generally there are more than one way to do things.

    Most of the functions such as exp(), ln(), and log10() aren’t needed unless you are using some formulas you found online.

    Unlerp() isn’t really used much. Last place I’ve seen it used was with a scroll bar but in general I’ve mostly just used the math behind it.

    Anyways. Most math functions and operators have too many possible uses to list. I usually go the other way, decide what I want to do and figure out how math can be utilized to do that.

  • Ahh I follow, thanks for the input! It probably explains why chatgpt kept saying most math functions can be used for a level up system, which seemed unusual. Like you say, most things can be done in more than one way. I suppose a lot can be achieved with basic operators anyway, and what matters is if the end result looks correct regardless of how it's achieved.

    I haven't touched the different math operators I listed for like 15 years. Maybe I was hopeful something would unlock a whole world of usefulness, much like sin/cos unlocking the world of angles and circles. I have a rough idea what they do, maybe one day I'll come across a use case within game dev.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • If math can be equated to cooking, it’s easier to list good recipes than list recipes that contain a certain ingredient. Maybe you’re more after some useful math formulas that aren’t covered with the expressions provided in construct.

    Here’s a partial list of the ones I find useful on a regular basis. They aren’t covered by expressions normally. Admittedly a fair amount have to do with physics and a few geometry problems. But they can be useful.

     // speed and angleOfmotion from vx,vy
    speed = distance(0,0,vx,vy)
    angleofmotion = angle(0,0,vx,vy)
    
    // vx,vy from speed and angle of motion a. 
    vx = speed*cos(a)
    vy = speed*sin(a)
    
    // it is also possible to represent directions as a unit vector (an xy vector with a length of 1) instead of with angles. so with a general velocity of vx,vy you can get the speed and direction vector with:
    speed = distance(0,0,vx,vy)
    dirX = vx/speed
    dirY = vy/speed
    // doing that gives the advantage of eliminating sin and cos from the formulas if you so desire.
    
    // bounce angle a along normal n
    a = 2*(n+90)-a
    
    // velocity along a direction a
    vx*cos(a)+vy*sin(a)
    
    // stop velocity along an angle a. 
    vrel = vx*cos(a)+vy*sin(a)
    vx = vx - vrel*cos(a)
    vy = vy - vrel*sin(a)
    // multiply vrel by 2 to to make it bounce instead. 1.5 would be a half bounce...etc
    
    // velocity of a point px,py on a spinning object with position x,y velocity vx,vy and angular speed w. 
    velocityX = vx - (py-y)*w*pi/180
    velocityY = vy + (px-x)*w*pi/180
    
    //distance from line to point. with start sx,sy and angle a and point px,py
    abs((px-sx)*cos(a-90)+(py-sy)*sin(a-90))
    // remove the abs and the sign will give what side of the line youre on. 
    
    // closest point on line segment. from point px,py to line segment x0,y0 x1,y1
    t = clamp(((px-x0)*(x1-x0)+(py-y0)*(y1-y0))/((x1-x0)^2+(y1-y0)^2), 0, 1)
    position = lerp(x0,x1,t), lerp(y0,y1,t)
    // remove the clamp to treat the segment as an infinite line. 
    
    // normalize an angle to the -180 to 180 range. 
    angle(0,0,cos(a),sin(a))
    
    // convert any angle to the 0 to 360 range
    (a%360+360)%360
    
    // signed angle differece between a and b. number of degrees between angles, but the sign gives the direction. negative for counter clockwise. More useful than constructs angleDiff expression. 
    angle(0,0,cos(a-b),sin(a-b))
    
    // rotate object a degrees around center cx,cy
    set position to (x-cx)*cos(a)-(y-cy)*sin(a)+cx, (x-cx)*sin(a)+(y-cy)*cos(a)+cy
    rotate a degrees clockwise
  • Wow that was an unexpected and extremely useful list! Thank you!! And thank you for the insightful wisdom, it's genuinely appreciated.

  • I'd recommend a good game development maths textbook if you want to learn more - and there's a good one available free online at gamemath.com! I have the hardback edition and it's proved useful.

  • Thanks for the recommendation! Plenty to read and keep me busy.

  • lerp() It is very commonly used by everyone.It allows us to move more smoothly

    lerp(Self.Angle, TargetAngle, 10 * dt)
    
    lerp(Self.Angle, TargetAngle, 1 - 0.000001 ^ dt)
    
    anglelerp(Self.Angle, TargetAngle, 1 - 0.000001 ^ dt)
    

    ---

    Unlerp() can gives you the percentage of point between two values.

    unlerp(sprite.bboxleft, sprite.bboxright, mouse.x)
    

    If yout run it will find that the value is between 0 and 1. Of course, even if it goes out of range, it will still continue to go negative, or go past 1. It can be used to make scroll bars.

    Since this is a "progress", it can also be used to create animation functions similar to Timeline. For example,If used together with lerp, can be used to map one range of numbers to another range of numbers.

    lerp(0, 200, unlerp(sprite.bboxleft, sprite.bboxright, mouse.x))
    

    Think of this 0 and 200 as the properties of the object. As long as you control the progress, for example, hang it on the Tween value, it can proceed at the expected pace.

    I made a example:

    cdn.discordapp.com/attachments/225550155531812865/1136725591329083525/UI_Mapping_Object_Event.c3p

  • exp() It seems that it can also be used to handle camera zoom

    twitter.com/XorDev/status/1703873385946607924

    LayoutScale *= exp(Mouse.WheelDeltaY * 0.1)
    

    ---

    ln() Can be used to help us convert pow() into exp(). You know that The pow() function is not provided in C3.

    zoom_target *= pow(0.8, Mouse.WheelDeltaY)
    
    zoom_target *= exp(ln(0.8) * Mouse.WheelDeltaY)
    

    ---

    sign() Can be used to determine whether a value is positive or negative. Therefore, it is useful to use it in places with Vector attributes such as Platform or 8 direction behavior to mirror the animation. You can easily see it in the built-in examples.

    Set width = sign(PlayerCollision.Platform.VectorX) * Self.ImageWidth
    
  • Isn't that pow() is the same as a^n ?

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