# some movement code

Bresenham movement

32 replies to this topic

### #26 LillapojkenpćönOFFLINE

Lillapojkenpćön

Chopper Commander

• 156 posts

Posted Tue Nov 14, 2017 12:22 AM

I made an example of what I want to do, I have robots in my game that shoots missiles at you, but they can only shoot in six directions now and I would like if they shot in the exact direction you where when they fired the shot, I can do that but the problem is that the robots slowly speed up with 0.05 every one hundred points, and the missile automaticly speeds up to,

the missilespeed is the robotspeed + 0.10

So it would just be for speed flexibility, and the smoothness ofcourse, subpixel bresenham movement seems impossible in my brain but I thought I should ask,

what do you think?

Edited by Lillapojkenpćön, Tue Nov 14, 2017 1:16 AM.

### #27 Mountain KingOFFLINE

Mountain King

Dragonstomper

• 689 posts

Posted Wed Nov 15, 2017 5:18 AM

Bogox,

How would you handle 3 moving objects?

Player moves a cursor,

first object moves to that cursor when activated.

Second object follows and tries to catch the first object.

### #28 bogaxONLINE

bogax

Dragonstomper

• Topic Starter
• 787 posts

Posted Wed Nov 15, 2017 5:44 PM

I made an example of what I want to do, I have robots in my game that shoots missiles at you, but they can only shoot in six directions now and I would like if they shot in the exact direction you where when they fired the shot, I can do that but the problem is that the robots slowly speed up with 0.05 every one hundred points, and the missile automaticly speeds up to,

the missilespeed is the robotspeed + 0.10

So it would just be for speed flexibility, and the smoothness ofcourse, subpixel bresenham movement seems impossible in my brain but I thought I should ask,

what do you think?

The movement code always moves one pixel

The first thing I'd try is set up an accumulator for the fractional change of
position and when it's accumulated a pixel's worth do the movement

something like

```
temp = accumulator

accumulator = accumulator + speed

if accumulator < temp then skip_move

movement code

skip_move

```

### #29 LillapojkenpćönOFFLINE

Lillapojkenpćön

Chopper Commander

• 156 posts

Posted Thu Nov 16, 2017 8:58 PM

Seems like I don't have to? I have no idea how it works without setting the error accumulator and all the other stuff?

### #30 bogaxONLINE

bogax

Dragonstomper

• Topic Starter
• 787 posts

Posted Sat Nov 18, 2017 8:18 AM

Seems like I don't have to? I have no idea how it works without setting the error accumulator and all the other stuff?

I'm not sure what you're asking

You're setting all the important stuff

You don't neccesarily need to set the error accumulator
it just makes nicer lines (I think)

You count through the delta in the error accumulator until it rolls over
at which point you've accumulated a pixel worth of error so you move one
pixel in the minor direction
If you set the error accumulator to 1/2 the delta you're half a pixel
from where you want to be on the line and you can only be at whole pixels
If you set the error accumulator to 0 you'd be at one pixel from where
you want to be and the first thing you'd do is take a step in the minor
direction

The line is formed from horizontal or vertical line segments in the major
direction

Say the segment is going to be 8 pixels ie you'll move 8 pixels in the major
direction to accumulate one pixel of error in the minor direction
(the error accumulator counts down through the major delta in minor direction
deltas until it goes through zero) if you set the error accumulator to
0 you're one pixel away and you'll immediately take a step in the minor direction
If you set the error accumulator to 1/2 the major delta you'll take four steps
(half of 8 )  before you take a step in the minor direction
and if you set EA to the major delta you'll take 8 steps before you've
accumulated enough error for a step in the minor direction

something like this for a 9 pixel line

``` _
________

____
_____

________
_

```

as far as multiplying by 2 if you're working with numbers below 128
that just gives you a little more room for accuracy if you're going
to do something else with the error accumulator (since you're continuously
bending the line perhaps you'd want to take into account where you are
on the previous line) but it's really not neccesary (or even useful if
you're just setting the error accumulator to eg 1/2 the delta)

Edited by bogax, Sat Nov 18, 2017 8:19 AM.

### #31 bogaxONLINE

bogax

Dragonstomper

• Topic Starter
• 787 posts

Posted Sat Nov 18, 2017 8:38 AM

Bogox,

How would you handle 3 moving objects?

Player moves a cursor,

first object moves to that cursor when activated.

Second object follows and tries to catch the first object.

I'm not sure I know what you're asking

Simplest would be to have variables and
movement code for each object that needed it and reset up
an object whenever a its target changed position

However you might end up doing more setup than you needed
depending on how they're moving relative to one another

You could parameterize and reuse the same code for different objects
not sure it would be worth it unless you had a lot of objects

### #32 Mountain KingOFFLINE

Mountain King

Dragonstomper

