Need help with collision detection
Author |
Message |
JavaMan
|
Posted: Fri Dec 07, 2012 1:50 pm Post subject: Need help with collision detection |
|
|
Can someone help me with my collision detection in my game:
Turing: | View.Set ("graphics:550;650,nobuttonbar")
const Gravity := 1
var x := 16
var y := 16
var y_velocity := 0
var keys : array char of boolean
var timerunning : int
var score := 0
loop
%---Time/Score Counters---%
clock (timerunning )
Draw.FillBox (0, 595, 550, 600, black)
Draw.FillBox (0, 0, 650, 5, black)
locate (1, 53)
put "Time: ", timerunning / 1200 : 0 : 1, " sec"
locate (2, 52)
put "Score: ", score
locate (3, 51)
put x, ", ", y
%---Movement---%
Input.KeyDown (keys )
if keys (KEY_UP_ARROW) and y <= 16 or keys ('w') and y <= 16 then
y_velocity := 25
end if
if keys (KEY_LEFT_ARROW) and x > 12 or keys ('a') and x > 12 then
x - = 5
end if
if keys (KEY_RIGHT_ARROW) and x < 536 or keys ('d') and x < 536 then
x + = 5
end if
if keys (KEY_ESC) then
cls
exit
end if
y_velocity - = Gravity
y + = y_velocity
if y < 16 then
y := 16
y_velocity := 0
end if
%---Collision Detection---%
if x < 400 and x > 300 and y > 165 then
y := 165
y_velocity := 0
end if
Draw.FillBox (300, 175, 400, 180, black)
%---Power Ups---%
Draw.FillOval (x, y, 10, 10, red)
View.Update
delay (25)
cls
end loop |
I cant figure out how to get collision detection for the blocker to work properly.
Mod Edit:
Pleas wrap you code in either of the following in order to preserve whitespace (indentation) and to highlight the syntax.
code: |
[syntax="turing"] ... code ... [/syntax]
[code] ... code ... [/code ]
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Fri Dec 07, 2012 9:56 pm Post subject: RE:Need help with collision detection |
|
|
When you punch a wall, your hand doesn't go through the wall, then magically be set outside of it. Instead, you don't even reach where your hand would be if it remained at the same velocity.
Right now, you're moving the (x,y) coordinates during your input stage. Instead hold off until reaching the logic stage. Due to which, you try to set the player outside of the object in a specific position.
Exhibit A: A general game engine loop.
code: |
loop:
% Input
% Logic
% Draw
|
Basically you should only change the velocity during the input stage, then later check where the entity will end up first. If it will end up somewhere it doesn't collide with anything, you can move it there.
Example:
code: |
entity.position.x = ...
entity.position.y = ...
entity.velocity.x = ...
entity.velocity.y = ...
loop:
% Input
if onKeyPress():
% Eg: Right arrow
playerControlledEntity.velocity.dx = +playerControlledEntity.speed
% Logic
for each entity:
% Create temporary variables to find out where the entity will end up.
p.x = entity.position.x + entity.velocity.x
p.y = entity.position.y + entity.velocity.y
if entity being at p.x collised with something (collision detection):
% Don't move
else:
% Move
entity.position.x = p.x
entity.position.y = p.y
% Draw
...
|
Note that this method is also flawed, but this should help get the ball rolling. |
|
|
|
|
|
|
|