Help with Collision and platform generation in Turing
Author |
Message |
ThgilFoDrol
|
Posted: Thu Nov 08, 2012 11:56 am Post subject: Help with Collision and platform generation in Turing |
|
|
Hey, trying to make a game where you control a ball using arrow keys or any other keys to stay alive by bouncing off the platforms that generate. So far, I have the platforms randomly generating, and I'm planning to add commands to make the ball actually move. How would I be able to use forloops and counting loops to make about 7-8 platforms generate and be on the screen at any given moment, and allow the platforms to move up? What commands are there to make collision physics with platforms you don't exactly know the locations of it? Also, the platforms seem to be getting thinner as they progress, any fixes? Thanks!
Turing: |
setscreen ("noecho")
var x, y, r1, r2, c : int
x := 175
y := 300
r1 := 15
r2 := 15
var platformx, platformy, platformx2, platformy2 : int
put "Choose the colour of your player."
get c
randint (platformx, 0, 400)
randint (platformy, 0, 0)
randint (platformx2, platformx, 400)
randint (platformy2, platformy, 40)
loop
drawfilloval (x, y, r1, r2, c )
drawfillbox (platformx, platformy, platformx2, platformy2, 255)
randint (platformx, platformx, platformx2 )
randint (platformy, platformy, platformy2 )
randint (platformx2, platformx, 400)
randint (platformy2, platformy2, platformy2+ 50)
delay (750)
cls
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
QuantumPhysics
|
Posted: Thu Nov 08, 2012 9:33 pm Post subject: Re: Help with Collision and platform generation in Turing |
|
|
Well I am not sure this will work in turing the same as it does with Allegro and C++, but hell, it should - You can just use if statements by simply saying
if (player >= (platformY2 + 5) && player > platformX1 && player < platformX2){
player = platformY2 + 5;
}
using that format you can make the ball, or player, or whatever you use to move never go beneath the platform (NOTE: It must be in that same order no different)
You set the boundaries for the player. It will not leave the boundaries once it hits the platform, unless the player falls off.
in turing the if statements are if-then format, so you would just write if (player >= (platformY2 + 5) && player > platformX1 && player < platformX2) then
ignore the "{}" they are used in C++ instead of then.
Do you get the explanation? Does it answer your question for the "physics"
RE: Also, be sure to read into View.Update(), by just typing it into the search bar on this forums. In turing it is apparently used to stop the flickering when shapes are drawn over and over again really fast (usually in loops) |
|
|
|
|
![](images/spacer.gif) |
jr5000pwp
|
Posted: Fri Nov 09, 2012 12:49 am Post subject: Re: Help with Collision and platform generation in Turing |
|
|
I'm assuming you are using a rectangle to represent the ball? If so, then it isn't too hard to do collision. The way I would do it would be as such:
Turing: |
%Set the movement distances to be the maximum the player can move this frame.
var minX : int := moveThisFrameX
var minY : int := moveThisFrameY
%Loop through all your blocks (You can do some more advanced checks to only check possible tile collisions.)
for i : 1 .. rectangles.Count
%This is the distance to the currently iterated block.
var distance : int
%If the player is moving down, you only need to check the top side, if your moving up, you only need to check the bottom.
if(moveThisFrameY< 0) then
%Calculate the distance to the edge of the tile
distance:=rectangles (i ). max.Y - playerPositionY
else
distance:=rectangles (i ). min.Y - (playerPositionY+playerHeight )
end if
%If this distance is less than the previous minimum, then make the minimum this distance
if(distance<minY ) then
minY:=distance
end if
end for
%Increment the players position by the mimimum vertical movement and do the same for the horizontal
playerPositionY+=minY
for i : 1 .. rectangles.Count
var distance : int := int.maxValue;
if(moveThisFrameX< 0) then
distance:=rectangles (i ). max.X - (playerPositionX-playerWidth/ 2)
else
distance:=rectangles (i ). min.X - (playerPositionX+playerWidth/ 2)
end if
if(distance<minX ) then
minX:=distance
end if
end for
playerPositionX+=minX
|
Note that I am assuming a custom datatype is pre-made, or is to be made for a rectangle. If it needs to be made, it can be done along these lines:
Turing: |
%A type for a point
type point:
record
X : int4
Y : int4
end record
%A type for a rectangle, with min and max representing the x and y values of the sides.
type Rectangle:
record
min : point
max : point
end record
|
That is a pretty hard method of doing collision, so if you don't understand it, feel free to ask. You don't have to use it either by any means, but it's a nice technique to know. I wish I did in grade 10 when I was doing my final project. |
|
|
|
|
![](images/spacer.gif) |
ThgilFoDrol
|
Posted: Fri Nov 09, 2012 11:47 am Post subject: RE:Help with Collision and platform generation in Turing |
|
|
Getting problems with incorporating the code, is there an easier method that's newbie friendly I can try? Not experienced with Turing =( Thanks for the input though, going to play with it. |
|
|
|
|
![](images/spacer.gif) |
jr5000pwp
|
Posted: Fri Nov 09, 2012 1:34 pm Post subject: RE:Help with Collision and platform generation in Turing |
|
|
It was an example, you will have to make changes to get it to work, but it is an example of one of the ways to do it. Essentially, you will end up wanting an array of rectangles (or 4 arrays of x1, y1, x2, y2) and then to go through the array and run your collision on each one. There are definitely simpler ways than the one I suggested, such as QuantumPhysics one.
The best way to learn how to do collision is to figure out how it works. If you have a rectangle, and you determine that you will collide with it that frame, you can either a) prevent the player from moving or b) determine the position to snap the player to. My method uses snapping, which looks better but is a lot harder to incorporate.
I would recommend for you to try and figure it out on paper, draw a bunch of rectangles, and see what calculations you are making every time you move the player, what do you check to see if it collided, what do you do if it collided, and how do you handle multiple collisions? |
|
|
|
|
![](images/spacer.gif) |
|
|