• 689 posts

Posted Sat Feb 24, 2018 10:15 PM

I think I almost have it. You can move missile0. Press the button and player1 will follow the missile while player0 chases player1. However player1 doesn't always move correctly. I'm not sure what I'm doing wrong.

```dim ea = a
dim dy = b
dim dx = c
dim x1 = temp1
dim x0 = temp2
dim y1 = temp3
dim y0 = temp4
dim delay = h
dim rand16 = z

player0:
%11000000
%11000000
%00000000
%00000000
%00000000
%00000000
%00000000
%00000000
end

player1:
%11110000
%11110000
%11110000
%11110000
%00000000
%00000000
%00000000
%00000000
end
missile0x=80:missile0y=75
player1x = 60 : player1y = 60
player0x = 10  : player0y = 15

gosub setup_move
m=0
loop

COLUP0 = \$1C
COLUP1 = \$B4
COLUPF = \$62
drawscreen
delay = delay - 1
temp1 = 0
if joy0right then missile0x = missile0x + 1 : temp1 = 1
if joy0left then missile0x = missile0x - 1 : temp1 = 1
if joy0up then missile0y = missile0y - 1 : temp1 = 1
if joy0down then missile0y = missile0y + 1 : temp1 = 1
if player1x > 160 then player1x = 16
if player1x < 16 then player1x = 160
if player1y > 88 then player1y = 2
if player1y < 2 then player1y = 88

if missile0x > 160 then missile0x = 16
if missile0x < 16 then missile0x = 160
if missile0y > 88 then missile0y = 2
if missile0y < 2 then missile0y = 88
if joy0fire then m{0}=1 :temp1=1
if m{0} then gosub __setup2:gosub __move2
if collision(player0, player1) then gosub P0_rand : temp1 = 1
if collision(player1, missile0) then temp1 = 1:m{0}=0
if temp1 then gosub setup_move

if delay & 1 then gosub move

goto loop

P0_rand
player0y = rand & \$3F : player0y = (player0y / 8) + player0y + 8
player0x = rand & \$7F : player0x = (player0x / 8) + player0x + 16
return

gub
;player1y = rand & \$3F : player1y = (player1y / 8) + player1y + 8
;player1x = rand & \$7F : player1x = (player1x / 8) + player1x + 16
return

move
temp1 = ea

if f{0} then skip
player0y = player0y + yinc[f]
ea = ea - dx
if temp1 < ea then ea = ea + dy : player0x = player0x + xinc[f]
return
skip
player0x = player0x + xinc[f]
ea = ea - dy
if temp1 < ea then ea = ea + dx : player0y = player0y + yinc[f]
return

setup_move
x0 = player0x + 1 : y0 = player0y + 1
x1 = player1x + 1 : y1 = player1y

if x0 < x1 then f{2} = 1 : dx = x1 - x0 else f{2} = 0 : dx = x0 - x1
if y0 < y1 then f{1} = 1 : dy = y1 - y0 else f{1} = 0 : dy = y0 - y1

if dx > dy then f{0} = 1 else f{0} = 0
return

skip2

__move2

temp5 = p

if j{0} then __skip2
player1y = player1y + yinc[j]
p = p - n
if temp5 < p then p = p + o: player1x = player1x + xinc[j]
return

__skip2

player1x = player1x + xinc[j]
p = p - o
if temp5 > p then p = p + n : player1y = player1y + yinc[j]

temp1=1
return

__setup2

if (player1x+1) < missile0x then j{2} = 1 : n = missile0x - (player1x+1) else j{2} = 0 : n = (player1x+1) - missile0x
if player1y < missile0y then j{1} = 1 : o = missile0y - player1y else j{1} = 0 : o = player1y - missile0y

if n > o then j{0} = 1 else j{0} = 0
temp1=1
return
data yinc
\$FF, \$FF, \$01, \$01, \$FF, \$FF,\$01, \$01
end

data xinc
\$FF, \$FF, \$FF, \$FF, \$01, \$01, \$01, \$01
end

```

### #33 Mountain KingOFFLINE

Mountain King

Dragonstomper

• 689 posts

Posted Sun Feb 25, 2018 10:53 AM

I figured it out I had a greater than sign going the wrong way. see what a good night sleep will do.

```

__move2

temp5 = p

if j{0} then __skip2
player1y = player1y + yinc[j]
p = p - n
if temp5 < p then p = p + o: player1x = player1x + xinc[j]
return

__skip2

player1x = player1x + xinc[j]
p = p - o
if temp5 < p then p = p + n : player1y = player1y + yinc[j]

temp1=1
return
```

#### Attached Files

Edited by Mountain King, Sun Feb 25, 2018 11:22 AM.

#### 0 user(s) are browsing this forum

0 members, 0 guests, 0 anonymous users