Jump to content

I need help figuring some math out


TrashBoat
 Share

Recommended Posts

So i have been working on this 2d physics game and added some physics to it but got stuck on this problem that the character ( the cube ) is sliding all the time but it should not do that because of this friction function:

If $horizontalVelocity > 0 Then
        If $inAir Then $currFriction = $airFriction
        $horizontalVelocity -= $currFriction
        $birbX += $horizontalVelocity
EndIf

this line "$horizontalVelocity -= $currFriction" should nullify the velocity to 0 but it stays somewhere in 0.09. How do i fix this?

Here's the game's source code and what i have done so far:

Spoiler
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <Misc.au3>

;~ HotKeySet("{SPACE}", "_up")
;~ HotKeySet("d", "_right")
;~ HotKeySet("a", "_left")

Opt("GUIOnEventMode", 1)

_GDIPlus_Startup()
$width = 500
$height = 300
$hGUI = GUICreate("Bootleg Physics Sim", $width, $height)

$birbX = $width / 2
$birbY = $height / 2

$gravity = 0.3
$velocity = 0
$lift = -6

$horizontalVelocity = 0
$force = 0.7
$friction = 0.3
$airFriction = 0.2
$currFriction = 0

$inAir = True

$value = False


$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
$hBitmap = _GDIPlus_BitmapCreateFromGraphics($width, $height, $hGraphic)
$hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
_GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2)
_GDIPlus_GraphicsClear($hBackbuffer, 0xFFF0F0F0)
GUISetState()
GUISetOnEvent(-3, "_Exit")
$hBrush = _GDIPlus_BrushCreateSolid(0xFFFF0000)
$hPen = _GDIPlus_PenCreate(0xFF000000)


While 1
    ;dont touch dis
    _GDIPlus_GraphicsClear($hBackbuffer, 0xFFF0F0F0)

    ;controls
    If _IsPressed(20) Then
        _up()
    EndIf

    If _IsPressed(41) Then
        _left()
    EndIf

    If _IsPressed(44) Then
        _right()
    EndIf

    ;draw our cube
    _GDIPlus_GraphicsDrawRect($hBackbuffer, $birbX, $birbY, 10, 10)

;~  _GDIPlus_GraphicsDrawLine($hBackbuffer,$width / 2,0,$birbX+5,$birbY+5,$hPen)

    $velocity += $gravity
    $birbY += $velocity
    $value = False

    ;horizontal movement
    ;right
    If $horizontalVelocity > 0 Then
        If $inAir Then
            $currFriction = $airFriction
        Else
            $currFriction = $friction
        EndIf
        $horizontalVelocity -= $currFriction
        $horizontalVelocity = Int($horizontalVelocity * 100) / 100 ; gives 2 digits after the decimal point
        If $horizontalVelocity < 0.3 Then $horizontalVelocity = 0
        $birbX += $horizontalVelocity
    EndIf

    ;left
    If $horizontalVelocity < 0 Then
        If $inAir Then
            $currFriction = $airFriction
        Else
            $currFriction = $friction
        EndIf
        $horizontalVelocity += $currFriction
        $horizontalVelocity = Int($horizontalVelocity * 100) / 100 ; gives 2 digits after the decimal point
        If $horizontalVelocity > -0.3 Then $horizontalVelocity = 0
        $birbX += $horizontalVelocity
    EndIf

    ;y collision
    If $birbY < 0 Then
        $birbY = 0
        $velocity = 0
        $value = True
    EndIf

    If $birbY + 11 > $height Then
        $birbY = $height - 11
        $velocity = 0
        $value = True
        $inAir = False
    EndIf

    ;x collision
    If $birbX < 0 Then
        $birbX = 0
        $horizontalVelocity = 0
        $value = True
    EndIf

    If $birbX + 11 > $width Then
        $birbX = $width - 11
        $horizontalVelocity = 0
        $value = True
    EndIf


    ;data output
    _GDIPlus_GraphicsDrawString($hBackbuffer, "Velocity: " & $velocity, 1, 2, "Arial", 8)
    _GDIPlus_GraphicsDrawString($hBackbuffer, "Coliding: " & $value, 1, 12, "Arial", 8)
    _GDIPlus_GraphicsDrawString($hBackbuffer, "X: " & $birbX, 1, 22, "Arial", 8)
    _GDIPlus_GraphicsDrawString($hBackbuffer, "Y: " & $birbY, 1, 32, "Arial", 8)
    _GDIPlus_GraphicsDrawString($hBackbuffer, "HVelocity: " & $horizontalVelocity, 1, 42, "Arial", 8)
    _GDIPlus_GraphicsDrawString($hBackbuffer, "inAir: " & $inAir, 1, 52, "Arial", 8)


    ;update stuff dont touch
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $width, $height)
    Sleep(3)
WEnd

Func _up()
    If Not $inAir Then
        $velocity += $lift
        $inAir = True
    EndIf
EndFunc   ;==>_up

Func _right()
    $horizontalVelocity += $force
EndFunc   ;==>_right

Func _left()
    $horizontalVelocity -= $force
EndFunc   ;==>_left

Func _Exit()
    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_BitmapDispose($hBitmap)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc   ;==>_Exit

 

Now i don't expect you reading my nasty code and understanding what it does but any help would be appreciated.:D

 

Edit: I guess the problem is solved, i have updated the source code if anyone needs it.

Edited by TrashBoat
Fixed Movement
Link to comment
Share on other sites

Try this tweak.

$horizontalVelocity -= $currFriction
        $horizontalVelocity = Int($horizontalVelocity * 100) / 100 ; gives 2 digits after the decimal point
        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $horizontalVelocity = ' & $horizontalVelocity & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console

You've run into the problem with dealing with floating point numbers. This tweak will multiply the result by 100, get just the integer and then divide that by 100. It's a bit more forgiving of floating point math.

Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Im guessing because if it slips below zero as a result of that math then it goes through the next If statement where you add the airfriction to the horizontalvelocity whether or not it is in the air, and then do no further checks of the return.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Yeah I've just decided to hard code so if the float is smaller than 0.3 or bigger than -0.3 (thanks to @BrewManNH for the rounding up numbers snippet) reset the velocity to 0

$horizontalVelocity = Int($horizontalVelocity * 100) / 100 ; gives 2 digits after the decimal point
If $horizontalVelocity < 0.3 Then $horizontalVelocity = 0
$horizontalVelocity = Int($horizontalVelocity * 100) / 100 ; gives 2 digits after the decimal point
If $horizontalVelocity > -0.3 Then $horizontalVelocity = 0

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...