Computer Science Canada

[Tutorial] Calculating Square Boundaries

Author:  tjmoore1993 [ Sat May 09, 2009 12:39 pm ]
Post subject:  [Tutorial] Calculating Square Boundaries

What is collision?
A collision is an isolated event in which two or more bodies (colliding bodies) exert relatively strong forces on each other for a relatively short time.


Why do we need it?
Games and applications without an occasional hit would be boring. Hit? Well most games now are fighting based and when things are hit they would have to respond right? When a programmer programs a game or application they must (not always the case) have to implement a collision detection to make sure if characters or buttons are touched they do what they are suppose to do.


What kind of games or applications?
In Turing specifically we have fighting and shooting kind of games which will need some sort of collision. Applications will normally have buttons either implemented by a GUI or through "Pic.Draw" function which would need a command to make sure the actual object its self is being toggled properly.


Getting started
The easiest way to get started is to grab a picture that isn't so big but roughly around 100x100. This is so that we have enough space to get an output of the important information.

Turing:

var x1, x2, y1, y2 : int
var Picture : int := Pic.FileNew ("C:\\Example.bmp")

put "Enter in your starting X-Value."
get x1
put "Enter in your starting Y-Value."
get y1

x2 := Pic.Width (Picture) + x1
y2 := Pic.Height (Picture) + y1

put "X-Left : ", x1
put "X-Right   : ", x2
put "Y-Bottom : ", y1
put "Y-Top   : ", y2


This will help you figure out your boundaries by taking the x1 and y1 and adding it accordingly

Example:
code:
x1 = Left Wall
y1 = Bottom Wall

Left Wall + Picture's Width = Right Wall
Bottom Wall + Picture's Height = Top Wall


In order to test that our values are correct we must setup a statement. There is a problem though we do not want to waste time setting up a crash of some sort so we will use the simple mouse method.

Example:
Turing:

var x1, x2, y1, y2 : int
var Picture : int := Pic.FileNew ("C:\\Example.bmp")
var x, y, click : int

put "Enter in your starting X-Value."
get x1
put "Enter in your starting Y-Value."
get y1

cls

x2 := Pic.Width (Picture) + x1
y2 := Pic.Height (Picture) + y1

View.Set ("offscreenonly")
loop
    cls
    Mouse.Where (x, y, click)

    put "Picture Information : "
    put "X-Left   : ", x1
    put "X-Right  : ", x2
    put "Y-Bottom : ", y1
    put "Y-Top    : ", y2
    put ""
    put "Mouse Location : "
    put "x     : ", x
    put "y     : ", y
    put "click : ", click

    Pic.Draw (Picture, x1, y1, 0)

    if click = 1 then
        if x >= x1 and x <= x2 and y >= y1 and y <= y2 then
            put "OBJECT CLICKED"
        end if
    end if
    View.Update
end loop


That's it for my tutorial if you have any questions feel free to ask!


: